java - JPA:一对一+自引用+双向

标签 java jpa one-to-one bidirectional self-reference

我有一个名为“说明”的实体。有时每个指令都必须跟踪它之前和之后的指令。例如,我有新的指令 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 持久性服务 - 2.0.1.v20100213-r6600):org.eclipse.persistence.exceptions.DatabaseException 内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'atiits.instructions_instructions'不存在 错误代码:1146 Call: 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.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_id, t1.instruction_status_id FROM instructions_instructions t0, instructions t1 WHERE ((t0.Instructions_instruction_t1 = in.?) 和 ( .instructionNextCollection_instruction_id)) 绑定(bind) => [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.instruction_is_cancelled, t1 instruction_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_id, t1.instruction_status_id FROM instructions_instructions t0, instructions t1 WHERE (( 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)

最佳答案

在您的示例中,每条指令后面可以跟一个指令还是多个指令,有些混淆。

如果是单个的,那么就不要用集合来指令下一个。

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

关于java - JPA:一对一+自引用+双向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9307602/

相关文章:

java - Java 中的同步 Get 方法

java - 不要使用 spring jpa 从数据库获取信息

database - Django:遵循一对一关系的反方向

java - "and ' 和有什么区别?

java - 作为 Java 开发人员,C 还是 C++?

java - 尝试在 Hibernate 中使用 C3P0

java - JPA 中多列的不同

java - 一对一惰性 ="no-proxy"在 Hibernate 中不起作用

c++ - 存储许多关系 1 :1 between various type of objects : decoupling & high performance

java - 为什么在一个 Set 上使用 FetchType.LAZY 时会出现 LazyInitializationException,而在另一 Set 上则不会?