Hibernate一对一关联(共享主键)

一对一关联,在两个表中都由主键或外键关联另一个表中的一条记录。

比如一个人用户表,包含的信息很多,而在使用的时候,很多字段的信息使用并不是很频繁,这种情况下,不常使用的字段就可以单独成一张表,主要还是为了提高访问数据库的性能!

package com.pojo; public class User implements java.io.Serializable { private static final long serialVersionUID = 3877915998065689256L; private Long id; private String username; private String password; //一对一关联 private Profile profile; public Profile getProfile() { return profile; } public void setProfile(Profile profile) { this.profile = profile; } public User() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } }

package com.pojo; public class Profile implements java.io.Serializable { private static final long serialVersionUID = -9070015557361756382L; private Long id; private String email; private String phone; private String address; //关联User private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Profile() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return this.phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } }

一对一的关联配置相对来说比较简单的,主要还是得注意主键的生成

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.pojo.User" table="USERS" schema="ZM"> <id name="id" type="java.lang.Long"> <column name="ID" precision="4" scale="0" /> <generator class="increment"></generator> </id> <property name="username" type="java.lang.String"> <column name="USERNAME" length="100" /> </property> <property name="password" type="java.lang.String"> <column name="PASSWORD" length="100" /> </property> <!-- 一对一关联 --> <one-to-one name="profile" class="com.pojo.Profile"></one-to-one> </class> </hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.pojo.Profile" table="PROFILE" schema="ZM"> <id name="id" type="java.lang.Long"> <column name="ID" precision="4" scale="0" /> <!-- 主键外键生成 --> <generator class="foreign"> <param name="property">user</param> </generator> </id> <property name="email" type="java.lang.String"> <column name="EMAIL" length="100" /> </property> <property name="phone" type="java.lang.String"> <column name="PHONE" length="100" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" length="100" /> </property> <one-to-one name="user" class="com.pojo.User"></one-to-one> </class> </hibernate-mapping>

就这样,一对一关联就配置完成,下面可以来测试:

package com.test; import org.hibernate.Session; import com.pojo.Profile; import com.pojo.User; import com.util.HibernateManager; public class Test2 { /** * beckham * Dec 15, 2009 2:24:21 PM */ public static void main(String[] args) { Test2.load() ; } public static void add(){ Session session = HibernateManager.openSession() ; User u = new User() ; u.setUsername("张三") ; u.setPassword("abcdefg") ; Profile p = new Profile() ; p.setAddress("西里小区"); p.setEmail("gao@163.com") ; p.setPhone("111111") ; p.setUser(u) ; u.setProfile(p) ; try { session.save(p); session.save(u) ; HibernateManager.closeSession() ; } catch (Exception e) { HibernateManager.rollbackTransaction() ; } } public static void load(){ Session session = HibernateManager.openSession() ; long a = new Long(1) ; User user = (User)session.load(User.class,a) ; System.out.println(""+user.getUsername()); System.out.println(""+user.getPassword()); System.out.println(""+user.getId()); System.out.println(""+user.getProfile().getId()); System.out.println(""+user.getProfile().getAddress()); System.out.println(""+user.getProfile().getEmail()); System.out.println(""+user.getProfile().getPhone()); try { HibernateManager.closeSession() ; } catch (Exception e) { HibernateManager.rollbackTransaction() ; } } }

测试结果:

Hibernate:
select
user0_.ID as ID3_2_,
user0_.USERNAME as USERNAME3_2_,
user0_.PASSWORD as PASSWORD3_2_,
profile1_.ID as ID2_0_,
profile1_.EMAIL as EMAIL2_0_,
profile1_.PHONE as PHONE2_0_,
profile1_.ADDRESS as ADDRESS2_0_,
user2_.ID as ID3_1_,
user2_.USERNAME as USERNAME3_1_,
user2_.PASSWORD as PASSWORD3_1_
from
ZM.USERS user0_
left outer join
ZM.PROFILE profile1_
on user0_.ID=profile1_.ID
left outer join
ZM.USERS user2_
on profile1_.ID=user2_.ID
where
user0_.ID=?
张三
abcdefg
1
1
西里小区
gao@163.com
111111

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值