wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes New value is inserted into db in @ManyToOne relation instead of update 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 "New value is inserted into db in @ManyToOne relation instead of update" Watch "New value is inserted into db in @ManyToOne relation instead of update" New topic
Author

New value is inserted into db in @ManyToOne relation instead of update

Andrej Kurochenko
Greenhorn

Joined: Jun 21, 2011
Posts: 3

Hello everybody,

I'm trying to create CRUD form using Spring FW 3.0.5 and Hibernate 3.4.0. In my web application i have two entities Author and Department in ManyToOne relation. Where one author belongs to one department and one department has many authors. When I add author with some department everything is OK. When I update author Spring is trying to insert new department, not just update it in database. Below are snippets of used code.


I've got Formatters, Controllers, AuthorService and DepartmentService interfaces and implementations for handling data operations. Controllers:


And the save.jsp page which contains spring form

When i add an author and then trying to update it (not changing the department) i'm getting an error

Spring is trying to insert same department. But I don't want to insert department, just update. The method which handles insert/update operations of Author entity:

So I'm asking for your advice and I'll be grateful for any hint of how to update, not insert department. Thanks in advance
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Moving this to the more appropriate ORM forum for Hibernate.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Hi Andrej,

Could you please answer couple of questions which would probably help me to solve this issue.

In the AuthorDAOImpl's updateAuthor(), where the Author object gets passed...

1) The author has the department object fully populated. From where the department object is retrieved...I mean, before saving the author are you getting the department object in a different query OR it is taken from some list in the memory ?

2) In your scenario, you are not changing the author's department but some other attribute of author ? Is this correct ?

3) Before saving the author, both the department and the author object are in detached mode ? Is this correct ?

Regards,
Naresh Waswani


Naresh Waswani
Andrej Kurochenko
Greenhorn

Joined: Jun 21, 2011
Posts: 3

Waswani Naresh wrote:
Hi Andrej,

Hello, Waswani
thank you for reply
Waswani Naresh wrote:
In the AuthorDAOImpl's updateAuthor(), where the Author object gets passed...

1) The author has the department object fully populated. From where the department object is retrieved...I mean, before saving the author are you getting the department object in a different query OR it is taken from some list in the memory ?

Well, here I just get Author object in AuthorController and pass it to AuthorService which passes object to aforementioned AuthorDAO. Nothing more is done. I've googled a lot and found very similar issue. So in my case Spring or Hibernate also don't know which department I am trying to update and can't map it to department in database. Maybe I'm passing wrongly list of departments in AuthorController.
Waswani Naresh wrote:
2) In your scenario, you are not changing the author's department but some other attribute of author ? Is this correct ?

Correct. Also my previous code inserted departments ID as name of department, but I've changed line with form:options from
toNow to reproduce exception I have to load update page and click on button. Exception occurs every time, because all values which are in select are also in database. And I have @Column(name="name", unique=true) to prevent duplicates. I don't need to change department table in database at all, I just need to change departments foreign key in authors table.
Waswani Naresh wrote:
3) Before saving the author, both the department and the author object are in detached mode ? Is this correct ?

I didn't knew what detached mode means so I've googled and found it in hibernate documentation. And as far as I know I'm not doing any detaching unless hibernate does it by itself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New value is inserted into db in @ManyToOne relation instead of update
 
Similar Threads
How to get Hebrew text from JSP page correctly?
Unable to call Controller method after form submission using Spring MVC 3
Bean cant be created error
Can't display validation error message
Spring model attribute is not populating values