我有實體,它有@EmbeddedId。在id類中有幾個字段,其中一個是具有生成類型序列的Long id。當我試圖插入這個實體的實例,而沒有在embeeded id中設置id時(應該從sequence中插入),問題會出現。EmbeddedId,sequence和ConstraintViolationException
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
當此embeddedId中的id設置爲任何值,則插入是成功的。
我能做些什麼來使它工作?
編輯:代碼
@Entity
@Table(name = "TestIds")
public class TestIds implements Serializable {
@EmbeddedId
private TestId testId;
public TestIds(TestId testId) {
this.setTestId(testId);
}
public TestId getTestId() {
return testId;
}
public void setTestId(TestId testId) {
this.testId = testId;
}
}
@Embeddable
public class TestId implements Serializable {
private static final long serialVersionUID = 1L;
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ")
@Column(name = "column_name")
private Long id;
public TestId() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
我執行
TestIds a = new TestIds(new TestId());
Session session = sessionFactory.getCurrentSession();
session.save(a);
session.flush();
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
Caused by:
java.sql.BatchUpdateException: Batch entry 0 insert into TestIds (column_name) values (NULL) was aborted
我定義sequance發電機使用@GeneratedValue。可嵌入的類不能有Id。 – mmatloka
編輯錯過對不起:S – osdamv
我已經添加了我的代碼和結果。 – mmatloka