- 浏览: 240701 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
akka_li:
学习了!但是为什么后续的没有了?!
EJB4:RMI和RMI-IIOP -
springaop_springmvc:
apache lucene开源框架demo使用实例教程源代码下 ...
Lucene学习笔记(一)Lucene入门实例 -
qepipnu:
求solr 客户端 jar包
Solr学习笔记(三)Solr客户端开发实例 -
zhangbc:
是这问题,赞!
Oracle Start Up 2 Oracle 框架构件、启动、解决一个问题 -
feilian09:
查询 select hibernate jdbc 那个效率快
Hibernate,JDBC性能探讨
本文记录Hibernate关联映射中的:组件映射、多对一单向、一对多单向、多对多单向、一对多或多对一双向、多对多双向。所以本文分为6个部分,每部分包括.1 简单说明、.2 Annotation配置及代码明细、.3 结果分析及相关测试、.4 XML配置下的XML配置。
1 组件映射
1.1 如下图所示
上图可以表示Laptop是Person的一个组件,这种情况下,我们可以这样设计,代码如下
1.2 Annotation配置及代码
package com.kylin.test; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_person") public class Person { private int id; private String name; private Laptop laptop; @Id @GeneratedValue 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; } @Embedded public Laptop getLaptop() { return laptop; } public void setLaptop(Laptop laptop) { this.laptop = laptop; } }
package com.kylin.test; public class Laptop { private String brand; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } }
如上就为全部配置代码,下面为结果及测试
1.3 结果及测试
先给出测试代码。以后2.3等久不给出,因为大同小异,测试代码:
package com.kylin.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class ComponentTest { private static SessionFactory sessionFactory; @BeforeClass public static void beforeClass() { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } @AfterClass public static void afterClass() { sessionFactory.close(); } @Test public void testExportTable() { new SchemaExport(new AnnotationConfiguration().configure()).create(true, true); } @Test public void testSaveAndLoad() { Person person = new Person(); person.setName("Kylin Soong"); Laptop l = new Laptop(); l.setBrand("DELL"); person.setLaptop(l); Session s = null; s = sessionFactory.getCurrentSession(); s.beginTransaction(); s.save(person); s.getTransaction().commit(); s = sessionFactory.getCurrentSession(); s.beginTransaction(); Person p = (Person) s.load(Person.class, 1); assert("DELL".equals(p.getLaptop().getBrand())); s.getTransaction().commit(); } }
在给出数据库中所建表结构,如下图:
1.4给出老版本HibernateXML配置
<hibernate-mapping> <class name="com.kylin.test.Person" dynamic-update="true"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <component name="laptop" class="com.kylin.test.Laptop"> <property name="brand"/> </component> </class> </hibernate-mapping>
2 多对一单向
2.1此处模拟多个User对一个Group
2.2相关Annotation配置及代码
@Entity @Table(name="t_user") public class User { private int id; private String name; private Group group; @Id @GeneratedValue 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; } @ManyToOne public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } }
@Entity @Table(name="t_group") public class Group { private int id; private String name; @Id @GeneratedValue 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; } }
2.3给出数据库中产生表结构,如下图:
2.4老版本XML配置
<hibernate-mapping> <class name="com.kylin.test.Group" dynamic-update="true" table="t_group"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> </class> </hibernate-mapping>
<hibernate-mapping> <class name="com.kylin.test.User" dynamic-update="true" table="t_user"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <many-to-one name="group" column="groupId" /> </class> </hibernate-mapping>
3 一对多单向
3.1 同样还是刚才User和Group,现在一个Group对应多个User
3.2 相关Annotation配置及代码
@Entity @Table(name="t_group") public class Group { private int id; private String name; private Set<User> users; public Group(String name) { this.name = name; } @Id @GeneratedValue 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; } @OneToMany @JoinColumn(name="groupId") public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } }
@Entity @Table(name="t_user") public class User { private int id; private String name; public User(String name) { this.name = name; } @Id @GeneratedValue 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; } }
3.3数据库中生成表结构
3.4XML配置
<hibernate-mapping> <class name="com.kylin.test.Group" dynamic-update="true" table="t_group"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <set name="users"> <key column="groupId"></key> <one-to-many class="com.kylin.test.User"></one-to-many> </set> </class> </hibernate-mapping>
<hibernate-mapping> <class name="com.kylin.test.User" dynamic-update="true" table="t_user"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> </class> </hibernate-mapping>
4. 多对多单向关联映射
4.1 描述:如下图所示:
学生和课程是典型的多对多的关系,这部分就描述一个学生和课程的多对多单向映射实例
4.2 直接贴出代码
@Entity @Table(name="tt_course") public class Course { private int id; private String name; private Set<Student> students = new HashSet<Student>(); @Id 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; } @ManyToMany @JoinTable(name="tt_cs") public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }
@Entity @Table(name="tt_student") public class Student { private int id; private String name; @Id 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; } }
4.3给出数据库中生成的表结构,如下图:
4.4同样给出老版本HibernateXML配置
<hibernate-mapping> <class name="com.kylin.test.Course"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <set name="students" table="t_s"> <key column="course_id"></key> <many-to-many class="com.kylin.test.Student" column="student_id"/> </set> </class> </hibernate-mapping>
<hibernate-mapping> <class name="com.kylin.test.Student"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> </class> </hibernate-mapping>
5 一对多或多对一双向
5.1简单描述,以User和Group为例说明一对多或多对一双向映射
5.2,贴出代码
@Entity @Table(name="t_group") public class Group { private int id; private String name; private Set<User> users = new HashSet<User>(); @Id @GeneratedValue 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; } @OneToMany(mappedBy="group", cascade={CascadeType.ALL}, fetch=FetchType.LAZY ) public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } }
@Entity @Table(name="t_user") public class User { private int id; private String name; private Group group; @Id @GeneratedValue 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; } @ManyToOne( cascade={CascadeType.REMOVE}, fetch=FetchType.LAZY) public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } }
5.3给出数据库中生成表结构
5.4,老版本XML配置
<hibernate-mapping> <class name="com.kylin.test.Group" dynamic-update="true" table="t_group"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <set name="users"> <key column="groupId"></key> <one-to-many class="com.kylin.test.User"></one-to-many> </set> </class> </hibernate-mapping>
<hibernate-mapping> <class name="com.kylin.test.User" dynamic-update="true" table="t_user"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <many-to-one name="group" column="groupId"></many-to-one> </class> </hibernate-mapping>
6 多对多双向映射
6.1 同多对多单向一样,以Student,Course来说明多对多双向映射
6.2,贴出代码
@Entity @Table(name="tt_course") public class Course { private int id; private String name; private Set<Student> students = new HashSet<Student>(); @Id 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; } @ManyToMany @JoinTable(name="tt_cs") public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }
@Entity @Table(name="tt_student") public class Student { private int id; private String name; private Set<Course> courses = new HashSet<Course>(); @Id 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; } @ManyToMany(mappedBy="students") public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; } }
6.3给出数据库中表结构
6.4老版本XML配置
<hibernate-mapping> <class name="com.kylin.test.Course"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <set name="students" table="t_s"> <key column="course_id"></key> <many-to-many class="com.kylin.test.Student" column="student_id"/> </set> </class> </hibernate-mapping>
<hibernate-mapping> <class name="com.kylin.test.Student"> <id name="id"> <generator class="native"></generator> </id> <set name="courses" table="tt_cs"> <key column="student_id"></key> <many-to-many class="com.kylin.test.Course" column="course_id"/> </set> <property name="name"></property> </class> </hibernate-mapping>
本章结束
发表评论
-
Oracle - Add Exist Validation Before Create Table
2011-11-07 13:49 1464Usually we need to check the ta ... -
JMX Architecture & "Hello Word" the JMX way
2011-10-25 20:07 1819JMX Architecture Overview: JMX ... -
Jboss-eap-5.1 Messaging
2011-08-02 21:50 2481This section I will concertate ... -
Jboss-eap-5.1 starting up note
2011-07-26 22:46 2593Jboss enterprise platform 5 hav ... -
EJB Security & JAAS Demo
2011-05-21 19:39 1616PROLOGUE: When deploying ... -
JBoss LoginInitialContext Factory Implementation
2011-05-15 16:05 1537Jboss has a series of imp ... -
Jboss Reference Exception Gallery
2011-04-27 14:08 28991. Unable to locate a login con ... -
Hibernate Annotation 的一个问题,给点意见
2011-03-10 12:43 22问题:org.hibernate.annotations. ... -
大家说说BBC的网站用的是什么技术做的
2011-02-22 05:01 1443最近在英国出差,发现这里的一些网站做的相当有特色,有些网站不是 ... -
Hibernate OneToMany 单向和双向配置对数据存取性能的比较
2011-02-08 17:06 23341. 开篇说明:今天是春 ... -
对Hibernate属性(CascadeType、JoinColumn、JoinTable、ForeignKey等)的研究
2010-12-26 15:45 16689本文列出几个“EJB 学习阶段总结:JBoss下发布一个Toy ... -
EJB 学习阶段总结:JBoss下发布一个Toy企业应用
2010-12-25 12:11 2649解释题目:为什 ... -
EJB7: Message Driven Bean
2010-12-21 22:42 2153在企业系统中需要使用 ... -
EJB6: EntityBean例子
2010-11-26 14:48 1517本例子描述向EJB容器(JBoss)部署http: ... -
JPA dev: 几个问题总结(续)
2010-11-25 18:02 24892. 如何由PoJo类生成数据库中的表 首先可以根据实体间关 ... -
JPA dev: 几个问题总结
2010-11-25 16:56 3409最近工作中遇到几个与JPA相关的问题,本文通过一个例子总结一下 ... -
JAXB学习
2010-11-24 22:35 01 什么是JAXB? JAXB全称Java Ar ... -
EJB5: JPA扩展-J2SE环境下使用EntityManager
2010-11-10 19:07 2712好久没有写博客了,最近比较忙,今天抽时间写点,最近 ... -
EJB4:RMI和RMI-IIOP
2010-11-02 21:14 4138计划提纲:引入→RMI概念→RMI HelloWorld程序→ ... -
EJB3: JBOSS 企业版、JBOSS服务器构架、EJB2.0 HelloWrold实例
2010-10-26 22:43 6479本文的研究基于jboss-eap- ...
相关推荐
hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-4.1.12.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar ...
《深入理解Hibernate配置与映射:hibernate-configuration-3.0.dtd与hibernate-mapping-3.0.dtd解析》 在Java世界里,Hibernate作为一款强大的对象关系映射(ORM)框架,极大地简化了数据库操作。而`hibernate-...
赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...
hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码 hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码
在本文中,我们将深入探讨`hibernate-commons-annotations-5.0.1.Final.jar`的源码,了解其内部结构和主要功能。 一、元数据注解 HCA的核心在于提供了一系列的注解,如`@Entity`、`@Table`、`@Column`、`@Id`等,...
很多人为了配置jpa找这个动态产生字节码的jar文件,hibernate-distribution-3.3.1.GA包太大,而hibernate-distribution-3.3.2.GA的jar没有这个jar文件,希望对大家有用
2. **库文件**:包含jar包,如hibernate-core.jar、hibernate-entitymanager.jar等,这些是我们在项目中引入Hibernate时需要用到的依赖库。 3. **文档**:通常包括用户指南、API文档、开发者文档等,这些文档提供了...
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final-sources.jar hibernate jpa 源代码
hibernate-core-5.4.24.Final.jar
使用hibernate-validator 进行校验的jar包,里面包括了基础hibernate-validator-5.0.0.CR2.jar hibernate-validator-annotation-processor-5.0.0.CR2.jar 之外,还包括了el-api-2.2.jar javax.el-2.2.4等项目必不可...
hibernate-commons-annotations-4.0.1.Final.jar
Hibernate稳定版(hibernate-release-5.3.23.Final.zip),Hibernate ORM 是一个为应用程序、库和框架提供对象/关系映射 (ORM) 支持的库。它还提供了 JPA 规范的实现,这是 ORM 的标准 Java 规范。
hibernate-mapping-3.0.dtd 配置后,就会在xml中进行提示
javaMD5加密及登录验证(备忘) - Hibernate - Java - ITeye论坛.mhtjavaMD5加密及登录验证(备忘) - Hibernate - Java - ITeye论坛.mhtjavaMD5加密及登录验证(备忘
javaEE框架Hibernate安装包 hibernate-release-5.0.7.Final
hibernate-commons-annotations-5.1.0.Final.jar
hibernate-release-5.0.7.Final压缩包 -document -lib -project 内部Hibernate依赖库: antlr-2.7.7.jar dom4j-1.6.1.jar geronimo-jta_1.1_spec-1.1.1.jar hibernate-commons-annotations-5.0.1.Final.jar ...
本次我们聚焦于`hibernate-release-4.3.10.Final`版本,这是一个稳定且功能丰富的版本,为开发者提供了强大的数据持久化支持。 1. **Hibernate概述**: Hibernate 是一款开源的 ORM 框架,它将 Java 类与数据库表...