In addition to previous post i got strange situation with persisting my object. Say we have beans operator and operatorProfiles defined as follows:
@org.hibernate.annotations.GenericGenerator(name="hibernate-native", strategy="increment") public class Operator implements java.io.Serializable, Cloneable { @Id @GeneratedValue(generator="hibernate-native") private Integer id;
@OneToMany(targetEntity=OperatorProfile.class, fetch=FetchType.LAZY, mappedBy="operator") @Cascade({ org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN }) private Set<Operatorprofile> profiles = new HashSet<Operatorprofile>(); ... }
@org.hibernate.annotations.GenericGenerator(name="hibernate-native", strategy="increment") public class Operatorprofile implements java.io.Serializable, Cloneable { @Id @GeneratedValue(generator="hibernate-native") private Integer id;
@ManyToOne(targetEntity=Subscriber.class, fetch=FetchType.LAZY) @Cascade({ org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.MERGE }) @JoinColumn(name="OperatorID", nullable=false) private Operator operator; ... }
Everything worked as expected until i manually inserted row in operatorProfile table with ID 0! Now every time when i try to add new profile to operator (add bean to profiles set and merge operator) i got my previous profile overwritten with new one! I checked hibernate logs and hibernate indeed call update on existing profile although i added new profile. But if i add new profile to operator which doesn't have that profile with id 0 in his set, everything is persisted properly (not overwritten) and hibernate calls insert on profile instead update.
I am very curious why this happened? Is there any chance that hibernate key generate strategy INCREMENT don't work if there is some row with primary key 0, manually inserted?
|