• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Can't get my mapping in order

 
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is probally asked a 1000 times, but I can't find a answer that works for me.
I Have a booktable, 6 other tables depend on it.
For example authors, the user should be able to add multiple authors to the same book.
So I created a seperate entity for authors which contains a list of authors and a book as foreign key, mapped as a manyToOne relationship.
When i try to save to the db, I get a detached entity pasted to persist error, when trying to save the second author with the same book.
Inline I red online the problem is caused by cascading all, but when I remove the cascade all, I get an error saying I have to save the transient variable first.
I also red it could be caused by autogenarating the ID, but when I remove the id, I got an error that I don't have an id.
Then I tried to asign the book_id to the id field, but then I he tries to update on second iteration.
I also red I should use cascade.Merge, but when I do I get the transient variable error again.
Any idea's?

The AuthorClass:



The bookClass:



Cause I did get a lot of different errors, when trying different things I didn't include a stacktrace
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When i try to save to the db, I get a detached entity pasted to persist error,...


Could you post the code that causes this error and the full stack trace of the error message?
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think the problem is in my controller, cause the first author is being saved, it's only on second iteration it goes wrong.
But anyways


The addBookClass:



Stacktrace when I try to merge:



Stacktrace with cascade all:



You see that the first author is being saved (I double checked with datagrip), it's on the second autor for the same books, shit hits the fan
 
Rancher
Posts: 4576
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the moment your model seems to imply that an Author can have multiple Authors.
That's clearly not correct.

What you have is a Many-to-Many relationship.
An Author can have written multiple Books, and a Book can have been written by multiple Authors.
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:At the moment your model seems to imply that an Author can have multiple Authors.
That's clearly not correct.

What you have is a Many-to-Many relationship.
An Author can have written multiple Books, and a Book can have been written by multiple Authors.



No, it suposed to be that way. One book should have multiple authors. As I explained in my question...
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I could be a manyTomany, but that dosn't mather, hibernate should be smart enough to figure that out, acording to my teatcher.
If I would change it to manyToMany, hibernate will expect a list of books for each author and I will get an error.
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic