2010-01-15 95 views
2

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

回答

0

什麼是應用程序的輸出,如果你的代碼更改爲以下

Transaction t = s.beginTransaction(); 

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

我的想法對這個:也許你有改變的狀態代碼@NaturalId @Id註釋。我認爲@Id指的是自動生成的ID,這也是錯誤信息中提到的。

0

我得到相同的錯誤信息。這不會對你有所幫助,但是我會爲解決我的問題提供解決方案,以供其他人閱讀這篇文章。

這不起作用:

import org.hibernate.annotations.Entity; 

這並不工作:

import javax.persistence.Entity;