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

  

  

  

  

hibernate---一对一双向外键关联 (重要)的更多相关文章

  1. hibernate一对一双向外键关联

    一对一双向外键关联:双方都持有对方的外键关联关系. 主控方和一对一单向外键关联的情况是一样的,主要的差异表现为,被空方需要添加: @OneToOne(mappedBy="card" ...

  2. Java进阶知识07 Hibernate一对一双向外键关联(Annotation+XML实现)

    1.Annotation 注解版 1.1.创建Husband类和Wife类 package com.shore.model; import javax.persistence.Entity; impo ...

  3. HIBERNATE一对一双向外键联合主键关联

    HIBERNATE一对一双向外键联合主键关联: 一. 创建主键类:这个主键必须实现serializedable接口和重写其中的hashCode方法和equals方法:为主键类添加一个叫做@Embedd ...

  4. 04-hibernate注解-一对一双向外键关联

    一对一双向外键 1,主控方的配置同一对一单向外键关联. 2,@OneToOne(mappedBy="card") //被控方 @OneToOne(mappedBy="ca ...

  5. Hibernate一对一单向外键关联

    一.一对一单向外键关联: 一对一单向外键关联主要用到了以下两个注解: 1.OneToOne(cascade=CasecadeTYPE.ALL); cascade=CasecadeTYPE.ALL:表示 ...

  6. Hibernate 再接触 关系映射 一对一双向外键关联

    凡是双向关联必设mapped by  由对方主导 wifi.java package com.bjsxt.hibernate; import javax.persistence.Entity; imp ...

  7. Java进阶知识06 Hibernate一对一单向外键关联(Annotation+XML实现)

    1.Annotation 注解版 1.1.创建Husband类和Wife类 package com.shore.model; import javax.persistence.Entity; impo ...

  8. Hibernate关系映射 一对一双向外键关联@OneToOne Annotation方式 双向关联和单向关联的区别

    首先还是来构造一个实际应用的场景,比如实体类车辆(Car),它具有以下属性:Id,品牌(brand),车牌(lisencePlate):实体类车牌(LisencePlate),它具有以下属性:Id,号 ...

  9. Hibernate关系映射 一对一双向外键关联@OneToOne Annotation方式

    首先还是来构造一个实际应用的场景,比如实体类车辆(Car),它具有以下属性:Id,品牌(brand),车牌(lisencePlate):实体类车牌(LisencePlate),它具有以下属性:Id,号 ...

  10. 012一对一 唯一外键关联映射_双向(one-to-one)

    ²  两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ²  有两种策略可以实现一对一的关联映射 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库 ...

随机推荐

  1. 关于WIFI DIRECT功能的

    http://processors.wiki.ti.com/index.php/WiFi_Direct_Configuration_Scripts#p2p_find      https://wire ...

  2. 改良UIScrollView滚动视图

    #define HEIGHT  self.view.frame.size.height #define WIDTH    self.view.frame.size.width @interface V ...

  3. linux sigaction信号处理

    sigaction函数相比signal函数更为复杂,但更具灵活性,下面具体介绍她的结构和用法: #include <signal.h> int sigaction(int signum, ...

  4. 重启库,提示找不到mysqld

    --ledir=/usr/local/mysql/bin    加上server的 directory https://dev.mysql.com/doc/refman/5.5/en/mysqld-s ...

  5. arcconf

    arcconf create 1 logicaldrive max volume 0 31 noprompt 创建 Logical Drive, 这里 0 31 就是之前记录的 Channel, De ...

  6. 设置TabBar分栏控制器上图片的大小问题

    我们都知道,iOS因为屏幕分辨率的问题,UID在交付我们iOS开发人员程序配图的时候,一般是三套图,分别对应三种不同的分辨率,对不同size的屏幕系统会自动使用不同像素的图片,我们只需要在命名时给三套 ...

  7. Spring Boot 系列教程3-MyBatis

    MyBatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache 迁移到了google code,并且改名为MyBatis .2013年11月迁移到Git ...

  8. Hibernate的dynamic-insert和dynamic-update的使用

    Hibernate在初始化的时候,默认按照配置为表预定义insert,delete,update,select(by id)的SQL语句放在session中,其中insert,update,selec ...

  9. robotframework常见问题解决汇总

    1.select window 失效 当关闭弹出框后,回到原页面,或者关闭弹出框后,又弹出新的对话框,导致select window 失效,报错 window not found 在select wi ...

  10. AppCompatActivity工具栏的设置(返回操作)

    <android.support.v7.widget.Toolbar android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionB ...