我有实体,有@EmbeddedId。在id类中有几个字段,其中一个是具有生成类型序列的Long id。当我尝试插入此实体的实例时,问题出现,而没有在embeeded id(应该从序列中插入)中设置id。
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
当此embeddedId中的id设置为任何值时,insert为successl。
我该怎么做才能让它发挥作用?
编辑:代码
@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
答案 0 :(得分:1)
似乎无法在@EmbeddedId
中使用@GeneratedValue答案 1 :(得分:0)
你应该将注释@GeneratedValue添加到id中,并采取你选择的行为。
例如为@Id
的错误编辑@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;