hibernate组建映射

当一个用户表中字段比较多,而有些字段是不常用,则可以用组件映射

基本信息:

private Long id; private String username; private String password; private UserMessage message ;

组件信息 ,UserMessage

private String address; private String email; private String phone; private String postcode; private Userinfo info ;

private Userinfo info 这里是为了配置双向关联

<hibernate-mapping> <class name="com.pojo.Userinfo" table="USERINFO" schema="ZM"> <id name="id" type="java.lang.Long"> <column name="ID" precision="10" scale="0" /> <generator class="increment" /> </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> <component name="message" class="com.pojo.UserMessage"> <parent name="info" /> <property name="address" type="java.lang.String"> <column name="ADDRESS" length="100" /> </property> <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="postcode" type="java.lang.String"> <column name="POSTCODE" length="100" /> </property> </component> </class> </hibernate-mapping>

组件映射用到了 <component name="message" class="com.pojo.UserMessage">

配置双向关联 : <parent name="info" />

测试类:

public static void main(String[] args) { Userinfo info = new Userinfo() ; info.setId(new Long(1)) ; info.setUsername("zhangsan") ; info.setPassword("111111") ; UserMessage message = new UserMessage() ; message.setAddress("西里小区") ; message.setEmail("gaown@163.com") ; message.setPhone("123456") ; message.setPostcode("100082") ; info.setMessage(message) ; message.setInfo(info) ; HibernateTestcompent.add(info) ; HibernateTestcompent.load(message) ; } public static void add(Userinfo userinfo) { session = HibernateManager.openSession(); try { session.save(userinfo); HibernateManager.closeSession(); } catch (Exception e) { HibernateManager.rollbackTransaction(); } } @SuppressWarnings("unchecked") public static void load() { session = HibernateManager.openSession(); try { Userinfo user = (Userinfo) session.get(Userinfo.class, new Long(1)); System.out.println(user.getId()); System.out.println(user.getUsername()); System.out.println(user.getPassword()); System.out.println(user.getMessage().getAddress()); System.out.println(user.getMessage().getEmail()); System.out.println(user.getMessage().getPhone()); System.out.println(user.getMessage().getPostcode()); HibernateManager.closeSession(); } catch (Exception e) { e.printStackTrace(); HibernateManager.rollbackTransaction(); } } public static void load(UserMessage message) { Userinfo info = message.getInfo(); System.out.println(info.getId()); System.out.println(info.getUsername()); System.out.println(info.getPassword()); System.out.println(message.getAddress()); System.out.println(message.getEmail()); System.out.println(message.getPhone()); System.out.println(message.getPostcode()); }

测试结果:

Hibernate:
select
max(ID)
from
USERINFO
Hibernate:
insert
into
ZM.USERINFO
(USERNAME, PASSWORD, ADDRESS, EMAIL, PHONE, POSTCODE, ID)
values
(?, ?, ?, ?, ?, ?, ?)
3
zhangsan
111111
西里小区
gaown@163.com
123456
100082

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值