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

标签 java hibernate

我使用 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>

最佳答案

我的猜测是您的 HotelUser 类上没有非私有(private)的默认(无参数)构造函数。

See this question for more details .

关于java - 使用 hibernate 从表中加载数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4171529/

相关文章:

java - 从生成的代码中编译并发出字节码

java - JPA和Hibernate Search的方法是什么?

java - JPA + Hibernate + 自动提交

java - 如何通过 HibernateTemplate 更新数据库中的特定字段

java - Hibernate 查询错误,集合定义为 Set

java - org.hibernate.LazyInitializationException : could not initialize proxy - no Session

javascript - Java 构建器模式可从 Rhino 中的 JavaScript 访问

java - 替换结果的html

java - 无法通过使用 Scanner 获取数据来从一个类创建两个对象

java - 在 Java 中生成全局唯一标识符