Hello,
I'm trying to save a class diagram in a database using hibernate with JPA. Most works fine but the associations can not be saved.
My structure is the following:
Code:
public class ClassModel {
...
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "attribute_fk")
@org.hibernate.annotations.Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
private Set<Attribute> attributes;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "source_association_fk")
@org.hibernate.annotations.Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
private Set<Association> sourceAssociations;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "target_association_fk")
@org.hibernate.annotations.Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
private Set<Association> targetAssociations;
...
}
Association:
Code:
public class Association {
..
@ManyToOne(cascade = CascadeType.ALL)
@org.hibernate.annotations.OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
@JoinColumn(name = "assoc_source_class_fk")
private ClassModel source;
@ManyToOne(cascade = CascadeType.ALL)
@org.hibernate.annotations.OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
@JoinColumn(name = "assoc_target_class_fk")
private ClassModel target;
..
}
Attribute:
Code:
public class Attribute {
...
@ManyToOne (cascade=CascadeType.ALL)
@org.hibernate.annotations.OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
@JoinColumn(name = "class_fk")
private ClassModel clasS;
...
}
I can save a new class with a new attribute without any problems, but if I create 2 new classes and add an association I will get an Exception:
javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not serialize
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:236)
...
Caused by: org.hibernate.type.SerializationException: could not serialize
at org.hibernate.util.SerializationHelper.serialize(SerializationHelper.java:158)
at org.hibernate.util.SerializationHelper.serialize(SerializationHelper.java:178)
...
Caused by: java.io.NotSerializableException: Association
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
If I change the cascading Type a little bit I will get this exception:
java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Association
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:644)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:236)
...
I really would appreciate any help, I was searching for a solution but failed.
Thanks in advance!
Best regards,
Alex