2017-10-08 10 views
0

こんにちは、私は間違って何をやっているのですか? これらは私のクラスは次のとおりです。 と私はスレッドでそれを「メイン」org.hibernate.AnnotationExceptionをハイバネーションマッピング例外不明mappedBy

例外を実行しようとすると、これは私が取得エラーです:不明mappedByで:hiberbiber.Student.studendetails、参照プロパティ不明:hiberbiber .StudenDetails.student

package hiberbiber; 

import javax.persistence.CascadeType; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.OneToOne; 
import javax.persistence.PrimaryKeyJoinColumn; 
import javax.persistence.Table; 


@Entity 
@Table(name = "STUDENTDETAILS") 
public class StudenDetails { 

private int id; 
private String jmbg; 
private int godine; 
Student student ; 
public StudenDetails() { 
} 

public StudenDetails(String jmbg, int godine) { 
    this.jmbg = jmbg; 
    this.godine = godine; 
} 

public StudenDetails(int id, String jmbg, int godine, Student student) { 
    this.id = id; 
    this.jmbg = jmbg; 
    this.godine = godine; 
    this.student = student; 
} 

@Id 
@GeneratedValue 
public int getId() { 
    return id; 
} 
@OneToOne(fetch = FetchType.LAZY,mappedBy = "student",cascade = 
CascadeType.ALL) 
@PrimaryKeyJoinColumn 
public Student getStudent() { 
    return student; 
} 

public void setStudent(Student student) { 
    this.student = student; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String getJmbg() { 
    return jmbg; 
} 

public void setJmbg(String jmbg) { 
    this.jmbg = jmbg; 
} 

public int getGodine() { 
    return godine; 
} 

public void setGodine(int godine) { 
    this.godine = godine; 
} 

} 
package hiberbiber; 

    import javax.persistence.Column; 
    import javax.persistence.Entity; 
    import javax.persistence.GeneratedValue; 
    import javax.persistence.Id; 
    import javax.persistence.Table; 

    @Entity 
    @Table(name = "PHONE") 
    public class Phone { 

    private long phoneId; 
    private String phoneType; 
    private String phoneNumber; 

    public Phone() { 
    } 

    public Phone(String phoneType, String phoneNumber) { 
     this.phoneType = phoneType; 
     this.phoneNumber = phoneNumber; 
    } 

    @Id 
    @GeneratedValue 
    @Column(name = "PHONE_ID") 
    public long getPhoneId() { 
     return this.phoneId; 
    } 

    public void setPhoneId(long phoneId) { 
     this.phoneId = phoneId; 

    } 

    @Column(name = "PHONE_TYPE", nullable = false, length = 10) 
    public String getPhoneType() { 
     return this.phoneType; 
    } 

    public void setPhoneType(String phoneType) { 
     this.phoneType = phoneType; 
    } 

    @Column(name = "PHONE_NUMBER", nullable = false, length = 15) 
    public String getPhoneNumber() { 
     return this.phoneNumber; 
    } 

    public void setPhoneNumber(String phoneNumber) { 
     this.phoneNumber = phoneNumber; 
     } 
    } 

この第1

package hiberbiber; 

import java.io.Serializable; 
import java.security.Identity; 
import java.util.HashSet; 
import java.util.Set; 
import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.JoinTable; 
import javax.persistence.OneToMany; 
import javax.persistence.OneToOne; 
import javax.persistence.Table; 


@Entity 
@Table(name = "STUDENT") 
public class Student implements Serializable { 

private long studentId; 
private String studentName; 
private Set<Phone> studentPhoneNumbers = new HashSet<>(0); 

private StudenDetails studendetails; 
public Student() { 
} 

public Student(String studentName, Set<Phone> studentPhoneNumbers) { 
    this.studentName = studentName; 
    this.studentPhoneNumbers = studentPhoneNumbers; 
} 

@Id 
@GeneratedValue() 
@Column(name = "STUDENT_ID") 
public long getStudentId() { 
    return this.studentId; 
} 


@OneToOne(fetch = FetchType.LAZY,mappedBy = "student",cascade = CascadeType.ALL) 
public StudenDetails getStudendetails() { 
    return studendetails; 
} 



public void setStudendetails(StudenDetails studendetails) { 
    this.studendetails = studendetails; 
} 

public void setStudentId(long studentId) { 
    this.studentId = studentId; 
} 

public Student(long studentId, String studentName, StudenDetails studendetails) { 
    this.studentId = studentId; 
    this.studentName = studentName; 
    this.studendetails = studendetails; 
} 

@Column(name = "STUDENT_NAME", nullable = false, length = 100) 
public String getStudentName() { 
    return this.studentName; 
} 

public void setStudentName(String studentName) { 
    this.studentName = studentName; 
} 

@OneToMany(cascade = CascadeType.ALL) 
@JoinTable(name = "STUDENT_PHONE", joinColumns = { 
    @JoinColumn(name = "STUDENT_ID")}, inverseJoinColumns = { 
    @JoinColumn(name = "PHONE_ID")}) 
public Set<Phone> getStudentPhoneNumbers() { 
    return this.studentPhoneNumbers; 
} 

public void setStudentPhoneNumbers(Set<Phone> studentPhoneNumbers) { 
    this.studentPhoneNumbers = studentPhoneNumbers; 
} 
} 

そして、これは私はあなたが関係の両側にmappedByを持って

public class HiberBiber { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    Session session = HibernateUtil.getFactory().openSession(); 
     Transaction tx = null; 
     try { 
      tx = session.beginTransaction(); 

      Set<Phone> phoneNumbers = new HashSet<Phone>(); 

      phoneNumbers.add(new Phone("house", "32333")); 
      phoneNumbers.add(new Phone("mobile", "06432333")); 


     Student student = new Student("eswar", phoneNumbers); 

     StudenDetails sd = new StudenDetails("123123", 13); 
     sd.setStudent(student); 

     student.setStudendetails(sd); 


     session.persist(student); 
     tx.commit(); 
     } catch (HibernateException e) { 
      tx.rollback(); 
     } finally{ 
      session.close(); 
     } 

    } 

} 

答えて

0

から実行するようにしようと試みたメインクラスです。あなたは両側のマッピングを使用している

@OneToOne(fetch = FetchType.LAZY,cascade = 
CascadeType.ALL) 
@PrimaryKeyJoinColumn 
public Student getStudent() { 
    return student; 
} 

@OneToOne(fetch = FetchType.LAZY, mappedBy="student",cascade = CascadeType.ALL) 
public StudenDetails getStudendetails() { 
    return studendetails; 
} 
+0

ありがとうございます。あなたは私の問題を解決しました。 –

+0

うれしいです。 –

+0

私はobejctを永続化するときに私のParentクラスのテーブルが挿入されるのを避ける方法を知っています。私が不明な場合は、同じままの親クラスを参照してkidテーブルのみを更新します。 –

0

:あなただけが所有している側であることを考えるの側にそれを設定します。片側に設定する必要があります。 StudentDetailsのStudentのマッピングを使用してください。

@OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL) 
@PrimaryKeyJoinColumn 
public Student getStudent() { 
    return student; 
} 
+0

それは問題を解決するようだが、私はまだjoinTableにマッピングされているのではなく、Parentクラスのエントリが重複してしまう –

関連する問題