• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 3
Netbeans IDE Tomcat Server Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the more appropriate ORM forum for Hibernate.

Mark
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Andrej Kurochenko
Greenhorn
Posts: 3
Netbeans IDE Tomcat Server Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic