-->
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.  [ 4 posts ] 
Author Message
 Post subject: AnnotationConfiguration().addPackage(packageName)
PostPosted: Thu Oct 25, 2007 11:40 am 
Newbie

Joined: Thu Oct 25, 2007 10:25 am
Posts: 2
Location: Denmark
Hi.

I have an issue with addPackage(), which i don't understand.

From what i can read it is possible to use this metod alone and add en entire package of entitys, but i can get it to work.

If i use addAnnotationdClass() there is not a problem, but den i have to add all the classes by themself.

Currently my code looks like this:
Code:
File configFile = new File(hibernateConfigurationFile);
SessionFactory sessionFactory = null;
if(packageName != null) {
   System.out.println("Package name: " + packageName);
   System.out.println("Hibernate configuration file: " + hibernateConfigurationFile);
      
   sessionFactory = new AnnotationConfiguration().addPackage(packageName).configure(configFile)
         .buildSessionFactory();
} else {
   sessionFactory = new AnnotationConfiguration().configure(configFile).buildSessionFactory();
}


System output is this:
Package name: it.courage.api.nas.db
Hibernate configuration file: etc/hibernate-no-datasource.cfg.xml

Error:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: it.courage.api.nas.db.Order
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:548)
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.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.get(Unknown Source)
at it.courage.api.nas.surveillance.SurveillanceApi.getOrder(SurveillanceApi.java:54)
at it.courage.api.nas.surveillance.SurveillanceApiTest.main(SurveillanceApiTest.java:19)

addAnnotatedClass() is working fine:
sessionFactory = new AnnotationConfiguration().addAnnotatedClass(Order.class).configure(configFile).buildSessionFactory();

Can someone tell me what i'm doing wrong???

/Stiwi.


Top
 Profile  
 
 Post subject: Re: AnnotationConfiguration().addPackage(packageName)
PostPosted: Thu Oct 25, 2007 5:54 pm 
Beginner
Beginner

Joined: Tue Feb 08, 2005 7:01 am
Posts: 21
Likely addPackage() refers only to package annotations, like those (or only those?) which appear in the special package-info.java source file. If that is true you cannot use this method to add all classes of a package.


Top
 Profile  
 
 Post subject: Re: AnnotationConfiguration().addPackage(packageName)
PostPosted: Fri Oct 26, 2007 4:27 am 
Newbie

Joined: Thu Oct 25, 2007 10:25 am
Posts: 2
Location: Denmark
dshk wrote:
Likely addPackage() refers only to package annotations, like those (or only those?) which appear in the special package-info.java source file. If that is true you cannot use this method to add all classes of a package.


Hi dshk.

You may be right, but this is a quote from Beginning hibernate ISBN: 1-59059-693-5 - page 126

Quote:
You can also add an annotated class to your Hibernate configuration programmatically.
The annotations toolset comes with an org.hibernate.cfg.AnnotationConfiguration object that extends the base Hibernate Configuration object for adding mappings. The methods on AnnotationConfiguration for adding annotated classes to the configuration are as follows:

addAnnotatedClass(Class persistentClass) throws MappingException
addAnnotatedClasses(List<Class> classes)
addPackage(String packageName) throws MappingExeption

Using these methods, you can add one annotated class, a list of annotated classes, or an entire package (by name) of annotated classes. As with the Hibernate XML configuration file, the annotated entities are interoperable with XML mapped entities.


The way i read it, it should be possible to add a package.

/Stiwi.


Top
 Profile  
 
 Post subject: Re: AnnotationConfiguration().addPackage(packageName)
PostPosted: Fri Oct 26, 2007 5:02 am 
Beginner
Beginner

Joined: Tue Feb 08, 2005 7:01 am
Posts: 21
I checked the source and addPackage doesn't try to add classes, only package level annotations. So I believe either the book is wrong, or the specification of addPackage was changed in the meantime.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.