4

「Instructions」というエンティティがあります。各インストラクションは、前後のインストラクションを追跡する必要がある場合があります。たとえば、既存の命令Aから継続している新しい命令Bがある場合、命令Bは命令Aが前の命令であることを認識し、命令Aも命令Bが次の命令であることを認識しなければなりません。すべてのインストラクションがインストラクションの前後にあるわけではありません。

これをJPA(EclipseLink)に実装する方法:[1対1 +自己参照+双方向]関係?

これまでのところ(まだ機能していません)私はこれを思いついた:

mysql db:

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)

4

1 に答える 1

4

あなたの例では、各命令の後に単一の命令を続けることができるか、それとも多くの命令を続けることができるかについて、いくつかの混乱があります。

単一の場合は、instructionNext にコレクションを使用しないでください。

多数の場合は、JPA のサンプル コード: 同じエンティティ タイプの一対多の関係を持つ方法が役立ちます。ではなく、@ManyToOne前の命令と次の命令が必要です。@OneToMany@OneToOne

于 2012-02-16T15:28:59.200 に答える