2012-08-13 4 views
0

Hibernate와 JPA를 사용하여 일대일 관계를 구현하고자했습니다. 계층 구조의 일부인 두 개의 클래스가 있습니다. 질문 계층 구조 및 응답 계층 구조.Hibernate, JPA - 단방향 one-to-one

@Entity 
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public abstract class QuestionUnit { 

    @Id 
    @GeneratedValue(strategy = GenerationType.TABLE) 
    private int id; 

    @OneToOne(cascade = CascadeType.ALL)  
    @PrimaryKeyJoinColumn 
    private AnswerUnit correctAnswer; 
...} 


@Entity 
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public abstract class AnswerUnit { 

    @Id 
    private int id; 

    public abstract Object getAnswerContent(); 

    public abstract boolean isEqual(AnswerUnit otherAnswer); 

    public int getId() { 
     return id; 
    } 
} 

우리는 OpenQuestion과 OpenAnswer를 구현으로 사용합니다.

OpenQuetions를 사용하는 테이블이 자동 생성 기본 키를 가지므로 OpenAnswer를 사용하는 테이블에는 OpenQuestion 테이블의 기본 키와 동일한 값을 가진 기본 키가 있습니다.

여기에서 예제를 따르려고했습니다. http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html 부분 2.2.5.1. 1-1. 나는 OpenQuestion을 계속 될 경우

는, 나는 열 id, id, answerContentquestionContent 및 OpenQuestionAnswer와 테이블 OpenQuestion를 얻을 수 있지만, 식별자의 값이 일치하지 않습니다.

그래서 나는 어디에서 실수하고 있습니까?

답변

0

"OpenQuestion"에 "CorrectAnswer"열이 없습니까?
"OpenAnswer"의 ID 열에 대한 외래 키 여야합니다.
- 일치하는 ID 값이어야합니다.
이 열이 없으면 JPA 매핑에 오류가있는 것 같습니다.
직접 편집 -
MappedSuperClass를 추가하십시오. 그것에 대해 here을 읽으십시오.

+0

전자 코드입니다. – Andna

+0

@MappedSupperClass도 필요합니다. 제 수정 된 답변을 참조하십시오. 나에게 그것에 대해 생각 나게 할 시간을 가졌다. –

+0

MappedSuperClass를 사용할 수 없으며 엔티티에 주석이없는 클래스에 매핑을 사용할 수없고 QuestionUnits의 ArrayList를 가진 QuestionList 클래스가 있습니다. – Andna

0

나는 무엇을 하려는지 확실하지 않습니다. 왜 당신은 질문에서 대답까지 일대일 관계가 있습니까? 논리는 질문이 여러 가지 대답을 가질 수 있음을 나타냅니다.

그리고 그 방법을 사용하면 경우 먹으 렴이 같은 모델이 있어야하는 경우, 두 아이디의 조건이 동일 걱정 할 필요가 없습니다 :이 같은 것을

@Entity 
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public abstract class QuestionUnit { 
    @Id ... private int id; 
    @OneToMany private List<AnswerUnit> answers; //optional assuming there's a list of answers for every question 
    @ManyToOne private AnswerUnit correctAnswer; 

    public void setCorrectAnswer(AnswerUnit answer){ 
     if(answer.getQuestion().equals(this)){ //or maybe instead answer.setQuestion(this) 
      this.correctAnswer = answer; 
     }else{ 
      throw new RuntimeError("Answer does not belong to this question"); 
     } 
    } 

    //Other getter and setters 
    //Override the "equals" method!!! 
} 


@Entity 
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public abstract class AnswerUnit { 
    @Id private int id; 
    @OneToMany private QuestionUnit question; 

    public abstract Object getAnswerContent();  
    public abstract boolean isEqual(AnswerUnit otherAnswer); //should't you just overwrite the equals method in here?  
    //Other getter, setters, etc 
} 

을 그리고 그것을 사용하는 :

QuestionUnit q = new OpenQuestion(); 
List<AnswerUnit> answers = new List<OpenAnswer>(); 

AnswerUnit a1 = new OpenAnswer(); 
AnswerUnit a2 = new OpenAnswer(); 
AnswerUnit a3 = new OpenAnswer(); 

a1.setQuestion(q); 
a2.setQuestion(q); 
a3.setQuestion(q); 

//Also set the contents and other required info for the questions... 

//And then associate the instances: 
q.setAnswers(answers); 
q.setCorrectAnswer(a3); 

em.persist(q); 

이도 개방적이고 정기적 인 답변과 함께 일하는 것이

내가 말한대로 포인트가 매핑이 일에 대해 난 단지`id`과`questionContent`, 그리고 모든 것을 가지고 OpenQuestion에,이다
+0

이 추상 클래스를 구현하는 클래스에는 질문 내용이라는 필드가 있기 때문에 질문에 단 하나의 대답 만 있습니다. 이 입력란의 유형은 질문의 유형에 따라 다릅니다. 그러나 모든 질문은 한 가지를 공유합니다. 정답을 갖고 있으며이 대답은 AnswerUnit 구체적인 클래스의 구체적인 하위 클래스이기도합니다. 그것이 정답을 가진 일대일이있는 이유입니다. – Andna

+0

그러나 그렇다면 모든 질문에는 오직 하나의 대답 만있을 수 있으며,이 경우 귀하의 모델을 수정하라고 조언합니다. 어쩌면 Embedabble 엔터티에 대한 답을하거나 해당 엔터티를 삭제하고 Question 필드에 직접 응답 필드를 포함시킬 수도 있습니다. –

관련 문제