aspose file tools*
The moose likes Spring and the fly likes spring+hibernate: insert data into database(one to many) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "spring+hibernate: insert data into database(one to many)" Watch "spring+hibernate: insert data into database(one to many)" New topic
Author

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

Ivy chen
Greenhorn

Joined: Aug 31, 2010
Posts: 16
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?

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Victor Balanica
Greenhorn

Joined: Jan 11, 2011
Posts: 5

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

Joined: Aug 31, 2010
Posts: 16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: spring+hibernate: insert data into database(one to many)