hibernate---一对一双向外键关联 (重要)

时间:2022-08-29 18:46:55

husband--wife: one to one 双向外键关联:

主导方:

@OneToOne

@JoinColumn(name="wifeId")

被主导方: @OneToOne(mappedBy="wife")

1. husband.java:

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne; @Entity
public class Husband {
private int id;
private String name;
private Wife wife;
@Id
@GeneratedValue
public int getId() {
return id;
} public String getName() {
return name;
}
@OneToOne
@JoinColumn(name="wifeId")
public Wife getWife() {
return wife;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(Wife wife) {
this.wife = wife;
} }

wife.java:

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne; @Entity
public class Wife {
private int id;
private String name;
private Husband husband;
@OneToOne(mappedBy="wife")
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
@Id
@GeneratedValue
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;
} }

hibernate.cfg.xml:

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne; @Entity
public class Wife {
private int id;
private String name;
private Husband husband;
@OneToOne(mappedBy="wife")
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
@Id
@GeneratedValue
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;
} }

junit语句:

package com.bjsxt.hibernate;

import java.util.Date;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; public class HibernateORMappingTest {
private static SessionFactory sessionFactory; //@BeforeClass
public static void beforeClass() {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
//@AfterClass
public static void afterClass() {
sessionFactory.close();
} @Test
public void testSchemaExport() {
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
}
public static void main(String[] args) {
beforeClass();
}
}

如果有双向关联 , mappedBy必设.

需要在wife里面设置, 这样husband作为主导.

xml里如何设置?

主导方: <many-to-one unique>   被主导方: <one-to-one property-ref>

1. student.java里设置:

private StuIdCard stuIdCard;
public StuIdCard getStuIdCard() {
return stuIdCard;
}
public void setStuIdCard(StuIdCard stuIdCard) {
this.stuIdCard = stuIdCard;
}

2. StuIdCard.java里设置:

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

3. Student.hbm.xml设置:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="com.bjsxt.hibernate.Student" dynamic-update="true">
<id name="id">
<generator class="native"></generator>
</id> <property name="name"></property>
<property name="age" />
<property name="sex" />
<property name="good" type="yes_no"></property>
<one-to-one name="stuIdCard" property-ref="student"></one-to-one>
</class> </hibernate-mapping>

4. StuIdCard.hbm.xml设置:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="com.bjsxt.hibernate.StuIdCard">
<id name="id">
<generator class="native"></generator>
</id> <property name="num"/>
<many-to-one name="student" column="studentId" unique="true"></many-to-one>
</class> </hibernate-mapping>

junit测试文件仍然是:

package com.bjsxt.hibernate;

import java.util.Date;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; public class HibernateORMappingTest {
private static SessionFactory sessionFactory; //@BeforeClass
public static void beforeClass() {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
//@AfterClass
public static void afterClass() {
sessionFactory.close();
} @Test
public void testSchemaExport() {
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
} public static void main(String[] args) {
beforeClass();
}
}