aspose file tools*
The moose likes Object Relational Mapping and the fly likes I need help with Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "I need help with Hibernate" Watch "I need help with Hibernate" New topic
Author

I need help with Hibernate

hernan rodriguez
Greenhorn

Joined: Mar 13, 2011
Posts: 14
hi, i'm using hibernate and i'm trying to use Criteria to get the data from a table in my db.

That's the code to do this:





I mapped the two tables i want to use in this example as follow:








And the relevant piece of code in my hibernate configuration file looks like:





I've added the mysql-connector-java-5.1.15-bin.jar driver in the project path and in the bin directory of Tomcat. It's weird because if I use HibernateTemplate all works fine. I don't know what else to do, someone could help me with this?


I want to do the join between both tables as well, e.g., get the country of a specified student, all at the class level. I don't want to do this with a JOIN HQL statements, and i think the best way is using Criteria, am i right?

Thanks,

regards
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3859

I've added the mysql-connector-java-5.1.15-bin.jar driver in the project path and in the bin directory of Tomcat.

Tomcat bin includes the executables for the Tomcat itself. Not the application jar files. Place the driver jar files in the lib directory corresponds to your Tomcat version.

I suggest you start with a simple setup which may insert an entity using Hibernate to work in the first place.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
hernan rodriguez
Greenhorn

Joined: Mar 13, 2011
Posts: 14
Vijitha Kumara wrote:
I've added the mysql-connector-java-5.1.15-bin.jar driver in the project path and in the bin directory of Tomcat.

Tomcat bin includes the executables for the Tomcat itself. Not the application jar files. Place the driver jar files in the lib directory corresponds to your Tomcat version.

I suggest you start with a simple setup which may insert an entity using Hibernate to work in the first place.


sorry, i meant lib directory of Tomcat, not bin. And i added it also in the WEB-INF directory of my project.

How could i do this? do you mean using Criteria? Is there another way? Formerly i used HibernateTemplate, just putting hibernateTemplate.save(entity) i could to insert an entity in the db. However, i couldn't join entities... just save or update a specific entity or getting a list of rows from a table in the db doing this: hibernateTemplate.find("from table"). Someone told me that this was not the right way to work with entities... so i tried using Criteria.

Karthikeyan Varadarajan
Ranch Hand

Joined: Jul 04, 2002
Posts: 98
hernan rodriguez ,

It seems your dialect


is not matching with driver



~With Smile<br />VK
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
your dialect should be as follows -

I want to do the join between both tables as well, e.g., get the country of a specified student, all at the class level. I don't want to do this with a JOIN HQL statements, and i think the best way is using Criteria, am i right?


It can be done by two ways -
1. by implementing a one-to-one condition for student and country in .hbm files
2. by using query OR criteria and attach the where condition.

If you have foreign key relationship established between your tables I would recommend first approach.


Regards
Shalindra Singh
SCJP, SCWCD
hernan rodriguez
Greenhorn

Joined: Mar 13, 2011
Posts: 14
thanks, I put <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> and it seems that hibernate creates the tables and their respective FKs, but when i try to use Criteria i'm still getting the same error. I also tried putting <property name="dialect">org.hibernate.dialect.MySQLDialect</property>.

All i have is what i showed you in my first post. Hibernate creates the tables from classes. And there are foreign keys of course. How would i do what you say? Could you show me with a simple example, please?


Thanks a lot
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
Hi Hernan!

I would like to indicate following things from your code -
1. error thrown indicate that your application is not able to pick the correct driver jar file. Sometimes this could be the problem with Tomcat. Can you please put the driver jar file in WEB-INF/lib (application library) instead of tomcat lib (server library).
2. I feel you would not like to save OR update country while updating records in Student table. I am not sure is required ?
3. I am not very sure how to do the same using annotation, but in .hbm file I would write in following way -

you should notice of use of many-to-many node and property node for country.

Let me know if you have any doubt .
hernan rodriguez
Greenhorn

Joined: Mar 13, 2011
Posts: 14
shalindra Singh Suryvanshee wrote:Hi Hernan!

I would like to indicate following things from your code -
1. error thrown indicate that your application is not able to pick the correct driver jar file. Sometimes this could be the problem with Tomcat. Can you please put the driver jar file in WEB-INF/lib (application library) instead of tomcat lib (server library).
2. I feel you would not like to save OR update country while updating records in Student table. I am not sure is required ?
3. I am not very sure how to do the same using annotation, but in .hbm file I would write in following way -

you should notice of use of many-to-many node and property node for country.

Let me know if you have any doubt .



Thanks, actually I already solved the issue. I put the code in the wrong place, i'm new at this...

Now i'm getting another error when I try to use Criteria in order to join two entities.

That's the code:



I tried too:



It throws the following error:



Someone help me please?

BTW, you're right, is not required at this time.
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
Hi Hernan!

you should be able to resolve your problem with the code I attached -


Here there are following things which need attention -

1. please observe last two line. both are written for student_country column
2. getter and setter method should be present for both 'country' and 'studentCountry' property in Student DTO.
3. you should use 'studentCountry' property to create criteria (as you already doing).
hernan rodriguez
Greenhorn

Joined: Mar 13, 2011
Posts: 14
shalindra Singh Suryvanshee wrote:Hi Hernan!

you should be able to resolve your problem with the code I attached -


Here there are following things which need attention -

1. please observe last two line. both are written for student_country column
2. getter and setter method should be present for both 'country' and 'studentCountry' property in Student DTO.
3. you should use 'studentCountry' property to create criteria (as you already doing).


Thanks a lot for your answers, I really don't know why should create another property for student_country column. My problem is already solved, i was putting the name of the property "student_country", but as i'm declaring the mapping relationships right above methods, should use their names without "set" and "get" prefix to refer them from criteria. I put
, and it worked fine.

Now i got another issue... I created a OneToMany relationship between Students and Phones, so now my entities looks like:





I just want to update the studentPhoneNumbers set from Student Entity using HibernateTemplate.


But it throw the following exception:



Somebody know how to fix it? I'd like to use hibernateTemplate.

I tried with session and it worked:




As I said, it didn't throw any exception, but didn't create the relationship either, created a new row in Phone table, but the join column (STUDENT_ID) appears empty (with a null value).

Same thing happens when i try to save a new Student with a new Phone, both Student and Phone are saved, but not the relationship.

how could i fix it?

I hope someone can help me out with this, thanks in advance.

regards



shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
are you using Spring along with Hibernate here, if yes, can you please paste the code of applicationContext.xml.
Actually If the association is lazy loaded and the Session has been closed then this won't work. You either need to eagerly load the association or reattach the entity to the Session before trying to lazy load.
try out with following lines of code -

hernan rodriguez
Greenhorn

Joined: Mar 13, 2011
Posts: 14
here is the code of applicationContext.xml:



And here the ClassDAOImpl class:





I pasted the code that you put, but eclipse detected an error that says: "context cannot be resolved", and gives me no option to import anything...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I need help with Hibernate