为了账号安全,请及时绑定邮箱和手机立即绑定

EclipseLink 中@ManyToOne 映射的问题

EclipseLink 中@ManyToOne 映射的问题

ABOUTYOU 2021-11-24 15:24:58
我在使用这个 @ManyToOne 地图时遇到了问题,搜索了很多,但仍然找不到解决此问题的方法。我有这两个类,我永远不会在 TB_MANUAL 中插入任何东西,我只是将它用作 TB_COMPANY 中 CD_MANUAL 字段的参考,如下所示:Company company = new Company();company.setManual("2"); //Theres already a row with this id in the TB_MANUAL然后坚持公司,但我收到了这个错误:Caused By: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: 2.at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.discoverUnregisteredNewObjects(RepeatableWriteUnitOfWork.java:313)at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.calculateChanges(UnitOfWorkImpl.java:723)at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1516)at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.issueSQLbeforeCompletion(UnitOfWorkImpl.java:3168)at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.issueSQLbeforeCompletion(RepeatableWriteUnitOfWork.java:355)Truncated. see log file for complete stacktrace——@Entity@Table(name = "TB_COMPANY", schema = "ADMPROD")@Cacheablepublic class Company implements Serializable {private static final long serialVersionUID = 1L;public Company() {}public Company(String id) {    this.id = id;}@ManyToOne@JoinColumn(name = "CD_MANUAL", referencedColumnName = "CD_MANUAL", nullable = true)private Manual manual;public void setManual(String idManual) {    this.manual = new Manual(idManual);}}和@Entity@Table(name = "TB_MANUAL")public class Manual implements Serializable{private static final long serialVersionUID = 1L;public Manual() {}public Manual(String id) {    this.id = id;}@Id@Column(name = "CD_MANUAL")private String id;@Column(name = "DS_OBS_MANUAL")private String description;}
查看完整描述

1 回答

?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

每次设置时都会创建新的 Manual,因此您的对象与 EntityManager 分离,或者根本没有数据。


我不争论这是否是一个好的设计(虽然我从来没有这样做过),为了解决你的问题,你应该将CascadeType.PERSIST添加到你的关系中。


@ManyToOne

@JoinColumn(name = "CD_MANUAL", referencedColumnName = "CD_MANUAL", nullable 

= true, cascade = CascadeType.PERSIST)

private Manual manual;


查看完整回答
反对 回复 2021-11-24
  • 1 回答
  • 0 关注
  • 194 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信