【问题标题】:Hibernate MappingException Unknown entity: $Proxy2Hibernate MappingException 未知实体:$Proxy2
【发布时间】:2011-01-05 14:30:45
【问题描述】:

我正在使用 Hibernate 注释并且有一个非常基本的数据对象:

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class State implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
private String stateCode;

private String stateFullName;

public String getStateCode() {
    return stateCode;
}
public void setStateCode(String stateCode) {
    this.stateCode = stateCode;
}
public String getStateFullName() {
    return stateFullName;
}
public void setStateFullName(String stateFullName) {
    this.stateFullName = stateFullName;
}   

}

我正在尝试运行以下测试用例:

public void testCreateState(){
    Session s = HibernateUtil.getSessionFactory().getCurrentSession();

    Transaction t = s.beginTransaction();

    State state = new State();
    state.setStateCode("NE");
    state.setStateFullName("Nebraska");

    s.save(s);

    t.commit();

}

得到一个

org.hibernate.MappingException: Unknown entity: $Proxy2
    at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:628)
    at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1366)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
    ....

我无法找到任何引用错误的 $Proxy 部分的内容 - 并且不知所措.. 任何指向我所缺少内容的指针将不胜感激。

hibernate.cfg.xml

<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost/xdb</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>

<property name="current_session_context_class">thread</property>

<property name="dialect">org.hibernate.dialect.HSQLDialect</property>

<property name="show_sql">true</property>

<property name="hbm2ddl.auto">update</property>

<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

<mapping class="com.test.domain.State"/>

在 HibernateUtil.java 中

public static SessionFactory getSessionFactory(boolean testing ) {

    if ( sessionFactory == null ){
        try {


            String configPath = HIBERNATE_CFG;


            AnnotationConfiguration config = new AnnotationConfiguration();
            config.configure(configPath);
            sessionFactory = config.buildSessionFactory();
        } catch (Exception e){
            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }
    }

    return sessionFactory;
}

【问题讨论】:

  • 您有任何工具可以对您的 bean 进行 AOP 检测吗?
  • 构建 SF 时日志中有什么特别的内容吗?
  • 这只是为了确保在您的休眠配置文件中将类命名为 com.test.domain.State,但我在您的 POJO 中没有看到包声明。你是不是漏了一句?

标签: java hibernate annotations


【解决方案1】:

如果您将代码更改为以下内容,您的应用程序的输出是什么

Transaction t = s.beginTransaction();

State state = new State();
System.out.println(state.getClass().getName());
state.setStateCode("NE");

【讨论】:

    【解决方案2】:

    我对此的想法:也许你必须将状态代码上的@Id注解更改为@NaturalId。我认为@Id 指的是自动生成的 id,错误消息中也提到了这一点。

    【讨论】:

      【解决方案3】:

      我收到了同样的错误信息。它不会帮助你,但无论如何我都会为遇到这篇文章的其他人发布我的问题的解决方案。

      这不起作用:

      import org.hibernate.annotations.Entity;
      

      这确实有效:

      import javax.persistence.Entity;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-01
        相关资源
        最近更新 更多