My problem is that now I am using annotations, I can't get the entityName to work as it did when I used Hibernate XML mapping files. I expected that by defining @Entity(name="MyEntityName") in my class I would be able to refer to the entity by the name "MyEntityName" rather than by the class name. Debugging various bits of hibernate code show me that the entity name for the class is my.package.MyClassName.
Hibernate 3.2.2 GA, Hibernate Annotations 3.3.0 GA
Using annotations
My annotated class...
Code:
package my.package;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity(name = "AccountManager")
@Table(name = "ACCOUNT_MANAGER")
public class AccountManager extends User {
methods and stuff...
}
Code between sessionFactory.openSession() and session.close():Code:
getCurrentSession().get("AccountManager", -1);
Full stack trace of any exception that occurs:org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: AccountManager; nested exception is org.hibernate.MappingException: Unknown entity: AccountManager
Caused by: org.hibernate.MappingException: Unknown entity: AccountManager
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:68)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at org.springframework.orm.hibernate3.HibernateTemplate$2.doInHibernate(HibernateTemplate.java:484)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:367)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:478)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:472)
at my.package.AccountManagerDaoImpl.getAccountManager(AccountManagerDaoImpl.java:59)
at my.package.AccountManagerDaoTests.testGetUserById(AccountManagerDaoTests.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Name and version of the database you are using:Oracle 9
The generated SQL (show_sql=true):N/A
The "Java Persistence with Hibernate" book says, on page 174, that...
"With annotations, you can give an entity an explicit name, if the short name would result in a collision in the JPA QL or HQL namespace:
@Entity (name="AuctionItem")
public class Item {....}"
I have debugged the code a bit and hunted around and found the following in the org.hibernate.cfg.annotations.EntityBinder class in the annotations jar
Code:
public void bindEntity() {
persistentClass.setAbstract( annotatedClass.isAbstract() );
persistentClass.setClassName( annotatedClass.getName() );
persistentClass.setNodeName( name );
//persistentClass.setDynamic(false); //no longer needed with the Entity name refactoring?
persistentClass.setEntityName( annotatedClass.getName() );
It looks like the entity name is being set to the class name by default, which isn't what the book suggests, nor what used to happen when using hbm.xml files.
I am using the correct Entity class (i.e. the javax.persistence.Entity class).
Can anyone tell me what I need to do to use entity names with annotations?