休眠MappingException未知实体:$ Proxy2将

问题描述:

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

我一直没能找到任何引用错误的$代理部分 - 并在亏损.. A指出我错过的东西将不胜感激。

的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; 
} 
+0

你有没有任何工具,正在做的豆AOP插桩? – Jherico 2010-01-15 18:15:58

+0

当SF正在建立时,日志中有什么特别的东西? – 2010-01-15 19:22:59

+0

这只是为了确保在您的hibernate配置文件中将类命名为com.test.domain.State,但在POJO中看不到包声明。你错过了一条线吗? – bogertron 2010-01-16 01:00:08

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

Transaction t = s.beginTransaction(); 

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

我的想法对这个:也许你有改变的状态代码@NaturalId @Id注释。我认为@Id指的是自动生成的ID,这也是错误信息中提到的。

我得到相同的错误信息。这不会对你有所帮助,但是我会为解决我的问题提供解决方案,以供其他人阅读这篇文章。

这不起作用:

import org.hibernate.annotations.Entity; 

这并不工作:

import javax.persistence.Entity;