【问题标题】:JPA: one-to-one + self referential + bidirectionalJPA:一对一 + 自引用 + 双向
【发布时间】:2012-03-07 15:12:58
【问题描述】:

我有一个名为“指令”的实体。有时,每个指令都必须跟踪它之前和之后的指令。例如,我有一个从现有指令 A 继续的新指令 B,指令 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

Local Exception Stack: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'atiits.instructions_instructions' doesn't exist Error Code: 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_id = ?) AND (t1.instruction_id = t0.instructionNextCollection_instruction_id)) bind => [874] Query: 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))") at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333) at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:687)

【问题讨论】:

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


【解决方案1】:

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

如果是单机,那么就不要使用集合进行指令下一个。

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

【讨论】:

  • 感谢您的回复:D,每条指令后面只能跟一条指令(一对一),那么我应该使用什么来声明指令Next?
  • 只需将Collection&lt;Instructions&gt; 替换为Instructions - 可以吗?
猜你喜欢
  • 1970-01-01
  • 2021-09-27
  • 2019-07-18
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多