• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I need help with Hibernate

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
hernan rodriguez
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hernan rodriguez ,

It seems your dialect


is not matching with driver

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
hernan rodriguez
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
reply
    Bookmark Topic Watch Topic
  • New Topic