关联映射之②:单向1--->1关联

① 基于外键的单向1--->1关联

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

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/temp</property>
        <property name="connection.username">root</property>
        <property name="connection.password">mysqladmin</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <mapping resource="db/mapping/Person.hbm.xml"/>
        <mapping resource="db/mapping/Address.hbm.xml"/>
    </session-factory>

</hibernate-configuration>
Person.java :

public class Person {
	
	private int id;
	private String name;
	private int age;
	private Address address;
	
	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;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	
}
Address.java :

public class Address {
	
	private Integer addressId;
	private String addressDetail;
	
	public Address() {
		
	}
	public Address(String addressDetail) {
		this.addressDetail = addressDetail;
	}
	
	public Integer getAddressId() {
		return addressId;
	}
	public void setAddressId(Integer addressId) {
		this.addressId = addressId;
	}
	public String getAddressDetail() {
		return addressDetail;
	}
	public void setAddressDetail(String addressDetail) {
		this.addressDetail = addressDetail;
	}
	
}
Person.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 package="db.domain">
   <class name="Person" table="persons">
   
    <id name="id" column="person_id">
        <generator class="identity"/>
    </id>
    <property name="name" type="string"/>
    <property name="age" type="integer"/>
    
    <many-to-one name="address" class="Address" unique="true" cascade="all" column="address_id"/>
        
   </class>
</hibernate-mapping>
Address.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 package="db.domain">
   <class name="Address" table="address">
   
    <id name="addressId" column="address_id">
        <generator class="identity"/>
    </id>
    
    <property name="addressDetail" type="string"/>
    
   </class>
</hibernate-mapping>
Test.java :

public class Test {

	public static void main(String[] args) {
		
		Session session=HibernateSessionFactory.getSession();
		Transaction txt=session.beginTransaction();
		
		Person p=new Person();
		p.setName("tom");
		p.setAge(25);
		Address a=new Address("广州天河");
		p.setAddress(a);
		session.persist(p);
		Address a2=new Address("上海虹口");
		p.setAddress(a2);
		
		txt.commit();
		HibernateSessionFactory.closeSession();
	}

}


② 有连接表的单向1--->1关联

Person.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 package="db.domain">
   <class name="Person" table="persons">
   
    <id name="id" column="person_id">
        <generator class="identity"/>
    </id>
    <property name="name" type="string"/>
    <property name="age" type="integer"/>
    
    <join table="person_address">
        <key column="person_id"/>
        <many-to-one name="address" class="Address" unique="true" cascade="all" column="address_id"/>
    </join>
            
   </class>
</hibernate-mapping>
其余代码不变。


③ 基于主键的单向1--->1关联:

Person.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 package="db.domain">
   <class name="Person" table="persons">
   
    <id name="id" column="person_id">
        <generator class="foreign">
            <param name="property">address</param>
        </generator>
    </id>
    <property name="name" type="string"/>
    <property name="age" type="integer"/>
    
    <one-to-one name="address"/>
            
   </class>
</hibernate-mapping>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值