将实体对象中的逻辑组成称为component。在配置文件中,使用component节点对组件进行声明。
例如,一个用户有两个部分组成,姓名和联系方式。姓名和联系方式都可以作为用户的组成部分。
1.表结构如下
use sample;
DROP TABLE t_user;
CREATE TABLE t_user (
id INT NOT NULL AUTO_INCREMENT
, age INT
, firstname VARCHAR(50)
, lastname VARCHAR(50)
, address VARCHAR(200)
, zipcode VARCHAR(10)
, tel VARCHAR(20)
, PRIMARY KEY (id)
);
DROP TABLE t_user;
CREATE TABLE t_user (
id INT NOT NULL AUTO_INCREMENT
, age INT
, firstname VARCHAR(50)
, lastname VARCHAR(50)
, address VARCHAR(200)
, zipcode VARCHAR(10)
, tel VARCHAR(20)
, PRIMARY KEY (id)
);
2.配置文件
TUser.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>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="cn.blogjava.component.TUser" table="t_user" catalog="sample">
<id name="id" type="integer">
<column name="id" />
<generator class="native" />
</id>
<property name="age" type="integer">
<column name="age" />
</property>
<component name="name" class="cn.blogjava.component.Name">
<property name="firstname" type="string">
<column name="firstname" length="50" />
</property>
<property name="lastname" type="string">
<column name="lastname" length="50" />
</property>
</component>
<component name="contact" class="cn.blogjava.component.Contact">
<property name="address" type="string">
<column name="address" length="200" />
</property>
<property name="zipcodes" type="string">
<column name="zipcode" length="10" />
</property>
<property name="tel" type="string">
<column name="tel" length="20" />
</property>
</component>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="cn.blogjava.component.TUser" table="t_user" catalog="sample">
<id name="id" type="integer">
<column name="id" />
<generator class="native" />
</id>
<property name="age" type="integer">
<column name="age" />
</property>
<component name="name" class="cn.blogjava.component.Name">
<property name="firstname" type="string">
<column name="firstname" length="50" />
</property>
<property name="lastname" type="string">
<column name="lastname" length="50" />
</property>
</component>
<component name="contact" class="cn.blogjava.component.Contact">
<property name="address" type="string">
<column name="address" length="200" />
</property>
<property name="zipcodes" type="string">
<column name="zipcode" length="10" />
</property>
<property name="tel" type="string">
<column name="tel" length="20" />
</property>
</component>
</class>
</hibernate-mapping>
将TUser.hbm.xml配置文件加入到hibernate.cfg.xml中去
3.POJO类
Contact.java
package cn.blogjava.component;
import java.io.Serializable;
public class Contact implements Serializable {
private String address;
private String zipcodes;
private String tel;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getZipcodes() {
return zipcodes;
}
public void setZipcodes(String zipcodes) {
this.zipcodes = zipcodes;
}
}
import java.io.Serializable;
public class Contact implements Serializable {
private String address;
private String zipcodes;
private String tel;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getZipcodes() {
return zipcodes;
}
public void setZipcodes(String zipcodes) {
this.zipcodes = zipcodes;
}
}
Name.java
package cn.blogjava.component;
import java.io.Serializable;
public class Name implements Serializable {
private String firstname;
private String lastname;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
import java.io.Serializable;
public class Name implements Serializable {
private String firstname;
private String lastname;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
TUser.java
package cn.blogjava.component;
public class TUser implements java.io.Serializable {
private Integer id;
private Integer age;
private Name name;
private Contact contact;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
}
public class TUser implements java.io.Serializable {
private Integer id;
private Integer age;
private Name name;
private Contact contact;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
}
4.测试类
package cn.blogjava.component;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import junit.framework.TestCase;
import junit.framework.Assert;
public class HibernateTest extends TestCase {
Session session = null;
protected void setUp(){
try {
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
// delete();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
protected void tearDown() {
try {
session.close();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void delete(){
Transaction tran = null;
String hql = " delete TUser";
try {
tran = session.beginTransaction();
Query query = session.createQuery(hql);
int ret = query.executeUpdate();
System.out.println("Delete records => " + ret);
tran.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
if(tran != null) {
try {
tran.rollback();
} catch(HibernateException e2) {
e2.printStackTrace();
}
}
}
}
public void testInsert(){
Transaction tran = null;
try {
System.out.println("session is " + session);
tran = session.beginTransaction();
//user1
TUser user = new TUser();
user.setAge(25);
Name name = new Name();
name.setFirstname("yu");
name.setLastname("yy");
user.setName(name);
Contact contact = new Contact();
contact.setAddress("dalian");
contact.setTel("42689334");
contact.setZipcodes("116023");
user.setContact(contact);
session.save(user);
//user2
TUser user2 = new TUser();
user2.setAge(26);
Name name2 = new Name();
name2.setFirstname("bai");
name2.setLastname("yf");
user2.setName(name2);
Contact contact2 = new Contact();
contact2.setAddress("beijing");
contact2.setTel("12345678");
contact2.setZipcodes("100010");
user2.setContact(contact2);
session.save(user2);
session.flush();
tran.commit();
Assert.assertEquals(user.getId().intValue() > 0, true);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
if(tran != null) {
try {
tran.rollback();
} catch(HibernateException e2) {
e2.printStackTrace();
}
}
}
}
public void testSelect(){
String hql = " from TUser where age=25 ";
try {
List userList = session.createQuery(hql).list();
TUser user = (TUser)userList.get(0);
Contact contact = user.getContact();
Assert.assertEquals(contact.getAddress(), "dalian");
Assert.assertEquals(user.getName().getFirstname(), "yu");
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
}
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import junit.framework.TestCase;
import junit.framework.Assert;
public class HibernateTest extends TestCase {
Session session = null;
protected void setUp(){
try {
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
// delete();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
protected void tearDown() {
try {
session.close();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void delete(){
Transaction tran = null;
String hql = " delete TUser";
try {
tran = session.beginTransaction();
Query query = session.createQuery(hql);
int ret = query.executeUpdate();
System.out.println("Delete records => " + ret);
tran.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
if(tran != null) {
try {
tran.rollback();
} catch(HibernateException e2) {
e2.printStackTrace();
}
}
}
}
public void testInsert(){
Transaction tran = null;
try {
System.out.println("session is " + session);
tran = session.beginTransaction();
//user1
TUser user = new TUser();
user.setAge(25);
Name name = new Name();
name.setFirstname("yu");
name.setLastname("yy");
user.setName(name);
Contact contact = new Contact();
contact.setAddress("dalian");
contact.setTel("42689334");
contact.setZipcodes("116023");
user.setContact(contact);
session.save(user);
//user2
TUser user2 = new TUser();
user2.setAge(26);
Name name2 = new Name();
name2.setFirstname("bai");
name2.setLastname("yf");
user2.setName(name2);
Contact contact2 = new Contact();
contact2.setAddress("beijing");
contact2.setTel("12345678");
contact2.setZipcodes("100010");
user2.setContact(contact2);
session.save(user2);
session.flush();
tran.commit();
Assert.assertEquals(user.getId().intValue() > 0, true);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
if(tran != null) {
try {
tran.rollback();
} catch(HibernateException e2) {
e2.printStackTrace();
}
}
}
}
public void testSelect(){
String hql = " from TUser where age=25 ";
try {
List userList = session.createQuery(hql).list();
TUser user = (TUser)userList.get(0);
Contact contact = user.getContact();
Assert.assertEquals(contact.getAddress(), "dalian");
Assert.assertEquals(user.getName().getFirstname(), "yu");
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
}
发表评论
-
hibernate 继承映射
2010-05-28 21:11 747一. 继承有三种策略 1. 每个分层结构一张表 ... -
Hibernate的主键生成机制
2010-05-26 16:37 954配置Hibernate的主键生成 ... -
hibernate 配置模板
2010-05-26 14:23 763<?xml version='1.0' encoding ... -
hibernate的hibernate.hbm2ddl.auto属性
2010-05-26 13:43 1140<property name=&qu ... -
hibernate 数据类型和java,db数据类型
2010-05-26 12:37 986Java数据类型,Hibernate数据类型,标准sql数据类 ... -
hiberante derive property
2010-05-22 14:47 821衍生属性(derive propertie)是一个特别强大 ... -
hibernate typename
2010-05-22 14:44 694typename 可以是如下几种: ... -
hibernate subselect
2010-05-21 22:41 1746:它将一个不可变(immutable)并且只读的实体映射到一个 ... -
hibernate where
2010-05-21 22:20 1410(可选)指定一个附加的 SQL WHERE 条件,在抓 ... -
hibernate select-before-update
2010-05-21 22:19 4183(可选,默认为 false ):指定 Hibernate ... -
hibernate dynamic-update(转)
2010-05-21 22:13 1355今天在update记录的时候遇到一个问题 我从数据库里 ... -
Hibernate"discriminator-value"用法(转)
2010-05-21 22:01 6064可能经常遇到这样的情况: 在数据库表中 ... -
HQL
2010-05-19 20:46 801select p from People p left joi ...
相关推荐
03 03Hibernate_Component : 实体细粒度的划分,数据库中的一张表在程序中划分为几个部分的实体,配置文件和增、删、改、查的使用。 04 04Hibernate_Composite : 复合主键的使用,在开发中很少用到,一般良好的设计...
`hb_02_component`和`hb_03_componentOid`、`hb_03_componentCollection`则涉及到了Hibernate的组件映射(Component Mapping)和复合主键(Composite Key)。组件映射允许将一个类的属性组合成另一个类的一部分,而...
在给出的文件名列表中,`cmp`可能是压缩包中某个文件或目录的简写,具体含义可能与JEECG的组件(Component)有关,比如可能是某个特定模块的代码或者配置文件。 总的来说,JEECG结合Hibernate,提供了一种高效、...
在Java开发领域,尤其是使用Spring或Hibernate框架时,“Component(组件映射)”是一个重要的概念。Component映射是Hibernate提供的一种机制,它允许我们将多个相关的简单属性或者对象封装到一个复合对象中,以此来...
`hibernate_component_mapping`文件可能讨论了Hibernate的组件映射。组件是一组相关的属性,它们可以作为一个整体被映射到数据库的一个或多个字段上。组件映射提供了一种有效的方式,将复杂对象分解为更小的部分,...
在Hibernate中,组件(Component)是一种特殊的关联关系,它将一个类的实例嵌入到另一个类中,就像Java中的内部类一样。组件可以是任何复杂的对象结构,如地址、时间戳等,它们作为主对象的一部分存储在数据库中。...
5. **Hibernate_Component**:这部分可能包含了Hibernate组件映射的示例。组件是对象的一部分,可以被嵌入到其他对象中,类似于数据库中的复合类型。通过@Component或@Embeddable注解,可以将一个类声明为组件,并在...
在IT行业中,SpringMVC、Hibernate和定时器是三个非常重要的技术组件,它们分别负责不同的功能领域。SpringMVC是Spring框架的一个模块,用于构建Web应用程序的Model-View-Controller(MVC)架构;Hibernate是一个...
`hibernate-mapping-4.0.xsd`包含关于如何定义实体、关联(Association)、组件(Component)、集合(Collection)等的规则,而`hibernate-configuration-4.0.xsd`则定义了配置Hibernate会话工厂、缓存策略、事件...
例如,教程中提到的版本为 `jbosstools-3.2.1.Final.component--hibernatetools-Update-2011-08-01_12-53-33-H1305.zip`。 - **安装步骤**:下载后的压缩文件需解压并放入 Eclipse 的 `dropins` 目录下,并且需要...
组件(Component)是一个被包含的对象,在持久化的过程中,它被当作值类型,而并非一个实体的引用。在这篇文档中,组件这一术语指的是面向对象的合成概念(而并不是系统构架层次上的组件的概念)。举个例子, 你对人...
2. 注解驱动开发:使用@Component、@Service、@Repository和@Controller等注解标记bean。 3. 配置事务管理:使用@Transactional注解开启事务支持,配置事务管理器。 4. 整合其他框架:如整合Hibernate,需要配置...
5.1.13. 组件(component), 动态组件(dynamic-component) 5.1.14. properties 5.1.15. 子类(subclass) 5.1.16. 连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键...
Hibernate支持三种持久化模型:实体(Entity)、值对象(Value Object)和组件(Component)。实体是与数据库表对应的Java对象;值对象用于封装业务逻辑中的数据;组件则用来表示一个对象的组成部分。 5. **级联...
这个Demo可能包含了Spring和Hibernate的XML配置文件,也可能涉及到@Component、@Service、@Repository和@Controller等注解的使用。 10. **Maven或Gradle构建**:项目可能使用Maven或Gradle进行依赖管理和构建,通过...
8. **配置注解驱动**:在`dispatcher-servlet.xml`中启用Spring MVC的注解驱动,配置`<context:component-scan>`扫描带有注解的bean,并设置`<mvc:annotation-driven>`以支持HTTP方法映射和数据绑定。 9. **测试与...