编程语言
首页 > 编程语言> > Java-JPA:一对一的自我参照双向

Java-JPA:一对一的自我参照双向

作者:互联网

我有一个名为“指令”的实体.有时,每个指令都必须跟踪其前后的指令.例如,我有一条新的指令B,该指令从现有的指令A继续执行,指令B必须知道指令A是前一条指令,而指令A还必须知道指令B是其后的下一条指令.并非每个指令在指令之前和之后都会有.

如何在JPA(EclipseLink):[一对一的自引用双向]关系中实现此功能?

到目前为止(尚未工作),我想到了这个:

MySQL数据库:

CREATE TABLE instructions (
instruction_id int(11) NOT NULL AUTO_INCREMENT,
instruction_title varchar(100) NOT NULL,
instruction_text varchar(999) NOT NULL,
instruction_previous_id int(11) DEFAULT NULL,
PRIMARY KEY (instruction_id),
CONSTRAINT instructions_ibfk_3 
FOREIGN KEY (instruction_previous_id) 
REFERENCES instructions (instruction_id));

实体:

@Entity
@Table(name = "instructions")
public class Instructions implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "instruction_id")
private Integer instructionId;
@Basic(optional = false)
@Column(name = "instruction_title")
private String instructionTitle;
@Basic(optional = false)
@Column(name = "instruction_text")
private String instructionText;

@JoinColumn(name="instruction_previous_id", referencedColumnName = "instruction_id", nullable = true)
@OneToOne(optional = true)
private Instructions instructionPrevious;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "instructionPrevious")
private Collection<Instructions> instructionNextCollection;
// other properties, setter & getter
}

目前在创建新指令时没有问题,阅读时有错误

Instructions instruction = em.find(Instructions.class, instructionId);
instruction.getInstructionNextCollection().size(); //error this line

本地异常堆栈:
异常[EclipseLink-4002](Eclipse Persistence Services-2.0.1.v20100213-r6600):org.eclipse.persistence.exceptions.DatabaseException
内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表“ atiits.instructions_instructions”不存在
错误代码:1146
调用:SELECT t1.instruction_id,t1.instruction_urgent,t1.instruction_uploaded_by,t1.instruction_translate,t1.instruction_title,t1.instruction_type,t1.instruction_translate_received,t1.instruction_is_cancelled,t1.instruction_translate_sent,t1. ,t1.instruction_update_by,t1.instruction_create_by,t1.instruction_translator,t1.instruction_create_date,t1.instruction_company_id,t1.instruction_previous_id,t1.instruction_status_id FROM指令_instructions t0,t1指令t1 WHERE(((t0.Instructions_instruction_id =(1 = 0. .instructionNextCollection_instruction_id))
    绑定=> [874]
查询:ReadAllQuery(name =“ instructionNextCollection” referenceClass = Instructions sql =“ SELECT t1.instruction_id,t1.instruction_urgent,t1.instruction_uploaded_by,t1.instruction_translate,t1.instruction_title,t1.instruction_type,t1.instruction_translate_received,t1.instructioned,t1.instruction_title struction_translate_sent,t1.instruction_had_workorder,t1.instruction_text,t1.instruction_update_date,t1.instruction_update_by,t1.instruction_create_by,t1.instruction_translator,t1.instruction_create_date,t1.instruction_company_id,t1.instruction_previous_in( t0.Instructions_instruction_id =?)AND(t1.instruction_id = t0.instructionNextCollection_instruction_id))“)
    在org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
    在org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:687)

解决方法:

您的示例中有些混乱,是每个指令后跟一个指令还是多个指令.

如果是单个,则不要对collectionNext使用集合.

如果很多,那么JPA: How to have one-to-many relation of the same Entity type中的示例代码应该会有所帮助.您需要@ManyToOne用于前面的指令,而@OneToMany用于后面的指令,而不是@OneToOne.

标签:self-reference,one-to-one,jpa,bidirectional,java
来源: https://codeday.me/bug/20191201/2082983.html