EclipseLink 中@ManyToOne 映射的问题

我在使用这个 @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")

@Cacheable

public 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;


}


ABOUTYOU
浏览 195回答 1
1回答

沧海一幻觉

每次设置时都会创建新的 Manual,因此您的对象与 EntityManager 分离,或者根本没有数据。我不争论这是否是一个好的设计(虽然我从来没有这样做过),为了解决你的问题,你应该将CascadeType.PERSIST添加到你的关系中。@ManyToOne@JoinColumn(name = "CD_MANUAL", referencedColumnName = "CD_MANUAL", nullable = true, cascade = CascadeType.PERSIST)private Manual manual;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java