2012-02-16 3 views
1

'지침'이라는 엔티티가 있습니다. 때로는 각 지침은 그 전후에 지침을 추적해야합니다. 예를 들어, 기존 명령어 A에서 계속 진행중인 새로운 명령어 B가있는 경우, 명령어 B는 명령어 A가 이전 명령어임을 인식해야하고 명령어 A는 명령어 B가 명령어 B가 그 다음 명령어임을 알고 있어야합니다. 모든 강좌가 강습 전후 강좌를 가질 수있는 것은 아닙니다.JPA : 일대일 + 자기 참조 + 양방향

JPA (EclipseLink)에서 이것을 구현하는 방법 : [일대일 + 자기 참조 + 양방향] 관계?

지금까지 (아직 작동하지 않는) 내가이 함께했다 :

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)

+0

답글을 달려면 http://stackoverflow.com/questions/3393515/jpa-how-to-have-one-to-many-relation-of-the-same-entity-type – perissf

답변

2

각 명령에 단일 명령 또는 여러 명령이 뒤따를 수 있는지 여부에 대한 혼동이 있습니다.

하나 인 경우 instructionNext에 대한 모음을 사용하지 마십시오.

많은 경우, JPA: How to have one-to-many relation of the same Entity type의 예제 코드가 도움이 될 것입니다. 앞의 지시 사항은 @ManyToOne이고 @OneToOne@OneToMany이 필요합니다.

+0

thx의 가능한 복제본 : D 각 명령어는 하나의 명령어 (일대일) 만 따라갈 수 있으므로 instructionNext를 선언 할 때 사용해야하는 것은 무엇입니까? –

+0

'Collection '를'Instructions'로 대체하십시오. - 작동합니까? –

+0

예 답변 : D .... thx 응답 –