代码之家  ›  专栏  ›  技术社区  ›  M.J.

使用hibernate从表加载数据时出错

  •  2
  • M.J.  · 技术社区  · 14 年前

    我使用hibernate作为ORM工具,当从表中加载数据时,出现以下错误。

    org.hibernate.HibernateException: CGLIB Enhancement failed: com.hotel.entity.HotelUser
        at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxy(CGLIBLazyInitializer.java:96)
        at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.getProxy(CGLIBProxyFactory.java:49)
        at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:379)
        at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3455)
        at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:257)
        at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:191)
        at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
        at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
        at org.hibernate.impl.SessionImpl.load(SessionImpl.java:795)
        at org.hibernate.impl.SessionImpl.load(SessionImpl.java:788)
        at com.hotel.domain.UserLoginService.checkUserCredentials(UserLoginService.java:17)
        at com.hotel.app.UserLoginManager.checkUserCredentials(UserLoginManager.java:12)
        at com.hotel.app.UserLoginManager.main(UserLoginManager.java:23)
    Caused by: java.lang.InstantiationException: com.hotel.entity.HotelUser$$EnhancerByCGLIB$$fa712a57
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyInstance(CGLIBLazyInitializer.java:107)
        at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxy(CGLIBLazyInitializer.java:93)
        ... 12 more
    

    请让我知道,我错过了什么。。。

    下面是我的课

    public class HotelUser implements Serializable {
    
        private static final long serialVersionUID = 1L;
        private String userId;
        private String password;
        private String userName;
    
        private HotelUser() {
    
        }
        /**
         * @param userId
         * @param password
         * @param userName
         * @param lastLoginDate
         */
        public HotelUser(String userId, String password, String userName) {
            super();
            this.userId = userId;
            this.password = password;
            this.userName = userName;
        }
        /**
         * @return the userId
         */
        public String getUserId() {
            return userId;
        }
        /**
         * @param userId the userId to set
         */
        public void setUserId(String userId) {
            this.userId = userId;
        }
        /**
         * @return the password
         */
        public String getPassword() {
            return password;
        }
        /**
         * @param password the password to set
         */
        public void setPassword(String password) {
            this.password = password;
        }
        /**
         * @return the userName
         */
        public String getUserName() {
            return userName;
        }
        /**
         * @param userName the userName to set
         */
        public void setUserName(String userName) {
            this.userName = userName;
        }
    }
    

    hbm文件如下:

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
        <class name="com.hotel.entity.HotelUser" table="hotel_user">
            <id name="userId" type="string" column="USER_ID">
            </id>
            <property name="password" type="string" column="PASSWORD" />
            <property name="userName" type="string" column="USER_NAME" />
        </class>
    </hibernate-mapping>
    
    3 回复  |  直到 14 年前
        1
  •  9
  •   Community CDub    7 年前

    我的猜测是,您的 HotelUser 上课。

    See this question for more details .

        2
  •  0
  •   Swapna    14 年前

    我最近也面临同样的问题。

    这与web应用的类路径中有多个版本的cglib jar文件有关。尽管我不确定是这个jar文件,还是hibernate使用和依赖的其他jar文件,因为我在另一个版本的类路径中也有hibernate jar文件的其余部分。

    通过删除cglib和其他jar的冲突版本,一切又恢复了正常。

        3
  •  0
  •   Java_Help_Line    11 年前

    检查POJO类中的(默认)非参数构造函数。如果没有,请插入并重新编译