- 浏览: 1974459 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (509)
- JavaEE (122)
- Oracle数据库 (29)
- JavaScript (37)
- SAP (5)
- MySql数据库 (7)
- JavaSE (4)
- Ajax (1)
- jQuery (13)
- SSH框架 (36)
- Web Service (10)
- JSF框架 (2)
- JBPM (0)
- ireport报表 (2)
- ibatis (5)
- Hibernate (31)
- JSP (11)
- Tomcat 服务器 (20)
- Other (19)
- JavaWeb (4)
- Maven (11)
- OSWorkFlow (10)
- HTML (13)
- Exception汇总 (7)
- SVN (2)
- 笑话 (1)
- JSTL (1)
- WebSphere Message Broker (13)
- ANT命令 (3)
- Liunx (12)
- Struts2 (26)
- Eclipse (6)
- DOS (3)
- Flex (11)
- WebSphere (1)
- 开发常用工具 (3)
- Junit (2)
- EJB (4)
- Struts1.2 (2)
- Jboss (1)
- Android (2)
- Java框架源码解析 (1)
- Spring (4)
- MyBatis (6)
- SpringMVC (4)
- Jetty (2)
- 数据库表设计 (1)
- SSO (4)
最新评论
-
贝塔ZQ:
也可以试试PageOffice插件,觉得更简单点
Jxl操作Excel设置背景、字体颜色、对齐方式、列的宽度 -
jia1208:
...
Could not publish server configuration for Tomcat v6.0 Server at localhost. -
u011274527:
赞
java.io.EOFException java.io.ObjectInputStream$PeekInputStream.readFully 错误 -
旭旭小牛啦:
怎么没哟了,继续赛
jQuery 选择器 -
wzw3919:
100行会报空指针
Java 解压缩zip文件
Hibernate的关联关系很多,也非常的复杂.
常见的有 one to one ,one to many ,many to one, many to many等等.
学习这些,关联关系.这个基本上占据了Hibernate学习的七成时间。熟悉这些映射模型,需要大量的实践才能掌握。
以下,就先拿最简单关联关系讲起..one to one 关联的实例, 比如1个人只有一个居住地址, 1个人只有一张唯一的身份证。
这里就拿,人和地址作为1对1的关联关系。
做这些实例采用 有如下开发工具:
eclipse 3.3
mysql 5.4 数据库.
tomcat 6.0
本实例,将通过Mapping 映射文件自动生成数据库表;
建立两个Pojo类,一个Person,一个Address类.
具体代码如下:
package pack.java.model; import java.io.Serializable; /** * 一个人只有一个地址; * 1对1,单向关联 * @author Administrator * */ public class Person implements Serializable{ private static final long serialVersionUID = -6313867775683964530L; private Integer id; private String name; private Integer age; private Address address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
Address类代码: 应为是单向关联,则address类不需要关联到person类.
package pack.java.model; import java.io.Serializable; /** * * 一对一的关系; * @author Administrator * */ public class Address implements Serializable{ private static final long serialVersionUID = 3635140598485086087L; private Integer addressID; private String addressDetail; public Integer getAddressID() { return addressID; } public void setAddressID(Integer addressID) { this.addressID = addressID; } public String getAddressDetail() { return addressDetail; } public void setAddressDetail(String addressDetail) { this.addressDetail = addressDetail; } }
接下来,手动建立Person.hbm.xml文件,或者也可以通过myEclipse工具直接生成.
Person.hbm.xml 代码如下:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="pack.java.model.Person" table="ZHT_PERSON"> <id name="id" type="java.lang.Integer" column="ID"> <generator class="identity"></generator> </id> <property name="name" type="java.lang.String"></property> <property name="age" type="java.lang.Integer"></property> <!-- 在Person里面的many-to-one里面的unique='true'是代表,唯一的,也就是一对一的关系. column 是address 对象里面的属性名称、 name 是person 对象里面的对象名称。 --> <many-to-one name="address" class="pack.java.model.Address" column="addressID" unique="true" cascade="save-update,persist" > </many-to-one> </class> </hibernate-mapping>
建立Address.hbm.xml文件:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="pack.java.model.Address" table="ZHT_ADDRESS"> <id name="addressID" column="addressID" type="java.lang.Integer"> <generator class="identity"></generator> </id> <!-- 属性映射 --> <property name="addressDetail" type="java.lang.String"></property> </class> </hibernate-mapping>
建立好之后,然后建立一个Hibernate的配置文件,添加连接数据库的一些基本信息.以及Mapping文件的映射地址.
Hibernate.cfg.xml文件:
<?xml version='1.0' encoding='UTF-8'?> <!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="connection.username">root</property> <property name="connection.password">mysql</property> <property name="connection.url">jdbc:mysql://localhost/test</property> <property name="connection.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!--自动创建sql脚本--> <property name="hbm2ddl.auto">create</property> <property name="connection.show_sql">true</property> <property name="connection.format_sql">true</property> <!-- Mapping映射文档; --> <mapping resource="pack/java/model/Address_one_to_one.hbm.xml"/> <mapping resource="pack/java/model/Person_one_to_one.hbm.xml"/> </session-factory> </hibernate-configuration>
最后,我们在建立一个HibernateSessionFactory文件,这个文件可以自动生成..
具体代码如下:
package pack.java.hibernate; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; /** * Configures and provides access to Hibernate sessions, tied to the * current thread of execution. Follows the Thread Local Session * pattern, see {@link http://hibernate.org/42.html }. */ public class HibernateSessionFactory { /** * Location of hibernate.cfg.xml file. * Location should be on the classpath as Hibernate uses * #resourceAsStream style lookup for its configuration file. * The default classpath location of the hibernate config file is * in the default package. Use #setConfigFile() to update * the location of the configuration file for the current session. */ private static String CONFIG_FILE_LOCATION = "pack/java/hibernate/hibernate.cfg.xml"; private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static Configuration configuration = new Configuration(); private static org.hibernate.SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION; static { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err .println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } private HibernateSessionFactory() { } /** * Returns the ThreadLocal Session instance. Lazy initialize * the <code>SessionFactory</code> if needed. * * @return Session * @throws HibernateException */ public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) { if (sessionFactory == null) { rebuildSessionFactory(); } session = (sessionFactory != null) ? sessionFactory.openSession() : null; threadLocal.set(session); } return session; } /** * Rebuild hibernate session factory * */ public static void rebuildSessionFactory() { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err .println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } /** * Close the single hibernate session instance. * * @throws HibernateException */ public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); } } /** * return session factory * */ public static org.hibernate.SessionFactory getSessionFactory() { return sessionFactory; } /** * return session factory * * session factory will be rebuilded in the next call */ public static void setConfigFile(String configFile) { HibernateSessionFactory.configFile = configFile; sessionFactory = null; } /** * return hibernate configuration * */ public static Configuration getConfiguration() { return configuration; } }
最后,我们可以在新建一个Test类来测试,这些Hibernate映射关系了.
package pack.java.test; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import pack.java.hibernate.HibernateSessionFactory; import pack.java.model.Address; import pack.java.model.Person; /** * 测试; * @author Administrator * */ public class HibernateDaoDemo { /** * 测试主方法; * @param args */ public static void main(String[] args) { Session session = getSession(); HibernateDaoDemo hibernateDaoDemo = new HibernateDaoDemo(); //保存person对象; hibernateDaoDemo.savePerson(session); //查询; List<Person> personList = hibernateDaoDemo.queryPerson(session); for(Person person:personList){ System.out.println(person.getId()+","+person.getName()+","+person.getAge()+","+person.getAddress()); } } /** * 创建session; * @return */ private static Session getSession(){ return HibernateSessionFactory.getSession(); } /** * 保存Person; * @param session */ private void savePerson(Session session){ Person person = new Person(); person.setAge(23); person.setName("周海涛"); Address address = new Address(); address.setAddressDetail("中国湖南株洲市"); person.setAddress(address); session.beginTransaction().begin(); //保存; session.save(person); session.beginTransaction().commit(); System.out.println("保存成功!"); } /** * 查询Person; * @param session * @return */ private List<Person> queryPerson(Session session){ Query query = session.createQuery("from Person p where p.name=:name"); query.setParameter("name", "ZhouHaiTao"); List<Person> list = query.list(); return list; } }
- HibernateDemo.rar (8.1 MB)
- 下载次数: 6
发表评论
-
大型互联网服务集成平台:springmvc+mybatis+restfull+JMS+webservice+bootstrap
2015-04-12 23:36 22SpringMVC + Mybatis + SpringS ... -
Spring中二级缓存的配置
2011-12-18 20:40 22111.首先,在spring的hibernate配置里(app ... -
Hibernate 配置文件提示
2011-12-11 16:03 10071.这个则是hibernate.cfg.xml文件的头信息.. ... -
Hibernate中二级缓存的配置
2011-10-18 00:26 1236我们知道 hibernate的session提供了一级缓存, ... -
Hibernate 生成策略属性
2011-10-17 14:01 987"assigned" 主键由外部程序负责生 ... -
Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/Syn
2011-10-16 18:37 2432Exception in thread "main& ... -
Hibernate缓存的分类
2011-10-16 11:25 1004Hibernate缓存分类: Session ... -
在Hibernate中使用日期函数获取数据库时间
2011-10-04 20:13 1644在Hibernate中使用日期函数获取数据库时间 ... -
Hibernate关联映射-one to one主键双向关联
2011-09-23 01:11 1294这次,继续上一次的实例。 记录1对1 主键双向关联. ... -
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: attem
2011-09-23 00:46 2039今天在做Hibernate中1多1双向关联映射的实例时, ... -
Hibernate关联映射-one to one 外键双向关联.
2011-09-22 00:28 1151说明:双向一对一,是一对多的特例,只需要在多的一方设置成 ... -
Hibernate关联映射-many to many 外键单向关联.
2011-09-20 01:18 1428多对多的实例很多,这里说一个比较简单的例子,而且还是单向 ... -
Hibernate关联映射-many to one 外键单向关联
2011-09-18 00:25 1930多个人可以对应一个地址,也就是所谓的多对一关系,many to ... -
Hibernate关联映射-one to to 主键单向关联
2011-09-17 23:23 1026这次介绍 还是以Person 和 Address 为例. ... -
Hibernate关联映射-one to many外键单向关联
2011-09-15 01:14 4565以下是说明,1对多 单向外键关联 的实例.同样以person ... -
org.hibernate.TransientObjectException: object references an unsaved transient i
2011-09-12 19:02 1464org.hibernate.TransientObjectEx ... -
org.hibernate.PropertyAccessException: Exception occurred inside getter of pack.
2011-09-12 18:54 6103报如下,异常是因为,在Person对象中.设置的属性类型 ... -
org.hibernate.MappingException: could not instantiate id generator
2011-09-10 14:32 2443在搭建Hibernate的环境时, ... -
配置hibernate.properties 或 hibernate.cfg.xml 文件时,在配置JDBC连接参数时,要配置数据库方言
2011-09-10 13:04 1910各数据库的方言 (配置 hibernate.cfg.xml 时 ... -
java.sql.SQLException: ORA-01747: user.table.column, table.column 或列说明无效
2011-08-17 11:53 7163今天,使用Hibernate框架,做映射时,添加数据时, ...
相关推荐
hibernate one-to-one 一对一唯一外键关联映射_单向 and 双向
Hibernate 一对一外键单向关联 Hibernate 一对一主键单向关联 Hibernate 一对一连接表单向关联 Hibernate 一对多外键单向关联 Hibernate 一对多连接表单向关联 Hibernate 多对一外键单向关联 Hibernate 多对一连接...
Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联 │ ├─ 一对一主键单向关联 │ ├─ 一对一连接表单向关联 │ ├─ 一对多外键单向关联 │ ├─ 一对多连接表单向关联 │ ├─ 多对一外键单向关联 │ ...
博文链接:https://shaqiang32.iteye.com/blog/201311
020 <one-to-one>、<many-to-one>单端关联上的 lazy(懒加载)属性 021 继承关联映射 022 component(组件)关联映射 023 复合主键 关联映射 024 其它 关联映射 025 hibernate 悲观锁、乐观锁 026 hibernate 操作树形...
Hibernate 实体关联关系映射 学习总结 把一对一 一对多 单向 双向 主键 外键 链接表等讲的比较清楚
hibernate一对一唯一外键关联映射(单向关联Person---->IdCard) 10 hibernate一对一唯一外键关联映射(双向关联Person<---->IdCard) 11 session flush测试(hibernate_session_flush) 12 hihernate一对多关联映射...
http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码
和其它许多批注一样,在多对多关联中很多值是自动生成,党双向多对多关联中没有定义任何物理映射时,Hibernate根据以下规则生成相应的值,关联表名:主表表名+下划线+从表表名,关联到主表的外键名:主表名+下划线+...
Hibernate高级映射的几个单向关系,单向一对一(共享主键方式、唯一外键方式),单向多对一,单向一对多,单向多对多。
个人的很详细的Hibernate一对一映射配置详解,对初学者有帮助!
Hibernate映射-单向关联 外键一对一关联 在此示例中,我创建两个表:人员和地址。 这两个表都有一个自动递增的id作为主键。 人员表持有地址表中的外键。
5.1.10. 多对一(many-to-one) 5.1.11. 一对一 5.1.12. 自然ID(natural-id) 5.1.13. 组件(component), 动态组件(dynamic-component) 5.1.14. properties 5.1.15. 子类(subclass) 5.1.16. 连接的子类(joined-...
5.1.10. 多对一(many-to-one) 5.1.11. 一对一 5.1.12. 自然ID(natural-id) 5.1.13. 组件(component), 动态组件(dynamic-component) 5.1.14. properties 5.1.15. 子类(subclass) 5.1.16. 连接的子类(joined-...
5.1.10. 多对一(many-to-one) 5.1.11. 一对一 5.1.12. 自然ID(natural-id) 5.1.13. 组件(component), 动态组件(dynamic-component) 5.1.14. properties 5.1.15. 子类(subclass) 5.1.16. 连接的子类(joined-...
5.1.10. 多对一(many-to-one) 5.1.11. 一对一 5.1.12. 自然ID(natural-id) 5.1.13. 组件(component), 动态组件(dynamic-component) 5.1.14. properties 5.1.15. 子类(subclass) 5.1.16. 连接的子类(joined-...
15.2 映射单向多对多关联 15.3 映射双向多对多关联关系 15.3.1 关联两端使用元素 15.3.2 在inverse端使用元素 15.3.3 使用组件类集合 15.3.4 把多对多关联分解为两个一对多关联 15.4 小结 15.5 思考...
5.1.10. 多对一(many-to-one) 5.1.11. 一对一 5.1.12. 自然ID(natural-id) 5.1.13. 组件(component), 动态组件(dynamic-component) 5.1.14. properties 5.1.15. 子类(subclass) 5.1.16. 连接的子类(joined-...