[英]Embeddable PK Object not populating after persist and flush
I have an embedded PK object that doesn't populate the id field after persisting and flushing to the database. 我有一个嵌入式PK对象,在持久化和刷新到数据库后不会填充id字段。 The ID is an auto-increment field in the database. ID是数据库中的自动增量字段。
Now normally, I would just try a refresh, but it throws the following error: 现在通常,我只是尝试刷新,但它会抛出以下错误:
"Entity no longer exists in the database: entity.Customers[ customersPK=entity.CustomersPK[ id=0, classesId=36 ] ]." “实体不再存在于数据库中:entity.Customers [customersPK = entity.CustomersPK [id = 0,classesId = 36]]。”
public class Customers implements Serializable {
@EmbeddedId
protected CustomersPK customersPK;
...
}
@Embeddable
public class CustomersPK implements Serializable {
@Basic(optional = false)
@Column(name = "id")
private int id;
@Basic(optional = false)
@NotNull
@Column(name = "classes_id")
private int classesId;
.....
}
And here's the code that makes the call 这是拨打电话的代码
Classes cl = em.find(Classes.class, classId);
CustomersPK custPK = new CustomersPK();
custPK.setClassesId(cl.getId());
Customers cust = new Customers(custPK);
em.persist(cust);
em.flush();
// The problem is right here where id always equals 0
int id = cust.getCustomerspk().getId();
Thanks for the help. 谢谢您的帮助。
Why would the id not be 0, you have never set it? 为什么id不是0,你从来没有设置它?
If it is a generated id, you need to annotate it using @GeneratedValue, otherwise you need to set the value. 如果它是生成的id,则需要使用@GeneratedValue对其进行注释,否则需要设置该值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.