现象:

多个实体的属性整合到一张表


代码片段:

User.java:

package com.shuoeasy.test;

public class User {
	private int id;
	private String userName;
	
	// 组件映射,多个实体的属性整合到一张表
	private UserDetail userDetail;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public UserDetail getUserDetail() {
		return userDetail;
	}
	public void setUserDetail(UserDetail userDetail) {
		this.userDetail = userDetail;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", userDetail=" + userDetail + "]";
	}
 
	
}

UserDetail.java:

package com.shuoeasy.test;

public class UserDetail {
	/**
	 * 不需要定义id
	 */
	private String email;
	private String phone;
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
}

User.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">
<!-- Generated 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
	<class name="com.shuoeasy.test.User" table="USER">
		<id name="id" type="int">
			<column name="ID" />
			<!-- 指定主键的生成方式,native:使用数据库本地方式 -->
			<generator class="native" />
		</id>
		<property name="userName" type="java.lang.String">
			<column name="USERNAME" />
		</property>
		<!-- 组件映射 -->
		 <component name="userDetail" class="com.shuoeasy.test.UserDetail">
			<property name="email" column="EMAIL"></property>
			<property name="phone" column="PHONE"></property>
		</component> 
	</class>
</hibernate-mapping>

AppTest.java:

package com.shuoeasy.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


/**
 * Unit test for simple App.
 */
public class AppTest {
	Session session;
	SessionFactory sf;

	@Before
	public void init() {

		Configuration conf = new Configuration().configure();

		ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();

		sf = conf.buildSessionFactory((org.hibernate.service.ServiceRegistry) sr);

		session = sf.openSession();

		session.beginTransaction();
		System.out.println("init");
	}

	@After
	public void destory() {
		session.getTransaction().commit();

		session.close();

		sf.close();
		System.out.println("dectory");
	}

	@Test
	public void testInsert() {
		User user = new User();
		user.setUserName("张三");
		
		UserDetail userDetail = new UserDetail();
		userDetail.setEmail("a@123abc.com");
		userDetail.setPhone("12345678901");
		user.setUserDetail(userDetail);
		session.save(user);
		
	}
	
	@Test
	public void test() {
		User user = (User) session.get(User.class, 1);
		System.out.println(user);
	}
	
}

数据库的表结构

blob.png

你可能感兴趣的文章