-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Can't get entityName to work using annotations
PostPosted: Wed Mar 28, 2007 7:36 am 
Newbie

Joined: Wed Dec 01, 2004 9:29 am
Posts: 2
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?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 11:44 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
That would go against the spec

But I don't see the point of an entity name for an annotated class, you can't map the class twice through annotations

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 5:07 am 
Newbie

Joined: Wed Dec 01, 2004 9:29 am
Posts: 2
I suppose my problem is that it breaks my legacy code which had my DAOs using entity names.

It's not a big deal, it's just that the @Entity(name="MyEntity") format looks like it is defining an entity name, the book suggests that it is supported and the org.hibernate.cfg.annotations.EntityBinder class sets an entityName variable on the persistentClass instance to the class name rather than to the user defined entity name, which is available from the annotatedClass instance.

I take your point about it not being strictly necessary because you can't map the class twice with annotations, although I now have inconsistencies in my code where sometimes I can use "MyEntityName" and other times I have to use MyEntityClassName.class.

Thanks for your response, though. I'll stop using entity names from now on.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.