`
yaasshole
  • 浏览: 664630 次
文章分类
社区版块
存档分类
最新评论

Hibernate高级映射 --- 组件映射

 
阅读更多

Hibernate高级映射 --- 组件映射

一 概念

1.组件映射概念

Hibernate建议在进行领域模型建模时要细粒度一些,简单的说,就是类要比表多,这种映射就是组件映射

组件类是值类型的,即它没有对象标识符属性,在数据库中也没有对应的表,它只隶书于另一个持久化类的实例。它的数据被嵌入到所隶书的持久化实例对应的数据库表的记录中。

2.组件映射的应用有三种常见的情况:

(1)把组件类作为持久化类的单个属性来使用

(2)把组件类作为持久化类的集合属性来使用

(3)把组件类作为持久化类的对象标识符来使用

3.组件类的作用

如果在User的实体类中有一个Name实体类,如果要想把Name的实体类放到User的表中,成为一张表,要用到组件映射。

二 代码分析 以User和Name为例

第一种:把组件类作为持久化类的单个属性来使用

1.建Name表 用来设置Name类中的属性

package com.hbsi.domain;

public class Name {

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;

}

}

2.建User表

package com.hbsi.domain;

import java.util.Date;

public class User {

private int id;

private Name name;//实体类

private Date birthday;

public User() {

super();

// TODO Auto-generatedconstructor stub

}

public User(int id, Namename, Date birthday) {

super();

this.id = id;

this.name = name;

this.birthday = birthday;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public NamegetName() {

return name;

}

public void setName(Name name) {

this.name = name;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

}

3.映射文件中增加Name类的组件映射

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hbsi.domain">

<class name="User" table="user">

<id name="id" column="id">

<generator class="native"/>

</id>

<!-- 组件映射 ,在User中有一个Name类 -->

<component name="name">

<property name="firstName"column="first_name"></property>

<property name="lastName"column="last_name"></property>

</component>

<property name="birthday" />

</class>

</hibernate-mapping>

注意:所用的标签是<component></component>

第二种:把组件类作为持久化类的集合属性来使用

1. 创建Photo实例类 为组件类的集合属性

package com.hbsi.domain;

public class Photo {

private String name;

private String filePath;

private String description;

public Photo() {

super();

// TODO Auto-generatedconstructor stub

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getFilePath() {

return filePath;

}

public void setFilePath(String filePath) {

this.filePath = filePath;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

}

2. 创建Album类

package com.hbsi.domain;

import java.util.List;

public class Album {

private Integer id;

private String title;

private String description;

private List<Photo> photos;

public Album() {

super();

// TODO Auto-generatedconstructor stub

}

public IntegergetId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

public List<Photo> getPhotos() {

return photos;

}

public void setPhotos(List<Photo> photos) {

this.photos = photos;

}

}

3.Album的映射文件

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hbsi.domain">

<class name="Album" table="album">

<id name="id"column="id">

<generator class="native"/>

</id>

<property name="title"/>

<property name="description"/>

<!-- 用bag来映射list集合 -->

<bag name="photos"table="photo">

<key column="album_id"not-null="true"></key>

<!-- composite合成的意思 即为合成的元素 -->

<composite-element class="Photo">

<property name="name"></property>

<property name="filePath"></property>

<property name="description"></property>

</composite-element>

</bag>

</class>

</hibernate-mapping>

4.持久化类

package com.hbsi.test;

import java.util.ArrayList;

import java.util.List;

import org.hibernate.Session;

import org.hibernate.Transaction;

import com.hbsi.domain.Album;

importcom.hbsi.domain.Photo;

import com.hbsi.hibernate.utils.HibernateUtil;

public class PhotoAlbum {

publicstatic void main(String[] args) {

add();

}

staticvoid add() {

Session session = null;

Transaction transaction = null;

try {

session= HibernateUtil.getSession();

transaction= session.beginTransaction();

Albumalbum = new Album();

album.setTitle("熊熊的亮相");

album.setDescription("老王就是熊熊");

Photophoto = new Photo();

photo.setName("熊熊纪念册");

photo.setFilePath("老王宾馆");

photo.setDescription("熊熊是非常可爱的");

List<Photo>photos = new ArrayList<Photo>();

photos.add(photo);

album.setPhotos(photos);

session.save(album);

transaction.commit();

}finally {

if(session != null) {

session.close();

}

}

}

}

非常提醒:在测试的时候,实例化Album类和Photo类,在插入数据库的时候只插入album即可,而对于photo根本不用保存,因为photo只属于Album类中的一个属性类,没有id等约束,它它只属于一个类的分支,只能通过Album去控制,而不可以单独拿photo来操作,这个问题纠结了我半天啊!


分享到:
评论
1 楼 yulanlian 2012-08-06  
QQ:17903425,能加一下我的QQ请教您点问题么?

相关推荐

    hibernate组件映射

    自己学习hibernate的心得。这个文档很不错哦,很不错,很不错

    Hibernate教程09_关系映射之组件映射

    http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码

    Hibernate组件映射代码详解

    主要介绍了Hibernate组件映射代码详解,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下

    hibernate-orm-3.6

    Hibernate ORM是为应用程序和其他组件/库提供对象/关系映射(ORM)支持的组件/库。 它还提供了JPA规范的实现,该规范是ORM的标准化Java规范。 有关其他信息,请参见 。快速开始 git clone git://github....

    Hibernate组件映射(annotation/xml)

    NULL 博文链接:https://cdxs2.iteye.com/blog/1932569

    Hibernate 3.6.0.Final Reference PDF 手册

    第 9 章 组件(Component)映射 第 10 章 继承映射(Inheritance Mapping) 第 11 章 与对象共事 第 12 章 Read-only entities 第 13 章 事务和并发 第 14 章 拦截器与事件(Interceptors and ...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     14.6 映射组件类型集合  14.7 小结  14.8 思考题 第15章 映射实体关联关系  15.1 映射一对一关联  15.1.1 按照外键映射   15.1.2 按照主键映射  15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  ...

    hibernate学习笔记

    组件component映射(hibernate_component) 27 复合(联合)主键映射(hibernate_composite) 27 集合(collection)映像 (hibernate_collection) 28 Hibernate 对数据库的并发支持 30 悲观锁(hibernate_pessimistic...

    Hibernate教程17_继承映射

    http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码

    Hibernate学习笔记

    022 component(组件)关联映射 023 复合主键 关联映射 024 其它 关联映射 025 hibernate 悲观锁、乐观锁 026 hibernate 操作树形结构 027 hibernate 查询语言(HQL) 028 hibernate 缓存(性能优化策略) 029 hibernate ...

    J2EE三大框架_笔记_a

    9-Struts高级部分(1)(解决重复提交、上传组件)笔记 10-Struts高级部分(2)(常用Action、验证框架、动态Form)笔记 J2EE框架_笔记_b: 11-留言管理程序_使用Struts + DAO完成笔记 12-Struts + DAO分页笔记 16-...

    J2EE框架_笔记_c

    9-Struts高级部分(1)(解决重复提交、上传组件)笔记 10-Struts高级部分(2)(常用Action、验证框架、动态Form)笔记 J2EE框架_笔记_b: 11-留言管理程序_使用Struts + DAO完成笔记 12-Struts + DAO分页笔记 16-...

    J2EE框架_笔记_b

    9-Struts高级部分(1)(解决重复提交、上传组件)笔记 10-Struts高级部分(2)(常用Action、验证框架、动态Form)笔记 J2EE框架_笔记_b: 11-留言管理程序_使用Struts + DAO完成笔记 12-Struts + DAO分页笔记 16-...

    hibernate annotation 中文文档

    嵌入式对象(又名组件) 2.2.2.4. 无注解之属性的默认值 2.2.. 映射主键属性 2.2.4. 映射继承关系 2.2.4.1. 每个类一张表 2.2.4.2. 每个类层次结构一张表 2.2.4.3. 连接的子类 2.2.4.4. 从父类继承的属性 2.2.5. 映射...

    HibernateAPI中文版.chm

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    hibernate3.2中文文档(chm格式)

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    hibernate

    hibernate实体层设计+hibernate实体映射+hibernate复合组件

    Hibernate+中文文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    对象关系映射,并持久化到数据库的开源组件

    Hibernate 是一种OR/M(object relation mapping)对象关系映射工具,是一个可以自动的根据xml 完成 对象关系映射,并持久化到数据库的开源组件,是对JDBC 的封装,主要负责java 对象的持久化.。 对象序列化适应临时...

    Hibernate PPT

    介绍HIBERNATE高级特性,组件映射,继承映射,事务和并发,拦截器,批量处理等.

Global site tag (gtag.js) - Google Analytics