15. component映射
来源:互联网 发布:java连接oracle数据库 编辑:程序博客网 时间:2024/06/05 11:22
componenet映射:
*在hibernate中,component是某个实体的逻辑组成部分,它与实体的根本区别是没有oid,component通常被称为是值对象(DDD)
*采用component映射的好处:它实现了对象模型的细粒度划分,层次会更分明,复用率会更高
1.定义Contact类
package com.bjsxt.hibernate;
public class Contact {
private String email;
private String address;
private String zipCode;
private String contactTel;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getContactTel() {
return contactTel;
}
public void setContactTel(String contactTel) {
this.contactTel = contactTel;
}
}
2.定义User类
package com.bjsxt.hibernate;
public class User {
private int id;
private String name;
private Contact contact;
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 Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
}
3.定义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">
<hibernate-mapping>
<class name="com.bjsxt.hibernate.User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<component name="contact">
<property name="email"/>
<property name="address"/>
<property name="zipCode"/>
<property name="contactTel"/>
</component>
</class>
</hibernate-mapping>
4.定义hibernate.cfg.xml文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_component_mapping</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">bjsxt</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5.定义ExportDB.java和HibernateUtils.java(略)
6.编写测试用例
package com.bjsxt.hibernate;
import org.hibernate.Session;
import junit.framework.TestCase;
public class ComponentMappingTest extends TestCase {
public void testSave1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
User user = new User();
user.setName("张三");
Contact contact = new Contact();
contact.setAddress("xxxxx");
contact.setEmail("xxx@rrr.com");
contact.setZipCode("1111111");
contact.setContactTel("9999999999");
user.setContact(contact);
session.save(user);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
表结构:
t_user表包含id,name,email,address,zipcode,contacttel六个字段
- 15. component映射
- Component映射
- component的集合映射
- 组件(Component)映射
- hibernate XML component 映射
- 4.Component映射:测试
- 5.Component映射:总结
- 组件(Component)映射
- Hibernate之component映射
- Hibernate中component映射
- 【Hibernate】Component映射
- 组件(Component)映射
- Hibernate入门10 - Component 映射
- 入門 10 - Component 映射
- Hibernate入门10 - Component 映射
- 入門 10 - Component 映射
- Hibernate入门之Component 映射
- 1.Component映射:Object,hbm
- 《Windows核心编程》笔记——进程
- Cocos2D开发的iPhone游戏的教程
- textChange跳出提示框
- VS2008安装WTL8.1创建Mobile WTL项目失败的解决方法
- 本文介绍两种方法,让大家在32位Win7中也能尽享大内存带来的爽快感觉!
- 15. component映射
- Android widget——AutoCompleteTextView学习笔记
- JAVA值传递(call by value)
- Jsp显示oracle的blob数据
- 常见的内存错误
- SQL时间函数详细说明
- Javascript中对象的基本用法
- String literal is not properly closed by a double-quote错误例子
- DDR的基本原理