- 浏览: 51227 次
- 性别:
- 来自: 长沙
最新评论
-
yaojianxiaoyu:
看你貌似不知道怎么弄,
public class BaseD ...
请教关于得到泛型的class -
d_xxm:
clazz = (Class<T>) ((Pa ...
请教关于得到泛型的class -
jiajia11:
其实第二种方法我觉得不是泛型的应用,你把<T>去掉 ...
请教关于得到泛型的class -
feisan:
推荐使用pylibmc
cmemcache不是很稳定
ubuntu9.04编译安装libmemcache1.4.0.rc -
seman18:
assertTrue("check",a= ...
hibernate入门使用系列 1-- 说明篇+试用篇
接上篇 hibernate入门使用系列 2-- xml关系映射篇(上)
上篇讲了1:1,那么这次继续讲1:n和n:1。
这次用到的例子是Father和child之间的关系。一个father可以有n个child,但是1个child只有一个father。这里只说生父。至于其他的继父、养父、干爹等等,不再范围之内。
好吧。还是同前面的一样。现建立实体模型如下:
根据模型创建数据库。sql脚本如下:
use HibernateQuickUse; drop table if exists Child; drop table if exists Father; create table Father ( id varchar(32) primary key, name varchar(32) not null ); create table Child ( id varchar(32) primary key, name varchar(128) not null, father_id varchar(32) not null, foreign key(father_id) references Father(id) );
根据模型创建java对象。
Father.java:
package org.py.hib.relation.one2many; import java.util.HashSet; import java.util.Set; /** * Father entity. */ @SuppressWarnings("serial") public class Father implements java.io.Serializable { private String id; private String name; private Set<Child> children = new HashSet<Child>(0); public Father() { } public String getId() { return this.id; } public void setId(String id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Set<Child> getChildren() { return children; } public void setChildren(Set<Child> children) { this.children = children; } }
Child.java:
package org.py.hib.relation.one2many; /** * Child entity. * @author MyEclipse Persistence Tools */ @SuppressWarnings("serial") public class Child implements java.io.Serializable { private String id; private String name; private Father father; public Child() { } public String getId() { return this.id; } public void setId(String id) { this.id = id; } public Father getFather() { return this.father; } public void setFather(Father father) { this.father = father; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } }
映射文件如下:
Father.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="org.py.hib.relation.one2many.Father" table="father"> <id name="id" type="java.lang.String" column="id" length="32"> <generator class="uuid" /> </id> <property name="name" type="java.lang.String" column="name" length="32" not-null="true"/> <set name="children" table="child" cascade="all" inverse="true"> <key column="father_id" /> <one-to-many class="org.py.hib.relation.one2many.Child" /> </set> </class> </hibernate-mapping>
这里要说说 "set" 这个标签里面的内容。
"name"是Father里面的属性的名字。
"table"表示它对应的是数据库中的哪个表。
cascade="all" 表示所有的操作都级联操作。
"inverse"表示关系的维护由谁来执行。true表示不由自己执行,而有对应的另外一方执行。false则相反,表示由自己维护关系。这里设置成 true 是由原因的。如果说把它设置成为false,那么就由他来维护关系了。
这里得说一下inverse属性的问题。在one-to-many中,如果关系由one来维护,那么会很麻烦,性能也会很低。每次对many一方的一条记录进行增、删、改 时都会多一次update操作。原因很简单,因为关系的维护设置在了one这一方,所以对many的每一次操作,one这一方都要维护一次双方的关系。
这个就好像皇帝和老百姓的关系。试问,是来一个老百姓,皇帝就宣布他是我的子民,还是由老百姓直接选择做那个皇帝的子民更加有效率呢?呵呵。不知道这个例子大家有没有明白。关于inverse的更具体的说明,在javaeye上搜一下,就会发现有很多。这里推荐一篇,我认为讲得很明白的:主题:inverse。
"key" 中的 "column" 表示在table(这里的table是child)中, 跟Father关联的字段名称。这里是"father_id"。可以看看开始的sql脚本。
one-to-many 表示father和children的关系。class则表示是同哪个类是这种关系。
Child.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="org.py.hib.relation.one2many.Child" table="child"> <id name="id" type="java.lang.String" column="id" length="32" > <generator class="uuid" /> </id> <property name="name" type="java.lang.String" column="name" length="128" not-null="true"/> <many-to-one name="father" class="org.py.hib.relation.one2many.Father" column="father_id" /> </class> </hibernate-mapping>
这个里面主要就是多了一个many-to-one,表示child 和 father 的关系是"many-to-one"
测试代码如下:
One2ManyTest.java
package org.py.hib.relation.one2many; import java.util.Set; import junit.framework.Assert; import junit.framework.TestCase; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; public class One2ManyTest extends TestCase { private SessionFactory factory; private static final String[] childname = new String[] { "child_1", "child_2", "child_3" }; private static final String[] newchildname = new String[] { "new_child_1", "new_child_2", "new_child_3" }; @Before public void setUp() throws Exception { Configuration conf = new Configuration().configure(); factory = conf.buildSessionFactory(); } /** * 测试添加 * @throws Exception */ public void testSave() throws Exception { System.out.println("\n=== test save ==="); Father father = new Father(); father.setName("Father_1"); Child child1 = new Child(); child1.setName(childname[0]); Child child2 = new Child(); child2.setName(childname[1]); Child child3 = new Child(); child3.setName(childname[2]); father.getChildren().add(child1); father.getChildren().add(child2); father.getChildren().add(child3); child1.setFather(father); child2.setFather(father); child3.setFather(father); Session session = null; Transaction tran = null; try { session = factory.openSession(); tran = session.beginTransaction(); session.save(father); tran.commit(); Assert.assertNotNull(father.getId()); Assert.assertNotNull(child1.getId()); Assert.assertNotNull(child2.getId()); Assert.assertNotNull(child3.getId()); } catch (Exception ex) { tran.rollback(); throw ex; } finally { if (session != null) { try { session.close(); } catch (Exception ex) { // nothing to do } finally { if (session != null) session = null; } } } } private boolean isChildrenName(String name) { for (String n : childname) { if (n.equals(name)) return true; } return false; } private boolean isNewChildrenName(String name) { for (String n : newchildname) { if (n.equals(name)) return true; } return false; } /** * 测试查询 * @throws Exception */ public void testFind() throws Exception { System.out.println("\n=== test find ==="); Session session = null; try { session = factory.openSession(); Father father = (Father) session.createQuery("from Father").list().get(0); Assert.assertNotNull(father.getId()); Assert.assertEquals("Father_1", father.getName()); Set<Child> children = father.getChildren(); for (Child child : children) { Assert.assertEquals(child.getFather(), father); Assert.assertNotNull(child.getId()); Assert.assertTrue(isChildrenName(child.getName())); } } catch (Exception ex) { throw ex; } finally { if (session != null) { try { session.close(); } catch (Exception ex) { // nothing to do } finally { if (session != null) session = null; } } } } /** * 测试修改 * @throws Exception */ public void testModify() throws Exception { System.out.println("\n=== test modify ==="); Session session = null; Transaction tran = null; try { session = factory.openSession(); tran = session.beginTransaction(); Father father = (Father) session.createQuery("from Father").list().get(0); father.setName("Father_2"); // 修改用户名 = m_name2.(原来用户名= m_name) Set<Child> children = father.getChildren(); int i = 0; for (Child child : children) { child.setName(newchildname[i++]); } tran.commit(); } catch (Exception ex) { throw ex; } finally { if (session != null) { try { session.close(); } catch (Exception ex) { // nothing to do } finally { if (session != null) session = null; } } } /* * 修改后再查询 */ System.out.println("\n=== test find after modify ==="); try { session = factory.openSession(); Father father = (Father) session.createQuery("from Father").list().get(0); Assert.assertNotNull(father.getId()); Assert.assertEquals("Father_2", father.getName()); Set<Child> children = father.getChildren(); for (Child child : children) { Assert.assertEquals(child.getFather(), father); Assert.assertNotNull(child.getId()); Assert.assertTrue(isNewChildrenName(child.getName())); } } catch (Exception ex) { throw ex; } finally { if (session != null) { try { session.close(); } catch (Exception ex) { // nothing to do } finally { if (session != null) session = null; } } } } /** * 测试删除 * @throws Exception */ public void testDelete() throws Exception { System.out.println("\n=== test delete ==="); Session session = null; Transaction tran = null; try { session = factory.openSession(); tran = session.beginTransaction(); Father father = (Father) session.createQuery("from Father").list().get(0); session.delete(father); tran.commit(); } catch (Exception ex) { throw ex; } finally { if (session != null) { try { session.close(); } catch (Exception ex) { // nothing to do } finally { if (session != null) session = null; } } } /* * 删除后再查询 */ System.out.println("\n=== test find after delete ==="); try { session = factory.openSession(); Integer num = (Integer) session.createQuery("from Father").list().size(); Assert.assertEquals(0, num.intValue()); num = (Integer) session.createQuery("from Child").list().size(); Assert.assertEquals(0, num.intValue()); } catch (Exception ex) { throw ex; } finally { if (session != null) { try { session.close(); } catch (Exception ex) { // nothing to do } finally { if (session != null) session = null; } } } } /** * */ @After public void tearDown() throws Exception { factory.close(); } }
这里不得不再重申以下 one-to-many 中 inverse 关系的维护问题。 在one-to-many中,把inverse放到many中来维护是一个好的习惯。大家可以把上面的inverse改成false,看看会发生什么情况。
在inverse=true的时候,输出结果如下:
=== test save === Hibernate: insert into father (name, id) values (?, ?) Hibernate: insert into child (name, father_id, id) values (?, ?, ?) Hibernate: insert into child (name, father_id, id) values (?, ?, ?) Hibernate: insert into child (name, father_id, id) values (?, ?, ?) === test find === Hibernate: select father0_.id as id13_, father0_.name as name13_ from father father0_ Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id14_0_, children0_.name as name14_0_, children0_.father_id as father3_14_0_ from child children0_ where children0_.father_id=? === test modify === Hibernate: select father0_.id as id23_, father0_.name as name23_ from father father0_ Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id24_0_, children0_.name as name24_0_, children0_.father_id as father3_24_0_ from child children0_ where children0_.father_id=? Hibernate: update father set name=? where id=? Hibernate: update child set name=?, father_id=? where id=? Hibernate: update child set name=?, father_id=? where id=? Hibernate: update child set name=?, father_id=? where id=? === test find after modify === Hibernate: select father0_.id as id23_, father0_.name as name23_ from father father0_ Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id24_0_, children0_.name as name24_0_, children0_.father_id as father3_24_0_ from child children0_ where children0_.father_id=? === test delete === Hibernate: select father0_.id as id33_, father0_.name as name33_ from father father0_ Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id34_0_, children0_.name as name34_0_, children0_.father_id as father3_34_0_ from child children0_ where children0_.father_id=? Hibernate: delete from child where id=? Hibernate: delete from child where id=? Hibernate: delete from child where id=? Hibernate: delete from father where id=? === test find after delete === Hibernate: select father0_.id as id33_, father0_.name as name33_ from father father0_ Hibernate: select child0_.id as id34_, child0_.name as name34_, child0_.father_id as father3_34_ from child child0_
而改成 inverse=false后,testDelete()是没法通过的。输出如下:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. === test save === Hibernate: insert into father (name, id) values (?, ?) Hibernate: insert into child (name, father_id, id) values (?, ?, ?) Hibernate: insert into child (name, father_id, id) values (?, ?, ?) Hibernate: insert into child (name, father_id, id) values (?, ?, ?) Hibernate: update child set father_id=? where id=? Hibernate: update child set father_id=? where id=? Hibernate: update child set father_id=? where id=? === test find === Hibernate: select father0_.id as id13_, father0_.name as name13_ from father father0_ Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id14_0_, children0_.name as name14_0_, children0_.father_id as father3_14_0_ from child children0_ where children0_.father_id=? === test modify === Hibernate: select father0_.id as id23_, father0_.name as name23_ from father father0_ Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id24_0_, children0_.name as name24_0_, children0_.father_id as father3_24_0_ from child children0_ where children0_.father_id=? Hibernate: update father set name=? where id=? Hibernate: update child set name=?, father_id=? where id=? Hibernate: update child set name=?, father_id=? where id=? Hibernate: update child set name=?, father_id=? where id=? === test find after modify === Hibernate: select father0_.id as id23_, father0_.name as name23_ from father father0_ Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id24_0_, children0_.name as name24_0_, children0_.father_id as father3_24_0_ from child children0_ where children0_.father_id=? === test delete === Hibernate: select father0_.id as id33_, father0_.name as name33_ from father father0_ Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id34_0_, children0_.name as name34_0_, children0_.father_id as father3_34_0_ from child children0_ where children0_.father_id=? Hibernate: update child set father_id=null where father_id=?
错误信息如下:
具体的出错原因是:违反了非空约束。
得修改sql脚本,把Child的建表脚本中的:
father_id varchar(32) not null, 修改成为:father_id varchar(32),
才能通过。这个时候输出的结果是:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. === test save === Hibernate: insert into father (name, id) values (?, ?) Hibernate: insert into child (name, father_id, id) values (?, ?, ?) Hibernate: insert into child (name, father_id, id) values (?, ?, ?) Hibernate: insert into child (name, father_id, id) values (?, ?, ?) Hibernate: update child set father_id=? where id=? Hibernate: update child set father_id=? where id=? Hibernate: update child set father_id=? where id=? === test find === Hibernate: select father0_.id as id13_, father0_.name as name13_ from father father0_ Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id14_0_, children0_.name as name14_0_, children0_.father_id as father3_14_0_ from child children0_ where children0_.father_id=? === test modify === Hibernate: select father0_.id as id23_, father0_.name as name23_ from father father0_ Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id24_0_, children0_.name as name24_0_, children0_.father_id as father3_24_0_ from child children0_ where children0_.father_id=? Hibernate: update father set name=? where id=? Hibernate: update child set name=?, father_id=? where id=? Hibernate: update child set name=?, father_id=? where id=? Hibernate: update child set name=?, father_id=? where id=? === test find after modify === Hibernate: select father0_.id as id23_, father0_.name as name23_ from father father0_ Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id24_0_, children0_.name as name24_0_, children0_.father_id as father3_24_0_ from child children0_ where children0_.father_id=? === test delete === Hibernate: select father0_.id as id33_, father0_.name as name33_ from father father0_ Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id34_0_, children0_.name as name34_0_, children0_.father_id as father3_34_0_ from child children0_ where children0_.father_id=? Hibernate: update child set father_id=null where father_id=? Hibernate: delete from child where id=? Hibernate: delete from child where id=? Hibernate: delete from child where id=? Hibernate: delete from father where id=? === test find after delete === Hibernate: select father0_.id as id33_, father0_.name as name33_ from father father0_ Hibernate: select child0_.id as id34_, child0_.name as name34_, child0_.father_id as father3_34_ from child child0_
所以,inverse的设置是很重要的一个事情。
附件中包含了源代码。这里说明一下。源代码中没有包含数据库驱动的jar包和hibernate3.1的jar包。需要大家自己添加。
评论
现在你例子中是双向关联, 也就是说child类有对father类的引向, 这样在Child.hbm.xml中就有了<many-to-one name="father" class="org.py.hib.relation.one2many.Father" column="father_id" />, 这里的inverse是默认为false的. 这时两表间的引用由child类的setFather触发.
若业务要求为单向关联,也就是说在child这边不再有father的引用, 这样一来两表间的关联关系只能由father类来维护了, 当然在<set name="children" table="child" cascade="all" >里的inverse默认为false的, 可这样做效率上是受影响的: 当加child集合时hibernate得去检查看哪个child与father的关系有改变, 而假若由child这边来维护的话,就不会有这样效率方面的问题.
可在child的配置里若没有对father的引用后,我们也就无从下手了,不能两全其美.
这是什么意思?
还有就是我最也出了一个错,请帮我看一下,这是什么错误!
org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: java.lang.String; nested exception is org.hibernate.MappingException: Unknown entity: java.lang.String
org.hibernate.MappingException: Unknown entity: java.lang.String
我的程序中出现这个错误,是什么意思?请教一下
我用的是一对多关系映射,一的这方用的是List集合
SELECT f.name, c.name FROM Father f, Child c WHERE f.id = c.father_id;
查了好多天没头绪,请高手们不吝指教!
我要是实现自定义表的字段···怎么实现咯 ··
不明白什么叫自定义表的字段。能否举一例?
他们的数据能进行查进去 么 ,因为表的关联是一下子取(通SET与GET)全部的数据··· 会报SQL语句的异常
表的关联可以不一下子全取。hibernate提供了 延迟处理。3版本后默认为lazy。
关于lazy的用法参看这个,http://www.hibernate.org/162.html
一个题目可以有a。b。c。d四个选项。题目类:subject 选项类 chooseItem
用户第一次建立题目是 可能是这样、
那个省人口最多
A.河南
B.河北
C.湖北
D.湖南
ok。保存入库,但用户下次修改时
可能如下
那个省人口最多
A.河南
B.河北
注意:只剩下两个选项 到此,此时的操作应该包括删除和修改多方的操作,这个hibernate能自动处理吗?该怎么处理?请赐教
完全可以。看帖子里面的测试代码。里面有完整的增删该查。你可以仿照从而写出你的业务逻辑。
我要是实现自定义表的字段···怎么实现咯 ··
他们的数据能进行查进去 么 ,因为表的关联是一下子取(通SET与GET)全部的数据··· 会报SQL语句的异常···
一个题目可以有a。b。c。d四个选项。题目类:subject 选项类 chooseItem
用户第一次建立题目是 可能是这样、
那个省人口最多
A.河南
B.河北
C.湖北
D.湖南
ok。保存入库,但用户下次修改时
可能如下
那个省人口最多
A.河南
B.河北
注意:只剩下两个选项 到此,此时的操作应该包括删除和修改多方的操作,这个hibernate能自动处理吗?该怎么处理?请赐教
<p>如果是单向的one to many 关联(没有Child 到Parent 的关联),这个约束可以加在Parent的映射文件的set定义中去。</p>
发表评论
-
hibernate入门使用系列 8-- annotation关系映射篇(下)
2008-11-12 17:27 1942终于要说ManyToMany了 场景:Product和Cust ... -
hibernate入门使用系列 7-- annotation关系映射篇(中)
2008-11-12 16:38 1629这次说说OneToMany和ManyToO ... -
hibernate入门使用系列 6-- annotation关系映射篇(上)
2008-11-12 15:55 2556次讲@OneToOne的用法。而且是基于主外键的关联。因为这个 ... -
hibernate入门使用系列 5 -- xml关系映射篇(补充1)
2008-07-18 18:02 1622由于最近一段时间比较忙。一直被手中的项目牵着。所以,也就没有时 ... -
hibernate入门使用系列 4-- xml关系映射篇(下)
2008-05-07 17:39 3093接上篇 hibernate入门使用系列 3-- xml关系映射 ... -
hibernate入门使用系列 2-- xml关系映射篇(上)
2008-05-05 16:20 2676接上篇 hibernate入门使用系列 1-- 说明篇+试用篇 ... -
hibernate入门使用系列 1-- 说明篇+试用篇
2008-05-05 11:09 3764说明篇 写这个 入门使用 系列的文章, 学习笔 ... -
要不要在数据库中建立主外键约束
2008-05-04 12:48 4785今天发现一个问题。一个遗留的系统上,每个表都是独立的, ...
相关推荐
- **XML映射文件**: 传统上,我们使用`hbm.xml`文件来定义对象关系映射。每个实体类都有一个对应的映射文件,其中定义了属性与数据库字段的映射关系。 - **注解映射**: 现代Hibernate更倾向于使用注解进行映射,...
"Hibernate入门到精通" Hibernate 是一个基于Java的ORM(Object-Relational Mapping,对象关系映射)框架,它提供了一种简洁高效的方式来访问和操作关系数据库。下面是 Hibernate 的主要知识点: Hibernate 简介 ...
Hibernate 是一个对象关系映射(ORM)框架,它允许开发者将Java对象与数据库表进行映射,简化了数据库操作。在Myeclipse中,通过Add Hibernate Capabilities来添加Hibernate支持。配置时,可以选择Hibernate版本(如...
- **使用配置文件实现映射关系**:创建XML格式的配置文件(如`User.hbm.xml`),并在其中指定实体类与数据库表的映射规则。 - **引入DTD约束**:为了确保XML配置文件符合规范,需要在文件头部引入DTD(Document ...
本压缩包提供的是Hibernate入门所需的jar包,包括了Hibernate的核心库以及与之配合使用的相关组件。让我们深入探讨一下这些jar包以及它们在Hibernate中的作用。 1. Hibernate核心库: - `hibernate-core.jar`:这...
1. Hibernate配置:包括数据库连接信息、实体类映射文件的加载、缓存设置等,通常在`hibernate.cfg.xml`中完成。 2. 实体类设计:使用Java类来代表数据库中的表,使用注解进行映射。 3. Session工厂和Session:通过...
在本教程“使用Hibernate开发租房系统7-11”中,我们将深入探讨如何利用Hibernate这一强大的对象关系映射(ORM)框架来构建一个完整的租房管理应用程序。Hibernate是Java开发人员常用于简化数据库交互的工具,它允许...
若使用XML映射,创建对应的`orm.hbm.xml`文件。 5. **初始化SessionFactory**:在应用程序启动时,根据`hibernate.cfg.xml`创建SessionFactory实例。 6. **使用Session**:通过SessionFactory获取Session,执行...
3. 映射实体类:使用XML映射文件(`.hbm.xml`)或注解来定义Java类与数据库表的关系。 4. 初始化SessionFactory:在应用程序启动时,根据配置文件创建SessionFactory实例,它是线程安全的,整个应用程序只需要一个。...
### hibernate3入门知识点 #### 一、概述 **Hibernate**是一种对象关系映射(Object/Relational Mapping,简称ORM)解决方案,它提供了一种机制,用于将Java对象及其关系映射到关系型数据库中的表及其关系。通过...
3. 实体类与数据表映射:深入学习Hibernate的主键策略、注解配置、实体类的创建以及HBM XML映射文件,理解对象与数据库表的映射关系。 4. Session与Transaction:理解Hibernate中的Session接口,它是数据库会话的...
本文将深入探讨 Hibernate 中的一对一映射关系,并以"hibernate 映射关系学习入门 一对一映射Eclipse源码"为主题,详细解析其概念、配置以及实际应用。 一对一映射是Hibernate中的一种关联关系,它表示两个实体之间...
**hibernate系列(一)hibernate入门** 在Java世界中,ORM(Object-Relational Mapping,对象关系映射)框架是连接数据库与应用程序的重要桥梁,它将数据库中的数据与程序中的对象进行关联,使得开发者可以使用面向...
在Hibernate中,一对多关系的映射通常通过XML配置文件(hibernate.cfg.xml)或注解实现。这里我们主要讨论注解方式: 1. 在父实体类上,我们需要使用`@OneToMany`注解来定义一对多关系。这个注解包含以下属性: - ...
本文档旨在指导读者快速从 Hibernate 入门到掌握 Hibernate 的使用。 Hibernate 配置文件 在使用 Hibernate 之前,需要将相关的 JAR 包导入到项目中。这些 JAR 包位于 `hibernate-release-4.2.5.Final\lib\...
【hibernate入门小例子】是一个适合初学者的教程,主要涵盖了如何在JavaWeb项目中使用Hibernate框架与MySQL数据库进行集成。在这个例子中,我们将会深入理解Hibernate的核心概念,包括实体映射、对象关系映射(ORM)...
映射定义是 Hibernate 中的一个重要概念,用于定义对象到关系数据库的映射关系。映射定义可以通过 XML 文件或 Java 注解来实现。 hibernate-mapping hibernate-mapping 是 Hibernate 的映射文件,用于定义对象到...
然后,创建一个简单的实体类,例如`User`,并使用注解或XML映射文件将其映射到数据库中的`users`表。 **第二部分:Hibernate入门(下)** 在这一部分,我们将学习如何初始化SessionFactory,这是Hibernate的核心...
### Hibernate经典入门篇知识点梳理 #### 一、Hibernate简介与作用 - **概念**:Hibernate是一种开放源码的对象关系映射(ORM)框架,用于Java应用与关系型数据库之间的交互。它提供了一种机制,使得Java对象能够...
通常在业务逻辑中使用`Transaction`对象进行事务控制。 ```java Transaction transaction = session.beginTransaction(); // 数据操作 transaction.commit(); ``` ### 八、HQL与Criteria查询 Hibernate 提供了...