views:

406

answers:

3

I'm using Hibernate annotations and have a VERY basic data object:

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;
}   

}

and am trying to run the following test case:

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();

}

and get an

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)
    ....

I haven't been able to find anything referencing the $Proxy part of the error - and am at a loss.. Any pointers to what I'm missing would be greatly appreciated.

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"/>

in 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;
}
A: 

What is the output of your application if you change the code to the following

Transaction t = s.beginTransaction();

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

My idea on this: Maybe you have to change the @Id annotation on the state code to @NaturalId. I think @Id refers to automatically generated ids, something which is mentioned in the error message as well.

Lunikon
A: 

I was getting the same error message. It's not going to help you, but I'll post the solution to my problem anyway for others that come across this post.

This does not work:

import org.hibernate.annotations.Entity;

This does work:

import javax.persistence.Entity;
Ben McCann