2017-10-08 129 views
0

你好傢伙我得到這個錯誤我做錯了什麼? 這些都是我的課: ,這是錯誤我得到當我嘗試運行它hibernate映射異常未知mappedBy

異常線程「main」 org.hibernate.AnnotationException:未知的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; 
     } 
    } 

,這第三個

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; 
} 
} 

這是主類我tryed從

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

運行你有關係的雙方mappedBy。你只有把它放在你所認爲的擁有方一邊:

@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,如果我不清楚我想只更新孩子表引用父類保持不變。 –

0

您使用兩側的映射。它需要設置在一邊。使用StudentDetails中的學生映射:

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

這似乎解決了這個問題,但我仍然得到重複父類的條目,而不是他們只是被映射在joinedTable –