J'utilise des annotations Hibernate et un objet de données très basique:
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;
}
}
et je tente d'exécuter le cas de test suivant:
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();
}
et obtenir un
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)
....
Je n'ai pas pu trouver quoi que ce soit faisant référence à la partie Proxy $ de l'erreur - et je suis à perte .. a Nys pointeurs à ce que je manque serait grandement apprécié.
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"/>
dans 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;
}
Avez-vous des outils qui font instrumentant AOP de vos haricots? – Jherico
Quelque chose de particulier dans les journaux lorsque le SF est construit? –
Pour vous assurer que dans votre fichier de configuration Hibernate, vous nommez la classe com.test.domain.State, mais je ne vois pas la déclaration du package dans votre POJO. Avez-vous juste manqué une ligne? – bogertron