可嵌入 PK 对象在持久和刷新后未填充
我有一个嵌入式 PK 对象,在持久保存并刷新到数据库后不会填充 id 字段。 ID是数据库中的一个自增字段。
现在通常情况下,我只是尝试刷新,但它会抛出以下错误:
“数据库中不再存在实体: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;
.....
}
这是拨打电话的代码
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();
感谢您的帮助。
I have an embedded PK object that doesn't populate the id field after persisting and flushing to the database. The ID is an auto-increment field in the database.
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 ] ]."
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么id不为0,你从来没有设置过?
如果是生成的id,需要使用@GeneeratedValue进行注解,否则需要设置值。
Why would the id not be 0, you have never set it?
If it is a generated id, you need to annotate it using @GeneratedValue, otherwise you need to set the value.