使用Hibernate注释的OneToOne双向映射

时间:2018-04-15 19:43:41

标签: java hibernate mapping

我想在我的2个实体上进行双向映射(PersonDetail,    PassportDetail)但似乎它没有正常工作。我要那个    PersonDetail有一个PassportId,PassportDetail也有一个PersonId。

我的PersonDetail java代码

@Entity
@Table(name="persondetail")
public class PersonDetail {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="person_id")
private int id;

@Column(name="person_name")
private String name;

@Column(name="person_phone")
private long phone;

@OneToOne(mappedBy="person",cascade=CascadeType.ALL)
@JoinColumn(name="passport_id")
private PassportDetail passport;

public PassportDetail getPassport() {
    return passport;
}

public void setPassport(PassportDetail passport) {
    this.passport = passport;
}

public int getId() {
    return id;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public long getPhone() {
    return phone;
}

public void setPhone(long phone) {
    this.phone = phone;
}
@Column(name="passport_id")
private int passort_id;

public int getPassort_id() {
    return passort_id;
}

public void setPassort_id(int passort_id) {
    this.passort_id = passort_id;
}
}

这是我的PassportDetail Java代码

@Entity
@Table(name="PassportDetail")
public class PassportDetail {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="passport_id")
private int id;

@Column(name="passport_number")
private String passportNumber;

@Column(name="country_name")
private String country;

@Column(name="issue_date")
@Temporal(TemporalType.DATE)
private Date date;

@OneToOne
@JoinColumn(name="person_id")
private PersonDetail person;


public String getPassportNumber() {
    return passportNumber;
}

public void setPassportNumber(String passportNumber) {
    this.passportNumber = passportNumber;
}

public int getId() {
    return id;
}

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

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public PersonDetail getPerson() {
    return person;
}

public void setPerson(PersonDetail person) {
    this.person = person;
}
}

这是MySql中的输出 Here is the Output of Both table as you can clearly see the problem i.e PersonDetail has a column named passport_id but it dosesn't have any value

以下是插入代码

public class MappingMain {

public static void main(String[] args) {

    PersonDetail person=new PersonDetail();
    PassportDetail passport = new PassportDetail();
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");

    person.setName("Ankit");
    person.setPhone(790148565);

    passport.setCountry("india");
    try {
    passport.setDate(sdf.parse("2018-04-15"));
    }
    catch(Exception e)
    {

    }

    passport.setPassportNumber("QUJMZ123");
    passport.setPerson(person);
    person.setPassport(passport);

    PersonDao dao=new PersonDao();
    dao.save(person);

}

这是DAO课程

public class MappingDao {

SessionFactory sf=Util.getSessionFactory();

public void save(UserDemo user)
{
    Session session=sf.openSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();
    session.close();

}
}

0 个答案:

没有答案