[英]Hibernate sequence generation not working for element of a composite key
I have a Hibernate entity with a composite key where the key is made up of a Long ID and a Timestamp. 我有一个带有复合键的Hibernate实体,其中的键由Long ID和Timestamp组成。 The ID should use a sequence generator.
该ID应该使用序列生成器。
MyEntity: MyEntity:
@Entity
@Table(name="MY_ENTITY")
public class MyEntity {
private MyEntityPk myEntityPk;
@EmbededId
public MyEntityPk getMyEntityPk() {
return myEntityPk;
}
// setter omitted
// other properties/getters/setters omitted
}
MyEntityPk: MyEntityPk:
public class MyEntityPk implements Serializable {
// version UID/hashCode()/equals() omitted
private Long myEntityId;
private Timestamp revisionTime;
@Column(name = "MY_ENTITY_ID", unique = true, nullable = false, precision = 13, scale = 0)
@GeneratedValue(strategy=GenerationType.AUTO, generator="HIBERNATE_SEQUENCE_GEN")
@SequenceGenerator(name="HIBERNATE_SEQUENCE_GEN", sequenceName="HIBERNATE_SEQUENCE")
public Long getMyEntityId() {
return myEntityId;
}
public void setMyEntityId(Long myEntityId) {
this.myEntityId = myEntityId;
}
@Column(name = "REVISION_TS", nullable = false)
public Timestamp getRevisionTime() {
return revisionTime;
}
public void setRevisionTime(Timestamp revisionTime) {
this.revisionTime = revisionTime;
}
}
The PK looks like it's wired up correctly. PK似乎已正确连接。 When I do a find all, I have a list of MyEntity objects where the MyEntityPk objects for each have an id and revisionTime from the database.
当我查找全部内容时,我将获得一个MyEntity对象列表,其中每个MyEntityPk对象都具有数据库中的ID和RevificationTime。
On insert, though, the following is giving me an error indicating that the MY_ENTITY_ID is null and the db won't allow a null value for that column. 但是,在插入时,以下内容给我一个错误,指示MY_ENTITY_ID为空,并且数据库不允许该列的空值。 With our other entities, we can insert and leave our auto-generated IDs null and Hibernate knows to grab the next sequence from the db and use it in the entity.
与其他实体一起,我们可以插入自动生成的ID并将其保留为空,Hibernate会从数据库中获取下一个序列并将其用于实体中。 In this case, for some reason that's not happening:
在这种情况下,由于某种原因没有发生:
MyEntity e = new MyEntity();
MyEntityPk pk = new MyEntityPk();
pk.setRevisionTime(new Timestamp(System.currentTimeMillis()));
//pk.setMyEntityId(5l); // see note below
e.setMyEntityPk(pk);
// setting a bunch of other entity parameters omitted
Session session = getSession();
session.saveOrUpdate( entity );
Ends up giving me: 最终给了我:
// a bunch of stack trace omitted
Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("MY_SCHEMA"."MY_ENTITY"."MY_ENTITY_ID")
// more stack trace omitted
Note that if I set the PK object's ID manually, the insert will work. 请注意,如果我手动设置PK对象的ID,则插入将起作用。
I only ever used composite keys which are references to other tables, but take a look at that one JPA Composite Key + Sequence 我只使用过组合键,这些组合键是对其他表的引用,但是请看一下该JPA组合键+序列
At first seems like bad news, but if you read the comments of the accepted answer you may get lucky 起初似乎是个坏消息,但是如果您阅读了已接受答案的评论,可能会很幸运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.