一对一双向外键关联(xml方式)

二。xml方式
编写具有对应属性的类
package org.hibernate.tutorial.domain;


public class StudentIdCard {
	private int id;
	private String num;
	private Student student ;
	
	
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	
}



package org.hibernate.tutorial.domain;

public class Student {
	private int id;
	private String name;
	private int age;
	private StudentIdCard studentIdCard;
	
	
	public StudentIdCard getStudentIdCard() {
		return studentIdCard;
	}
	public void setStudentIdCard(StudentIdCard studentIdCard) {
		this.studentIdCard = studentIdCard;
	}
	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;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}



StudentIdCard.hbm.xml中对应StudentIdCard类的student属性一对一方式
 
<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping package="org.hibernate.tutorial.domain"> 

    <class name="StudentIdCard" table="StudentIdCard"> 
        <id name="id" column="StudentIdCard_ID"> 
            <generator class="native"/> 
        </id> 
        <property name="num" type="java.lang.String" column="num"></property>
		<many-to-one name="student"  unique="true"></many-to-one>
    </class> 

</hibernate-mapping> 


Student.hbm.xml中设置Student类中studentIdCard属性包含的student属性对应关系,相当于mappedby
 
<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping package="org.hibernate.tutorial.domain"> 

    <class name="Student" table="Student"> 
        <id name="id" column="student_ID"> 
            <generator class="native"/> 
        </id> 
        <property name="name"></property>
        <property name="age"></property>
		<one-to-one name="studentIdCard" property-ref="student"></one-to-one>
    </class> 

</hibernate-mapping> 



添加映射
 
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/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/test?useUnicode=true&amp;characterEncoding=utf8</property>
        
        <property name="connection.username">root</property>
        <property name="connection.password">admin</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.internal.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">update</property>-->

		
		<mapping resource="org/hibernate/tutorial/domain/Student.hbm.xml"/> 
		<mapping resource="org/hibernate/tutorial/domain/StudentIdCard.hbm.xml"/>
		
	
    </session-factory>
    
</hibernate-configuration>

猜你喜欢

转载自javafu.iteye.com/blog/2015144
今日推荐