`

hibernate 的 一对一的映射 annotation

 
阅读更多
hibernate 的 一对一的映射 
数据表的关系 有主键管理 有外键关联  下面的先是外键关联
先从小实验开始

第一:建立一个两个类
package com.nnl.www;

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

@Entity
public class Wife {
private int id;
private String name;
@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;
}

}

---------------------------


package com.nnl.www;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Hunsander {
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;
}

}
然后到hibernate.cfg.xml去设置


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
       <!--  <property name="connection.pool_size">1</property> -->

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
         <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <!--  <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>-->

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

     <!-- <mapping resource="com/nnl/www/Student.hbm.xml"/> -->
     <mapping class="com.nnl.www.Hunsander"/>
      <mapping class="com.nnl.www.Wife"/>
      
    </session-factory>

</hibernate-configuration>
---------------------------
然后到Junit测试
package com.nnl.www;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;


public class TestOneToOne {
@Test
public void testOneToOne(){
	new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
}
}







分享到:
评论

相关推荐

    Hibernate一对多、一对一、多对多映射之Annotation篇.doc

    Hibernate一对多、一对一、多对多映射之Annotation篇

    Hibernate_Annotation关联映射

    1.共享主键的一对一关联映射: @Entity @Table(name="Test_Body") public class Body { private Integer id; private Heart heart; @Id public Integer getId() { return id; } public void setId(Integer id...

    hibernate annotation 中文文档

    一对一(One-to-one) 2.2.5.2. 多对一(Many-to-one) 2.2.5.3. 集合类型 2.2.5.4. 用cascading实现传播性持久化(Transitive persistence) 2.2.5.5. 关联关系获取 2.2.6. 映射复合主键与外键 2.2.7. 映射二级表...

    Hibernate1对多1对1多对多关联映射例子源码含注解配置

    在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式以后,发现使用...

    hibernate的中所有关系映射(分别用xml和jpa实现)

    hibernate的中关系映射分为一对一、一对多、多对一、多对多,其中有单双向之分。而一对一还有主键和外键区分。一共是九种。分别用xml配置文件和annotation实现。annotation的版本没有写测试程序,见谅。

    Hibernate1对多1对1多对多关联映射例子源码含xml配置

    在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,这个是xml配置的例子

    hibernate annotation帮助文档

    一对一(One-to-one) 2.2.5.2. 多对一(Many-to-one) 2.2.5.3. 集合类型 2.2.5.4. 用cascading实现传播性持久化(Transitive persistence) 2.2.5.5. 关联关系获取 2.2.6. 映射复合主键与外键 2.2.7. 映射二级表...

    hibernate注解案例

    Hibernate 的注解学习 ExportDBAnnotation HibernateAnnotationUtils 【一对一】关系映射【一对多】关系映射 【多对多】关系映射 【composite复合主键】关系映射【component组件】关系映射

    Spring和Hibernate学习笔记

    第四课:hibernate多对一关联映射 ...................... Spring: 第一课:构建Spring的环境并建立一个例子 第三课:spring Bean的作用域 第四课:spring对AOP的只是(采用Annotation的方式) 第五课:spring对AOP...

    精通Java Web整合开发(第2版)

    12.4.4 一对一关联的annotation注解实现536 12.4.5 多对一单向关联的annotation注解实现538 12.4.6 一对多双向关联的annotation注解实现540 12.4.7 一对多双向自身关联的annotation注解实现542 12.4.8 多对多单向...

    Hibernate4注解全教程

    借助新的 Hibernate Annotation 库,即可一次性地分配所有旧映射文件——一切都会按照您的想法来定义——注解直接嵌入到您的 Java 类中,并提供一种强大及灵活的方法来声明持久性映射。籍由自动代码完成和语法突出...

    Hibernate笔记 马士兵

    第9课 Hibernate的重点学习:Hibernate的对象关系映射 12 一、对象---关系映射模式 12 二、常用的O/R映射框架: 13 第10课 模拟Hibernate原理(OR模拟) 13 一、 项目名称 13 二、 原代码 13 第11课 Hibernate基础配置...

    Hibernate+中文文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    hibernate3.2中文文档(chm格式)

    7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. ...

    HibernateAPI中文版.chm

    7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. ...

    Hibernate中文详细学习文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    Hibernate 中文 html 帮助文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    最全Hibernate 参考文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    Hibernate教程

    8.2.2. 一对一(one to one) 8.2.3. 一对多(one to many) 8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一...

    hibernate 体系结构与配置 参考文档(html)

    一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many ...

Global site tag (gtag.js) - Google Analytics