Reputation: 499
i have the following two entities and one join table:
entity:
@Entity
@Table(name = "PERSON")
public class parent {
@OneToOne(mappedBy="person", cascade= CascadeType.ALL)
private Reader reader;
//more fields and getters and setters and of course @Id.....
}
and another entity:
@Entity
@Table(name="READER")
public class Reader{
@Id
@Column(name= "READER_ID")
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="Person"))
private long readerId;
@OneToOne
@PrimaryKeyJoinColumn
private Person person;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "READER_BOOKS", joinColumns={@JoinColumn(name="READER_ID")}, inverseJoinColumns={@JoinColumn(name="BOOK_ID")})
private List<Books> books;
//more fields and getters and setters
}
And i have a join table in the data base which is named "READER_BOOKS" which is consisted with two columns - readerId and bookId - both are a primary key as a pair, and each of the columns is a foreign key to the corresponding table.
A few details:
When i add a Person and set the reader field with several books, it works and the database is filled with the right values.(a person,reader and join tables are inserted with new rows)
The problem is when i delete the Person instance OR the Reader instance AND the books instance is not null(books are assigned to reader). hibernate is just stuck and not throwing any execption. but when the books instance is null (didnt assign any books to the reader) the deletion is successfull.
I think that something might be wrong with the "ManyToMany" , because when the join table is empty the deletion is successful.
what is wrong in my implementation?
Edited: i found out sometimes hibernate gets stuck even if that list is empty, so i think something is also wrong with the "OneToOne" anottation, maybe i missed out something
Upvotes: 0
Views: 989
Reputation: 499
there was no problem in my implementation. i use PL/SQL developer and i didn't notice but it acquired a lock on one of the tables :|. so when i tried to delete a record, hibernate got stuck and from some reason didn't throw any error.
Any way, the implementation is correct.
Upvotes: 1