I am working with JPA and use Hibernate as a provider to my SQL Server database.
I need a many-to-many self referencing relation that has an additional column or even more additional columns.
That is my current code. I am getting exceptions by Hibernate:
@Entity
public class Person {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "person", fetch = FetchType.EAGER)
private Set<Relation> relations;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "relPerson", fetch = FetchType.EAGER)
private Set<Relation> inverseRelations;
}
@Entity
public class Relation implements Serializable {
@Id
@ManyToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.EAGER)
@PrimaryKeyJoinColumn(name = "PersonID", referencedColumnName = "id")
private Person person;
@Id
@ManyToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.EAGER)
@PrimaryKeyJoinColumn(name = "RelPersonId", referencedColumnName = "id")
private Person relPerson;
}
During runtime i get an exception from hibernate:
org.hibernate.TransientObjectException: object references an unsaved transient instance
Is there any way to implement this a little bit more intelligent and nicely?? Without getting that exception.
Thanks, ihrigb
If an object not associated with a Hibernate Session
, the object will be Transient
.
An instance of Relation
list may be Transient
(Normally, There is no identifier value for that instance) when you save Person
.
Here is better way to understand object state.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.