When composite ID has at least one IDENTITY generated value, you cannot persist an entity.
Suppose you have a class (getters-setters and other stuff is cut to be shorter):
{ { code} @Entity @Table(name = "animals") @IdClass(IdWithSubId.class) public class Animal { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Id @Column(name = "sub_id") private Long subId; }
public class IdWithSubId implements Serializable { private static final long serialVersionUID = -2304656293054567177L; private Long id; private Long subId; } {code } }
When you try to persist in debug 1) # you will get to org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(...) line 117: {{Serializable generatedId = persister.getIdentifierGenerator().generate( source, entity );}} 2) # then org.hibernate.mapping.Component.ValueGenerationPlan.execute(...) line 440 will set {{generatedValue = IdentifierGeneratorHelper.POST_INSERT_INDICATOR}} 3) # later org.hibernate.property.BasicPropertyAccessor.BasicSetter.set(...) line 68 will try to set {{IdentifierGeneratorHelper.POST_INSERT_INDICATOR}} (String) to Long and this will cause an exception, which will bubble up, but not handled appropriately, so the code will never return to org.hibernate.event.internal.AbstractSaveEventListener line 124 for comparing generated ID with {{IdentifierGeneratorHelper.POST_INSERT_INDICATOR}}
This is not happening when ID is non-composite.
Also I have reported somewhat related bug #HHH-9657 . In general looks like Hibernate is a bit weak with composite IDENTITY IDs |
|