EclipseLink中的@ManyToOne映射问题

时间:2018-10-17 21:55:43

标签: java oracle jpa weblogic eclipselink

我在处理@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;

}

2 个答案:

答案 0 :(得分:0)

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

我不认为这是否是一个好的设计(尽管我从来没有那样做过),要解决您的问题,应在您的关系中添加 CascadeType.PERSIST 。< / p>

@ManyToOne
@JoinColumn(name = "CD_MANUAL", referencedColumnName = "CD_MANUAL", nullable 
= true, cascade = CascadeType.PERSIST)
private Manual manual;

答案 1 :(得分:0)

问题出在Manual表主键中,JPA找不到ID为1的任何行,因为Manual的主键是char(2),传递“ 1”而不是“ 1”解决了问题。