简体   繁体   English

EclipseLink中的@ManyToOne映射问题

[英]Problem with @ManyToOne map in EclipseLink

I'm having trouble with this @ManyToOne map, searched a lot, but still can't find a solution for this problem. 我在此@ManyToOne地图上遇到了麻烦,进行了很多搜索,但仍然找不到解决此问题的方法。

I have these two classes, i will never insert anything into TB_MANUAL, i'll just use it as reference for the CD_MANUAL field in TB_COMPANY, like this: 我有这两个类,我将永远不会在TB_MANUAL中插入任何内容,而只是将其用作TB_COMPANY中CD_MANUAL字段的引用,如下所示:

Company company = new Company();
company.setManual("2"); //Theres already a row with this id in the TB_MANUAL

and then persist company, but i got this error: 然后坚持陪伴,但是我得到了这个错误:

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);
}




}

and

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

}

You create new Manual every time you set it, so your object is detach from EntityManager, or has not data at all. 您每次设置新手册时都会创建它,因此您的对象与EntityManager分离,或者根本没有数据。

I don't argue if that is a good design (althought I've never would do it like that), to over come your problem you should add CascadeType.PERSIST to your relation. 我不认为这是否是一个好的设计(尽管我从来没有那样做过),要解决您的问题,应在您的关系中添加CascadeType.PERSIST

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM