`

036_一对一单向外键关联_xml

 
阅读更多

星期日, 一月 10, 2016   16:22

在xml中这种单向的关联该怎么配置

 

调试了特么久,结果正确了。

 

代码案例:

使用student   stuidcard进行验证一对一的单向关联

 

1.先建一个student类

2.对应的xml文件进行配置

3.再建一个stuidcard类(里面包含一个student类的引用)

4.对应的xml文件进行配置

5.在hibernate.cfg.xml进行指明student类和stuidcard类的位置

6.进行写测试类

7.运行结果

 

 

 

 

具体的代码案例:

student类

package com.zhuhw.hibernate.model;

public class Student {
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

 

student.hbm.xml文件

/hibernate_0700_One2One_unic_fk/src/com/zhuhw/hibernate/model/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">
<!-- 找不到entity,是因为这个类没改包名 -->
<hibernate-mapping package="com.zhuhw.hibernate.model">
	<class name="Student" table="student" >
		<id name="id" >
		 <generator class="native"></generator>
		</id>
		<property name="name" />
	</class>
</hibernate-mapping>

 

 

/hibernate_0700_One2One_unic_fk/src/com/zhuhw/hibernate/model/StuIdCard.java

 

package com.zhuhw.hibernate.model;

public class StuIdCard{
	private int id;
	private String name;
    private Student student;
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}

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

 

 

 

/hibernate_0700_One2One_unic_fk/src/com/zhuhw/hibernate/model/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">
<!-- 找不到entity,是因为这个类没改包名 -->
<hibernate-mapping package="com.zhuhw.hibernate.model">
	<class name="StuIdCard" table="StuIdCard" >
		<id name="id" >
		 <generator class="native"></generator>
		</id>
		<property name="name" />
		<many-to-one name="Student" column="studentId" unique="true"></many-to-one>
	</class>
</hibernate-mapping> 

这个一对一的关系的映射有一个需要注意的点,注意name的值
<!-- name指的是 StuIdCard中的student中的一个引用,与这个引用是一样的值-->
		<many-to-one name="student" column="studentId" unique="true"></many-to-one>

 

 

 

/hibernate_0700_One2One_unic_fk/src/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">root</property>

        
        <!--
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">scoot</property>
        <property name="connection.password">tiger</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>
        <property name="format_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>
      	<!-- hibernate去哪里找这个配置文件 -->
        <mapping resource="com/zhuhw/hibernate/model/Student.hbm.xml"/>
        <mapping resource="com/zhuhw/hibernate/model/StuIdCard.hbm.xml"/><!--
        
	    <mapping class="com.zhuhw.hibernate.model.Husband"/>
		<mapping class="com.zhuhw.hibernate.model.Wife"/>	    
    --></session-factory>

</hibernate-configuration>

 

 

测试类

/hibernate_0700_One2One_unic_fk/test/com/zhuhw/hibernate/model/HibernateORMappingTest.java

 

package com.zhuhw.hibernate.model;

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 {
	
	public static SessionFactory sf = null;
	@BeforeClass
	public  static void beforeClass(){
			sf = new AnnotationConfiguration().configure().buildSessionFactory();
	}
	
	@Test
	public void testOne2One(){
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
	}
	@AfterClass
	public static void afterClass(){
		sf.close();
	}
	
	
	
}

 

 

运行结果:

     console结果:

 

  

create table StuIdCard (
        id integer not null auto_increment,
        name varchar(255),
        studentId integer unique,
        primary key (id)
    )
16:19:55,274 DEBUG SchemaExport:377 - 
    create table student (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )
16:19:55,352 DEBUG SchemaExport:377 - 
    alter table StuIdCard 
        add index FKD3A449FF7576FD0B (studentId), 
        add constraint FKD3A449FF7576FD0B 
        foreign key (studentId) 
        references student (id)

 

 

此例子完成

0
0
分享到:
评论

相关推荐

    Hibernate一对一单向外键关联(annotation/xml)

    NULL 博文链接:https://cdxs2.iteye.com/blog/1930748

    Hibernate注释大全收藏

    Hibernate注释大全收藏 声明实体Bean @Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id; } public void setId(Long id) { this.id...一对一 使用 @OneToOne...

    jdbc基础和参考

    基于外键的一对一 Wife Husband id id name name h_id references Husband(id) unique 基于主键的一对一 Wife Husband id references Husband(id) id name name create table Husband( id number ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    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. ...

    Hibernate3的帮助文档

    8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ...

    hibernate 框架详解

    一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多...

    hibernate3.04中文文档.chm

    8.4.2. 一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.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中文详细学习文档

    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+中文文档

    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)

    一对一(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 ...

    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. ...

    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. ...

    最全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+中文参考文档

    7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向关联(Bidirectional associations) 7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的...

    Hibernate使用技巧汇总

    property-ref:关联类中用于与主控类相关联的属性名,默认为关联类的主键属性名 单向一对多需在一方配置,双向一对多需在双方进行配置 8.lazy=false:被动方的记录由hibernate负责记取,之后存放在主控...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part4

     16.4 多对一和一对一关联的检索策略  16.4.1 迫切左外连接检索(fetch属性为“join”)  16.4.2 延迟检索(lazy属性为默认值“proxy”)  16.4.3 无代理延迟检索(lazy属性为“no-proxy”)  16.4.4 立即检索...

Global site tag (gtag.js) - Google Analytics