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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值