my dog learned polymorphism*
The moose likes Object Relational Mapping and the fly likes hbm file version working but annotation not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hbm file version working but annotation not" Watch "hbm file version working but annotation not" New topic
Author

hbm file version working but annotation not

Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
I am using Netbenas IDE.
I did same example using hbm file and annotation.

here my config file


here my hbm file


here is my User class


here is main class

this example is working fine.No problem.


anyway i try same with annotation
here my annotation class


then it gives this error

Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
i change my User class


still i am getting error

Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Hi Samanthi perera,
You have added your hbm file version but not the annotated class.
When you are using annotated version, it must be specified like or using the package.


A.A.Anbarasu
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
here is my new config file


still i get error

Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Replace above line by then try
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
ya .i did that .
then it gives me tyhis error

i don't know why it can't find my class.
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Hibernate Annotations comes with a really great documentation for beginners. Read it and you'll see, that you have to write something like that



and not like that



The unfriendly part in "a friendly place for Java greenhorns":
Did you ever hear about such things called "tutorial" or "documentation"? Do you really really really want to learn Java, Hibernate and the other things by posting each and every little problem into forums? You've been told that in some other threads... RTFM! Or try google at first.
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Hi Samanthi perera,
replace above line by
Use . not /.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
i am using this tutorial

http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-one-using-annotations-1.html
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
The solution to you question is in that tutorial...
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
no.
Still i have the error


Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Maybe you should repost your configuration, your mapped class and your main class. Because no one knows what you changed between to different runs. E.g. one of your stacktraces contained this: "people/User.hbm.xml". Now you class is Annotation.User or isn't it or is it or not?
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
this is my config file


User class


util class


error

Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
There are several classes called Annotation. Maybe Hibernate assumes Annotation.User to be an inner class of some of these classes. E.g. java.text.Annotation. Rename your package to annotation. Naming conventions aren't always useless... In this special case: each part of the fully qualified package-name are written in lower-case.
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Check the Entity package you are importingI think it must be
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
config class


user class


Now it is working.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
config class


user class


Now it is working.
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

SessionFactory sessionFack=new AnnotationConfiguration().configure().buildSessionFactory();
I think you are not using the cfg.xml file
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
why did you say that?
what is the meanning of that?
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

That's my guess. I think you are using two classes for building session factory. One is for using cfg.xml file and another one is for annotated version.
If you are using annotated version for building session factory then no need to worry about your cfg file.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
this is my user class


this is my cfg.xml


If you are using these two classes can you tell me how to make a SessionFactory using
1)only cfg.xml
2)only annotated class
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

I think you are not getting my point. post "HibernateUtil" and "AnnotationConfiguration" class codes.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
aha.then it is ok.
I am not using the Hibernate util class.I mean in this example.
anyway it is here.

Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Anbarasu Aladiyan wrote:I think you are not getting my point. post "HibernateUtil" and "AnnotationConfiguration" class codes.


Why are you doing this? If you want to help him, don't ask him for code of AnnotationConfiguration. That's a Hibernate class not his own.
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Christian Dillinger wrote:
Anbarasu Aladiyan wrote:I think you are not getting my point. post "HibernateUtil" and "AnnotationConfiguration" class codes.


Why are you doing this? If you want to help him, don't ask him for code of AnnotationConfiguration. That's a Hibernate class not his own.
There are two ways to build the session factory. Using cfg.xml file and another one is without using the cfg xml file (in which we can give the configuration details using java code. Which is likely used when we do annotated version).
If we use annotated version then obliviously it has nothing to do with cfg file (since we can configuration details using java code when we build session factory)

Also I don't think that is possible to help without seeing the code
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Anbarasu Aladiyan wrote:There are two ways to build the session factory. Using cfg.xml file and another one is without using the cfg xml file (in which we can give the configuration details using java code. Which is likely used when we do annotated version).
If we use annotated version then obliviously it has nothing to do with cfg file (since we can configuration details using java code when we build session factory)

Also I don't think that is possible to help without seeing the code


Why should anyone want to get rid of the cfg.xml completly just because he uses annotated entities? You because you CAN configure your annotated classes programmatically it's by far not OBVIOUS that you do not want a hibernate.cfg.xml. You need the class AnnotationConfiguration if you want to use annotated classes, that's the whole difference.
What is easier when you add a new entity? Insert one line in a textfile without recompiling or change code in another class?

BTW we seen his complete code over and over again in the thread and the problem finally got solved...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hbm file version working but annotation not
 
Similar Threads
Problem in <one-to-one> relation mapping
one to one mapping problem
query.list() and query.uniqueResult() in hibernate return null
Error with Hibernate
org.hibernate.exception.GenericJDBCException