• 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

spring+hibernate: insert data into database(one to many)

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,everyone~

I'm new to spring~now I'm doing a “one to many”practice with spring & hibernate~how ever somes problems come out~

Details:
in the database(Mysql) there are two tables:"User" & "Order"~(One to Many): one "User" to many "Order"~i want to insert data into the table"Order"~

Problems:can not insert the data into the table"Order".and the problems cause in the code "OrderServiceImpl".

here is my code:
(Spring:ApplicationContext.xml)


User.hbm.xml


Order.hbm.xml


OrderDaoImpl


OrderServiceImpl(problems cause in these code)


i don't know how to solve the problems~help~thanks~

another question:when i add the spring capability into the project, the speed of querying is lower than only add hibernate into the project~why would it happen?

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I found a few problems.

1) User and Order should not be a Spring Bean, they are domain objects and hold state. Not good for a Spring Bean
2) You are creating an ApplicationContext object directly in your OrderService, it should be outside of your classes that you are defining as Beans. So one problem is everytime you call addOrder, it goes and creates a brand new ApplicationContext. You should only have one of these for your entire application. ApplicationContext is a heavy-weight object that can take a little bit of time to instantiate, There is a whole bunch of steps in Initialization that occurs when creating an ApplicationContext.

3) This is not a Spring bug, but a Hibernate issue. You have from User to Order a cascade option, but you do not have a cascade option from Order to User. And since you save the Order, it will not do anything to User. And I would assume this is a new user and the database would have a FK constraint in Order table for the user id. So the User information must be in the User table before any Order can be inserted into the Order table for that User. But you are looking up a User, so if it exists in the database, then that wouldn't be a problem.

Please post your exception stack trace because we are missing that context information on what is you actual problem.

Mark
 
Greenhorn
Posts: 5
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this is a new project, you might want to look into using more of the stereotype annotations, the component scanning and the autowiring functionality. It will lower the amount of code you have to handle at xml file level and allow you to configure a bean just by annotating it.
 
Ivy chen
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Spritzler wrote:Well, I found a few problems.

1) User and Order should not be a Spring Bean, they are domain objects and hold state. Not good for a Spring Bean
2) You are creating an ApplicationContext object directly in your OrderService, it should be outside of your classes that you are defining as Beans. So one problem is everytime you call addOrder, it goes and creates a brand new ApplicationContext. You should only have one of these for your entire application. ApplicationContext is a heavy-weight object that can take a little bit of time to instantiate, There is a whole bunch of steps in Initialization that occurs when creating an ApplicationContext.

3) This is not a Spring bug, but a Hibernate issue. You have from User to Order a cascade option, but you do not have a cascade option from Order to User. And since you save the Order, it will not do anything to User. And I would assume this is a new user and the database would have a FK constraint in Order table for the user id. So the User information must be in the User table before any Order can be inserted into the Order table for that User. But you are looking up a User, so if it exists in the database, then that wouldn't be a problem.

Please post your exception stack trace because we are missing that context information on what is you actual problem.

Mark



Dear Mark:
Thanks for your answer,with your help,i solve the problem.And you help me to know more about the Spring.

Next time i will post the exception in the topic.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic