- 浏览: 1197416 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (361)
- java综合 (33)
- 项目管理 (10)
- 工作流 (6)
- spring (11)
- hibenate (17)
- struts (0)
- javascript,html,css,ajax,jquery (11)
- IDE (9)
- 生活 (0)
- 工作 (0)
- 杂记 (1)
- 数据库 (96)
- 服务器 (5)
- 可视编辑 (0)
- freemarker (6)
- 操作系统 windows (13)
- web页面 (6)
- jms (15)
- 调优 (4)
- 测试和bug管理 (2)
- 原理 (1)
- 項目-atf (17)
- 安全 (3)
- xml (4)
- 操作系统 liunx (21)
- 网络 (22)
- office (11)
- 设计 (2)
- 软件 (1)
- 数据库 mysql (6)
- 胖客户端-flex (1)
- 正则 (9)
- oracle- liunx (3)
- sql2000 (2)
- 模式 (1)
- 虚拟机 (2)
- jstl (2)
- 版本控制 打包工具 (0)
- AOP (1)
- demo (1)
- 小软件 (2)
- 感恩 (1)
- iphone 4 (1)
- 反欺诈业务经验整理 (0)
最新评论
-
sea0108:
mark
java内存模型 -
XingShiYiShi:
方便把:testPNR();具体实现发出来吗?谢谢
用正则表达式解析 航信的电子客票和pnr报文 -
wh359126613:
如果js和webservice不在同一个服务器上,有跨域问题如 ...
使用javascript调用webservice示例 -
雨飛雁舞:
...
oracle 动态性能(V$)视图 -
ouyang1224:
好东西
oracle 动态性能(V$)视图
many-to-one节点有以下属性(摘自Hibernate文档):
(1) name: 属性名。
(2) column (可选): 字段名。
(3) class (可选 - 默认是通过反射得到属性类型): 关联的类的名字。
(4) cascade(级联) (可选): 指明哪些操作会从父对象级联到关联的对象。
(5) outer-join(外连接) (可选 - 默认为 自动): 当设置hibernate.use_outer_join的时候,对这个关联允许外连接抓取。
(6) update, insert (可选 - defaults to true) 指定对应的字段是否在用于UPDATE 和/或 INSERT的SQL语句中包含。如果二者都是false,则这是一个纯粹的“外源性(derived)”关联,它的值是通过映射到同一个(或多个)字段的某些其他属性得到的,或者通过trigger(除法器),或者是其他程序。
(7) property-ref: (可选) 指定关联类的一个属性,这个属性将会和本外键相对应。如果没有指定,会使用对方关联类的主键。
(8) access (可选 - 默认是 property): Hibernate用来访问属性的策略。
cascade 属性允许下列值: all, save-update, delete, none。设置除了none以外的其它值会传播特定的操作到关联的(子)对象中。参见后面的“Lifecycle Objects(自动管理生命周期的对象)”。
outer-join参数允许下列三个不同值:
auto (默认) 使用外连接抓取关联(对象),如果被关联的对象没有代理(proxy)
true 一直使用外连接来抓取关联
false 永远不使用外连接来抓取关联
Hibernate 一对一连接表双向关联
一、模型介绍
一个人(Person)对应一个地址(Address)。
二、实体(省略getter、setter方法)
public class Person11tab_sx {
private int personid;
private String name;
private int age;
private Address11tab_sx address11tab_sx;
public class Address11tab_sx {
private int addressid;
private String addressdetail;
private Person11tab_sx person11tab_sx;
三、表模型
person_11tab_sx;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
join_11tab_sx;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| addressid | int(11) | NO | UNI | | |
| personid | int(11) | NO | PRI | | |
+-----------+---------+------+-----+---------+-------+
address_11tab_sx;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
四、映射方法
<hibernate-mapping>
<class name="com.lavasoft.sx._1_1_tab.Person11tab_sx" table="PERSON_11tab_sx">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<join table="join_11tab_sx"
optional="true">
<key column="personid"
unique="true"/>
<many-to-one name="address11tab_sx"
column="addressid"
not-null="true"
unique="true"/>
</join>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.sx._1_1_tab.Address11tab_sx" table="ADDRESS_11tab_sx">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
<join table="join_11tab_sx"
optional="true"
inverse="true">
<key column="addressid"
unique="true"/>
<many-to-one name="person11tab_sx" column="personid"
not-null="true" unique="true"/>
</join>
</class>
</hibernate-mapping>
五、测试方法
public class Test_11tab_sx {
public static void main(String[] args){
Address11tab_sx add = new Address11tab_sx();
Person11tab_sx p = new Person11tab_sx();
add.setAddressdetail("。。。。。");
p.setAge(12);
p.setName("。。。。。");
add.setPerson11tab_sx(p);
p.setAddress11tab_sx(add);
Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(p);
session.saveOrUpdate(add);
tx.commit();
HibernateUtil.closeSession();
}
}
六、测试结果
1) :正常保存.
session.saveOrUpdate(p);
session.saveOrUpdate(add);
Hibernate: insert into PERSON_11tab_sx (name, age) values (?, ?)
Hibernate: insert into ADDRESS_11tab_sx (addressdetail) values (?)
Hibernate: insert into join_11tab_sx (addressid, personid) values (?, ?)
Hibernate 一对一主键双向关联
一、模型介绍
一个人(Person)对应一个地址(Address)。
二、实体(省略getter、setter方法)
public class Person11pk_sx {
private int personid;
private String name;
private int age;
private Address11pk_sx address11pk_sx;
public class Address11pk_sx {
private int addressid;
private String addressdetail;
private Person11pk_sx person11pk_sx;
三、表模型
person_11pk_sx;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL
; | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
address_11pk_sx;
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| addressid | int(11) | NO | PRI | | |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+-------+
四、映射方法
<hibernate-mapping>
<class name="com.lavasoft.sx._1_1_pk.Person11pk_sx" table="PERSON_11pk_sx">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<one-to-one name="address11pk_sx" cascade="all"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.sx._1_1_pk.Address11pk_sx" table="ADDRESS_11pk_sx">
<id name="addressid">
<generator class="foreign">
<param name="property">person11pk_sx</param>
</generator>
</id>
<property name="addressdetail"/>
<!--表明主键由关联属性生成-->
<one-to-one name="person11pk_sx" cascade="all" constrained="true"/>
</class>
</hibernate-mapping>
五、测试方法
public class Test_11pk_sx {
public static void main(String[] args){
Address11pk_sx add = new Address11pk_sx();
Person11pk_sx p = new Person11pk_sx();
add.setAddressdetail("。。。。。");
p.setAge(12);
p.setName("。。。。。");
add.setPerson11pk_sx(p);
p.setAddress11pk_sx(add);
Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction();
session.save(p);
// session.save(add);
tx.commit();
HibernateUtil.closeSession();
}
}
六、测试结果
1) :正常保存.
session.save(p);
// session.save(add);
Hibernate: insert into PERSON_11pk_sx (name, age) values (?, ?)
Hibernate: insert into ADDRESS_11pk_sx (addressdetail, addressid) values (?, ?)
Hibernate 一对一外键双向关联
一、模型介绍
一个人(Person)对应一个地址(Address)。
二、实体(省略getter、setter方法)
public class Person11fk_sx {
private int personid;
private String name;
private int age;
private Address11fk_sx address11fk_sx;
public class Address11fk_sx {
private int addressid;
private String addressdetail;
private Person11fk_sx person11fk_sx;
三、表模型
person_11fk_sx;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
address_11fk_sx;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
| person11fk_sx | int(11) | YES | UNI | NULL | |
+---------------+--------------+------+-----+---------+----------------+
四、映射方法
<hibernate-mapping>
<class name="com.lavasoft.sx._1_1_fk.Person11fk_sx" table="PERSON_11fk_sx">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<one-to-one name="address11fk_sx"
cascade="all"
property-ref="person11fk_sx"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.sx._1_1_fk.Address11fk_sx" table="ADDRESS_11fk_sx">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
<!--unique="true"表示本实体是"1"-->
<many-to-one name="person11fk_sx" unique="true"/>
</class>
</hibernate-mapping>
五、测试方法
public class Test_11fk_sx {
public static void main(String[] args) {
Address11fk_sx add = new Address11fk_sx();
Person11fk_sx p = new Person11fk_sx();
add.setAddressdetail("。。。。。");
p.setAge(12);
p.setName("。。。。。");
add.setPerson11fk_sx(p);
p.setAddress11fk_sx(add);
Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction();
session.save(p);
tx.commit();
HibernateUtil.closeSession();
}
}
六、测试结果
1) :正常保存.
session.save(p);
Hibernate: insert into PERSON_11fk_sx (name, age) values (?, ?)
Hibernate: insert into ADDRESS_11fk_sx (addressdetail, person11fk_sx) values (?, ?)
Hibernate 多对多单向关联
一、模型介绍
多个人(Person)对应多个地址(Address)。
一个人可对应多个地址,一个地址也可以对应多个人。
二、实体(省略getter、setter方法)
public class Personnn {
private int personid;
private String name;
private int age;
private Set addresses=new HashSet();
public class Addressnn {
private int addressid;
private String addressdetail;
三、表模型
person_nn;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
join_nn;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| personid | int(11) | NO | PRI | | |
| addressid | int(11) | NO | PRI | | |
+-----------+---------+------+-----+---------+-------+
person_nn;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
四、映射方法
<hibernate-mapping>
<class name="com.lavasoft.dx._n_n.Personnn" table="PERSON_nn">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<!--映射集合属性,join_1ntab是连接表表名-->
<set name="addresses"
table="join_nn"
>
<!--“column="personid"”确定PERSON_1ntab表关联到连接表的外键列名-->
<key column="personid"/>
<!--“column="addressid"”关联PERSON_1ntab表的Address1ntab对象的id在连接表中的列名-->
<many-to-many
column="addressid"
class="com.lavasoft.dx._n_n.Addressnn"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.dx._n_n.Addressnn" table="ADDRESS_nn">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
</class>
</hibernate-mapping>
五、测试方法
public class Test_nn {
public static void main(String[] args){
Addressnn add1=new Addressnn();
Addressnn add2=new Addressnn();
Addressnn add3=new Addressnn();
Personnn p1=new Personnn();
Personnn p2=new Personnn();
add1.setAddressdetail("。。。。。");
add2.setAddressdetail("。。。。。");
add3.setAddressdetail("。。。。。");
p1.setName("。。。。。");
p1.setAge(30);
p2.setName("。。。。。");
p2.setAge(50);
p1.getAddresses().add(add1);
p1.getAddresses().add(add2);
p2.getAddresses().add(add2);
p2.getAddresses().add(add3);
Session session= HibernateUtil.getCurrentSession();
Transaction tx=session.beginTransaction();
session.save(add1);
session.save(add2);
session.save(add3);
session.save(p1);
session.save(p2);
tx.commit();
HibernateUtil.closeSession();
}
}
六、测试结果
1) :正常保存.
session.save(add1);
session.save(add2);
session.save(add3);
session.save(p1);
session.save(p2);
Hibernate: insert into ADDRESS_nn (addressdetail) values (?)
Hibernate: insert into ADDRESS_nn (addressdetail) values (?)
Hibernate: insert into ADDRESS_nn (addressdetail) values (?)
Hibernate: insert into PERSON_nn (name, age) values (?, ?)
Hibernate: insert into PERSON_nn (name, age) values (?, ?)
Hibernate: insert into join_nn (personid, addressid) values (?, ?)
Hibernate: insert into join_nn (personid, addressid) values (?, ?)
Hibernate: insert into join_nn (personid, addressid) values (?, ?)
Hibernate: insert into join_nn (personid, addressid) values (?, ?)
Hibernate 多对一连接表单向关联
一、模型介绍
多个人(Person)对应一个地址(Address)。
二、实体(省略getter、setter方法)
public class Personn1tab {
private int personid;
private String name;
private int age;
private Addressn1tab addressn1tab;
public class Addressn1tab {
private int addressid;
private String addressdetail;
三、表模型
address_n1tab;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
join_n1tab;
+------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| personid | int(11) | NO | PRI | | |
| addressn1tab | int(11) | YES | MUL | NULL | |
+------------+---------+------+-----+---------+-------+
person_n1tab;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
四、映射方法
<hibernate-mapping>
<class name="com.lavasoft.dx._n_1_tab.Personn1tab" table="PERSON_n1tab">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<!--使用join元素显式确定链接表-->
<join table="join_n1tab">
<!--映射关联所用的外键-->
<key column="personid"/>
<many-to-one name="addressn1tab"/>
</join>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.dx._n_1_tab.Addressn1tab" table="ADDRESS_n1tab">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
</class>
</hibernate-mapping>
五、测试方法
public class Test_n1tab {
public static void main(String[] args){
Personn1tab p1=new Personn1tab();
Personn1tab p2=new Personn1tab();
p1.setAge(21);
p1.setName("p1");
p2.setAge(23);
p2.setName("p2");
Addressn1tab add=new Addressn1tab();
add.setAddressdetail("。。。。。");
p1.setAddressn1tab(add);
p2.setAddressn1tab(add);
Session session=HibernateUtil.getCurrentSession();
Transaction tx=session.beginTransaction();
session.save(add);
session.save(p1);
session.save(p2);
tx.commit();
HibernateUtil.closeSession();
}
}
六、测试结果
1) :正常保存. 推荐这么干!
session.save(p1);
session.save(p2);
Hibernate: insert into ADDRESS_n1tab (addressdetail) values (?)
Hibernate: insert into PERSON_n1tab (name, age) values (?, ?)
Hibernate: insert into join_n1tab (address11fk, personid) values (?, ?)
Hibernate: insert into PERSON_n1tab (name, age) values (?, ?)
Hibernate: insert into join_n1tab (address11fk, personid) values (?, ?)
2) :正常保存.
session.save(p1);
session.save(p2);
session.save(add);
Hibernate: insert into PERSON_n1tab (name, age) values (?, ?)
Hibernate: insert into join_n1tab (address11fk, personid) values (?, ?)
Hibernate: insert into PERSON_n1tab (name, age) values (?, ?)
Hibernate: insert into join_n1tab (address11fk, personid) values (?, ?)
Hibernate 多对一外键单向关联
一、模型介绍
多个人(Person)对应一个地址(Address)。
二、实体(省略getter、setter方法)
public class Personn1fk {
private int personid;
private String name;
private int age;
private Addressn1fk addressn1fk;
public class Addressn1fk {
private int addressid;
private String addressdetail;
三、表模型
address_n1kf;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
person_n1kf;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| addressId | int(11) | YES | MUL | NULL | |
+-----------+--------------+------+-----+---------+----------------+
四、映射方法
<hibernate-mapping>
<class name="com.lavasoft.dx._n_1_fk.Personn1fk" table="PERSON_n1fk">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<!--用来映射关联PO column是Address在该表中的外键列名-->
<many-to-one name="addressn1fk" column="addressId"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.dx._n_1_fk.Addressn1fk" table="ADDRESS_n1fk">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
</class>
</hibernate-mapping>
五、测试方法
public class Test_n1fk {
public static void main(String[] args){
Personn1fk p1=new Personn1fk();
Personn1fk p2=new Personn1fk();
p1.setAge(21);
p1.setName("p1");
p2.setAge(23);
p2.setName("p2");
Addressn1fk add=new Addressn1fk();
add.setAddressdetail("。。。。。");
p1.setAddressn1fk(add);
p2.setAddressn1fk(add);
Session session=HibernateUtil.getCurrentSession();
Transaction tx=session.beginTransaction();
session.save(add);
session.save(p1);
session.save(p2);
tx.commit();
HibernateUtil.closeSession();
}
}
六、测试结果
1) :正常保存. 推荐这么干!
session.save(p1);
session.save(p2);
Hibernate: insert into ADDRESS_n1kf (addressdetail) values (?)
Hibernate: insert into PERSON_n1kf (name, age, addressId) values (?, ?, ?)
Hibernate: insert into PERSON_n1kf (name, age, addressId) values (?, ?, ?)
2) :正常保存.
session.save(p1);
session.save(p2);
session.save(add);
Hibernate: insert into PERSON_n1kf (name, age, addressId) values (?, ?, ?)
Hibernate: insert into PERSON_n1kf (name, age, addressId) values (?, ?, ?)
Hibernate: insert into ADDRESS_n1kf (addressdetail) values (?)
Hibernate: update PERSON_n1kf set name=?, age=?, addressId=? where personid=?
Hibernate: update PERSON_n1kf set name=?, age=?, addressId=? where personid=?
3) :正常保存.
session.save(add);
// session.save(p1);
// session.save(p2);
Hibernate: insert into ADDRESS_n1kf (addressdetail) values (?)
4) : 发生异常,不能保存.
// session.save(add);
session.save(p1);
session.save(p2);
Hibernate: insert into PERSON_n1kf (name, age, addressId) values (?, ?, ?)
Hibernate: insert into PERSON_n1kf (name, age, addressId) values (?, ?, ?)
Exception in thread "main" org.hibernate.TransientObjectException: com.lavasoft.dx._n_1_
Hibernate 一对多连接表单向关联
一、模型介绍
一个人(Person)对应多个地址(Address),比如家庭地址、公司地址。
二、实体(省略getter、setter方法)
public class Person1ntab {
private int personid;
private String name;
private int age;
private Set addresses=new HashSet();
public class Address1nfk implements Serializable {
private int addressid;
private String addressdetail;
三、表模型
join_1ntab;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| personid | int(11) | NO | PRI | | |
| addressid | int(11) | NO | PRI | | |
+-----------+---------+------+-----+---------+-------+
person_1ntab;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
address_1ntab;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
四、映射方法
<hibernate-mapping>
<class name="com.lavasoft.dx._1_n_tab.Person1ntab" table="PERSON_1ntab">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<!--映射集合属性,join_1ntab是连接表表名-->
<set name="addresses"
table="join_1ntab"
>
<!--“column="personid"”确定PERSON_1ntab表关联到连接表的外键列名-->
<key column="personid"/>
<!--“column="addressid"”关联PERSON_1ntab表的Address1ntab对象的id在连接表中的列名-->
<!--“unique="true"表示1-N,Person1ntab是1,Address1ntab是多”-->
<many-to-many
column="addressid"
unique="true"
class="com.lavasoft.dx._1_n_tab.Address1ntab"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.dx._1_n_tab.Address1ntab" table="ADDRESS_1ntab">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
</class>
</hibernate-mapping>
六、测试方法
public class Test_1ntab {
public static void main(String[] args){
Address1ntab add1=new Address1ntab();
Address1ntab add2=new Address1ntab();
Address1ntab add3=new Address1ntab();
Person1ntab p1=new Person1ntab();
Person1ntab p2=new Person1ntab();
add1.setAddressdetail("。。。。。");
add2.setAddressdetail("。。。。。");
add3.setAddressdetail("。。。。。");
p1.setName("。。。。。");
p1.setAge(30);
p2.setName("。。。。。");
p2.setAge(50);
p1.getAddresses().add(add1);
p1.getAddresses().add(add2);
//p2.getAddresses().add(add2);
p2.getAddresses().add(add3);
Session session= HibernateUtil.getCurrentSession();
Transaction tx=session.beginTransaction();
session.save(add1);
session.save(add2);
session.save(add3);
session.save(p1);
session.save(p2);
tx.commit();
HibernateUtil.closeSession();
}
}
六、测试结果
1) :正常保存.
session.save(add1);
session.save(add2);
session.save(add3);
session.save(p1);
session.save(p2);
Hibernate: insert into PERSON_1nfk (name, age) values (?, ?)
Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)
Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)
Hibernate: update ADDRESS_1nfk set personid=? where addressid=?
Hibernate: update ADDRESS_1nfk set personid=? where addressid=?
Hibernate 一对多外键单向关联
这种情况很少见,但Hibernate同样允许采用连接表关联1-1.有连接表的1-1同样只需要将N-1的many-to-one元素增加unique="true"属性即可。
一、模型介绍
一个人(Person)对应多个地址(Address),比如家庭地址、公司地址。
二、实体(省略getter、setter方法)
public class Person1nfk implements Serializable {
private int personid;
private String name;
private int age;
private Set addresses=new HashSet();
public class Address1nfk implements Serializable {
private int addressid;
private String addressdetail;
三、表模型
address_1nfk;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
| personid | int(11) | YES | MUL | NULL | |
+---------------+--------------+------+-----+---------+----------------+
person_1nfk;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
四、映射方法:在实体类Person里面添加Address的集合,即可形成一对多关系。
<!--映射集合属性,关联到持久化类,inverse="false"表示主控端在Person1nfk端,lazy="false"表示不采用延迟加载-->
<set name="addresses"
table="ADDRESS_1nfk"
cascade="all"
>
<!--确定关联的外键列-->
<key column="personid"/>
<!--用以映射到关联类属性-->
<one-to-many class="com.lavasoft.dx._1_n_fk.Address1nfk"/>
</set>
<hibernate-mapping>
<class name="com.lavasoft.dx._1_n_fk.Person1nfk" table="PERSON_1nfk">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<!--映射集合属性,关联到持久化类,inverse="false"表示主控端在Person1nfk端,lazy="false"表示不采用延迟加载-->
<set name="addresses"
table="ADDRESS_1nfk"
cascade="all"
>
<!--确定关联的外键列-->
<key column="personid"/>
<!--用以映射到关联类属性-->
<one-to-many class="com.lavasoft.dx._1_n_fk.Address1nfk"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.dx._1_n_fk.Address1nfk" table="ADDRESS_1nfk">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
</class>
</hibernate-mapping>
五、测试方法
public class Test_1nfk {
public static void main(String[] args){
Address1nfk add1=new Address1nfk();
Address1nfk add2=new Address1nfk();
Person1nfk p=new Person1nfk();
add1.setAddressdetail("。。。。。。");
add2.setAddressdetail("。。。。。。");
p.setName("。。。。。");
p.setAge(30);
p.getAddresses().add(add1);
p.getAddresses().add(add2);
Session session= HibernateUtil.getCurrentSession();
Transaction tx=session.beginTransaction();
session.save(add1);
session.save(add2);
session.save(p);
tx.commit();
HibernateUtil.closeSession();
}
}
六、测试结果
1) :正常保存.
// session.save(add1);
// session.save(add2);
session.save(p);
Hibernate: insert into PERSON_1nfk (name, age) values (?, ?)
Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)
Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)
Hibernate: update ADDRESS_1nfk set personid=? where addressid=?
Hibernate: update ADDRESS_1nfk set personid=? where addressid=?
2) :正常保存.
session.save(add1);
session.save(add2);
session.save(p);
Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)
Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)
Hibernate: insert into PERSON_1nfk (name, age) values (?, ?)
Hibernate: update ADDRESS_1nfk set personid=? where addressid=?
Hibernate: update ADDRESS_1nfk set personid=? where addressid=?
3) :正常保存.
session.save(add1);
session.save(add2);
// session.save(p);
Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)
Hibernate: insert into ADDRESS_1nfk (addressdetail) values (?)
Hibernate 一对一连接表单向关联
这种情况很少见,但Hibernate同样允许采用连接表关联1-1.有连接表的1-1同样只需要将N-1的many-to-one元素增加unique="true"属性即可。
一、模型介绍
一个人(Person)对应一个地址(Address)。
二、实体(省略getter、setter方法)
public class Person11tab {
private int personid;
private String name;
private int age;
private Address11tab address11tab;
public class Address11tab {
private int addressid;
private String addressdetail;
三、表模型
address_11tab;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
join_11tab;
+--------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| personid | int(11) | NO | PRI | | |
| address11tab | int(11) | YES | UNI | NULL | |
+--------------+---------+------+-----+---------+-------+
person_11tab;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
四、映射方法:在Person中添加Address属性,映射配置为:
<!--使用join元素显式确定链接表-->
<join table="join_11tab">
<key column="personid"/>
<!--映射1-1关联属性,其中unique=“true”属性确定为“1-1”-->
<many-to-one name="address11tab" unique="true"/>
</join>
<hibernate-mapping>
<class name="com.lavasoft.dx._1_1_tab.Person11tab" table="PERSON_11tab">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<!--使用join元素显式确定链接表-->
<join table="join_11tab">
<key column="personid"/>
<!--映射1-1关联属性,其中unique=“true”属性确定为“1-1”-->
<many-to-one name="address11tab" unique="true"/>
</join>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.dx._1_1_tab.Address11tab" table="ADDRESS_11tab">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
</class>
</hibernate-mapping>
五、测试方法
public class Test_11tab {
public static void main(String[] args){
Person11tab p1=new Person11tab();
p1.setAge(21);
p1.setName("p1");
Address11tab add1=new Address11tab();
add1.setAddressdetail("。。。。。");
p1.setAddress11tab(add1);
Session session= HibernateUtil.getCurrentSession();
Transaction tx=session.beginTransaction();
session.save(add1);
session.save(p1);
tx.commit();
HibernateUtil.closeSession();
}
}
六、测试结果
1) :正常保存. 推荐这么干!
session.save(add1);
session.save(p1);
Hibernate: insert into ADDRESS_11tab (addressdetail) values (?)
Hibernate: insert into PERSON_11tab (name, age) values (?, ?)
Hibernate: insert into join_11tab (address11tab, personid) values (?, ?)
2) :正常保存.
session.save(p1);
session.save(add1);
Hibernate: insert into PERSON_11tab (name, age) values (?, ?)
Hibernate: insert into join_11tab (address11tab, personid) values (?, ?)
Hibernate: insert into ADDRESS_11tab (addressdetail) values (?)
Hibernate: update join_11tab set address11tab=? where personid=?
3) :正常保存.
// session.save(p1);
session.save(add1);
Hibernate: insert into ADDRESS_11tab (addressdetail) values (?)
4) : 发生异常,不能保存.
session.save(p1);
// session.save(add1);
Hibernate: insert into PERSON_11tab (name, age) values (?, ?)
Hibernate: insert into join_11tab (address11tab, personid) values (?, ?)
Exception in thread "main" org.hibernate.TransientObjectException: com.lavasoft.dx._1_1_
Hibernate 一对一主键单向关联
1-1的关联可以基于主键关联,但基于主键关联的持久化类不能拥有自己的主键生成策略,它的主键由关联类负责生成。另外,另外,增加one-to-one元素来关联属性,必须为one-to-one元素增加constrained="true"属性,表明该类主键由关联类生成。
一、模型介绍
一个人(Person)对应一个地址(Address)。
二、实体(省略getter、setter方法)
public class Person11pk {
private int personid;
private String name;
private int age;
private Address11pk address11pk;
public class Address11pk {
private int addressid;
private String addressdetail;
三、表模型
address_11pk;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
person_11pk;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| personid | int(11) | NO | PRI | | |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
四、映射方法:在Person中配置id生成策略为:
<id name="personid">
<!--基于主键关联时,主键生成策略是foreign,表明根据关联类生成主键-->
<generator class="foreign">
<!--关联持久化类的属性名-->
<param name="property">address11pk</param>
</generator>
</id>
......
<!--用于映射1-1关联-->
<one-to-one name="address11pk" constrained="true"/>
<hibernate-mapping>
<class name="com.lavasoft.dx._1_1_pk.Person11pk" table="PERSON_11pk">
<id name="personid" column="presonid">
<!--基于主键关联时,主键生成策略是foreign,表明根据关联类生成主键-->
<generator class="foreign">
<!--关联持久化类的属性名-->
<param name="property">address11pk</param>
</generator>
</id>
<property name="name"/>
<property name="age"/>
<!--用于映射1-1关联-->
<one-to-one name="address11pk" constrained="true"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.dx._1_1_pk.Address11pk" table="ADDRESS_11pk">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
</class>
</hibernate-mapping>
五、测试方法
public class Test_11pk {
public static void main(String[] args){
Person11pk p1=new Person11pk();
p1.setAge(21);
p1.setName("p1");
Address11pk add1=new Address11pk();
add1.setAddressdetail("。。。。。");
p1.setAddress11pk(add1);
Session session= HibernateUtil.getCurrentSession();
Transaction tx=session.beginTransaction();
session.save(add1);
session.save(p1);
tx.commit();
HibernateUtil.closeSession();
}
}
六、测试结果
1) :正常保存. 推荐这么干!
session.save(add1);
session.save(p1);
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
2) :正常保存.
session.save(p1);
session.save(add1);
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
Hibernate: update PERSON_11fk set name=?, age=?, addressId=? where personid=?
3) :正常保存.
// session.save(p1);
session.save(add1);
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
4) : 发生异常,不能保存.
session.save(p1);
// session.save(add1);
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
Exception in thread "main" org.hibernate.TransientObjectException: com.lavasoft.dx._1_1_
Hibernate 一对一外键单向关联
事实上,单向1-1与N-1的实质是相同的,1-1是N-1的特例,单向1-1与N-1的映射配置也非常相似。只需要将原来的many-to-one元素增加unique="true"属性,用于表示N的一端也必须是唯一的,在N的一端增加了唯一的约束,即成为单向1-1。基于外键的单向1-1的配置将与无连接表N-1关联的many-to-one增加unique="true"属性即可。
一、模型介绍
一个人(Person)对应一个地址(Address)。
二、实体(省略getter、setter方法)
public class Person11fk {
private int personid;
private String name;
private int age;
private Address11fk address11fk;
public class Address11fk {
private int addressid;
private String addressdetail;
三、表模型
address_11fk;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
person_11fk;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| addressId | int(11) | YES | UNI | NULL | |
+-----------+--------------+------+-----+---------+----------------+
四、映射方法:
在Person中添加Address属性,映射配置为:
<!--用来映射关联PO column是Address在该表中的外键列名,增加unique变成“1-1”-->
<many-to-one name="address11fk" column="addressId" unique="true"/>
<hibernate-mapping>
<class name="com.lavasoft.dx._1_1_fk.Address11fk" table="ADDRESS_11fk">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.dx._1_1_fk.Person11fk" table="PERSON_11fk">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<!--用来映射关联PO column是Address在该表中的外键列名,增加unique变成“1-1”-->
<many-to-one name="address11fk" column="addressId" unique="true"/>
</class>
</hibernate-mapping>
五、测试方法
public class Test_11fk {
public static void main(String[] args){
Person11fk p1=new Person11fk();
p1.setAge(21);
p1.setName("p1");
Address11fk add1=new Address11fk();
add1.setAddressdetail("。。。。。");
p1.setAddress11fk(add1);
Session session= HibernateUtil.getCurrentSession();
Transaction tx=session.beginTransaction();
session.save(add1);
session.save(p1);
tx.commit();
HibernateUtil.closeSession();
}
}
六、测试结果
1) :正常保存. 推荐这么干!
session.save(add1);
session.save(p1);
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
2) :正常保存.
session.save(p1);
session.save(add1);
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
Hibernate: update PERSON_11fk set name=?, age=?, addressId=? where personid=?
3) :正常保存.
// session.save(p1);
session.save(add1);
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
4) : 发生异常,不能保存.
session.save(p1);
// session.save(add1);
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
Exception in thread "main" org.hibernate.TransientObjectException: com.lavasoft.dx._1_1_fk.Address11fk
Hibernate关联关系映射实例速查
Hibernate的映射关系很多,也比较复杂,也很容易忘记。这个基本上占据了Hibernate学习的七成时间。熟悉这些映射模型,需要大量的实践才能掌握。下面是我对Hibernate关联关系映射的一个总结,放到blog上一是自己查看方便,二来也可以和更多Hibernate开发人员交流分享。希望各位多多留言哦:)。
本文主要参考夏昕翻译的“Hibernate参考文档 V3.12”,也在附件中给出了。
本文的模块较多,映射关系部分分为一下模块:
Hibernate关联关系映射目录
│
├─单向关联
│ ├─ 一对一外键单向关联
│ ├─ 一对一主键单向关联
│ ├─ 一对一连接表单向关联
│ ├─ 一对多
发表评论
-
Hibernate的拦截器和监听器
2009-11-02 14:13 2104最近项目需要,用到了Hibernate的拦截器和监听器,有些小 ... -
深入 理解 Statement 和 PreparedStatement
2009-07-20 10:26 2435翻译:陈先波(turbochen@163.com)阅读原文:h ... -
开发者请注意oracle jdbc的resultSet.last()方法的效率问题
2009-07-20 10:20 2770在使用Hibernate分页器时 ... -
深入理解JDBC Scrollable ResultSet
2009-07-20 10:06 3934JDBC2.0后提出了三种不 ... -
hibernate ScrollableResults 中CacheMode 和 ScrollMode 介绍
2009-07-20 09:26 2608CacheMode.GET - 从二级缓存中读取数据,仅在数 ... -
Hibernate中Criteria的完整用法
2009-06-21 20:13 1201Hibernate中Criteria的完整用法2008年07 ... -
Hibernate 深入研究之 Criteria
2009-06-21 17:02 1526最近在项目中使用 Spring 和 Hibernate 进行 ... -
Hibernate3的DetachedCriteria支持
2009-06-20 19:07 2769Hibernate3支持DetachedCriteria,这是 ... -
Hibernate Annotation几种关联映射
2009-04-26 02:10 1842Hibernate Annotation几种关联映射 一对一 ... -
HibernateTemplate中HibernateCallback的事务
2009-03-08 03:41 2352目的:使用HibernateTemplate执行execute ... -
关于hibernate的缓存和CRUD
2009-03-08 02:05 1125hibernate作为一种现在 ... -
EJB3/JPA Annotations 学习
2009-03-07 15:43 2415Document file and example: Hibe ... -
java回调机制及Hibernate中的HibernateTemplate实现
2009-03-06 20:42 2008谈谈回调吧,以前学java的时候居然没接触到这个词汇,汗,最近 ... -
hibernate大对象类型映射
2009-02-02 23:28 1804hibernate大对象类型映射 ... -
Hibernate二级缓存使用手册
2009-02-02 23:16 18571启用Hibernate二级缓存 Hiberna ... -
Hibernate 多对多单向关联
2009-02-02 02:17 1546Hibernate 多对多单向关联 一、模型介绍 多个人 ...
相关推荐
在Java世界中,Hibernate是一个非常流行的对象关系映射(ORM)框架,它允许开发者使用面向对象的方式来操作数据库,而无需关心底层SQL语句。本文将深入探讨Hibernate中的九种对象关系映射策略,并结合数据库表结构...
**Hibernate对象关系详解** 在Java世界中,Hibernate作为一款强大的对象关系映射(ORM)框架,使得开发者可以方便地在Java对象与数据库表之间进行数据操作。本篇将深入探讨Hibernate中的对象关系映射,包括一对一...
Hibernate 配置文件详解 Hibernate 是一个流行的 ORM(Object-Relational Mapping)框架,用于简化 Java 应用程序中的数据库交互。 Hibernate 的配置文件是其核心组件之一,用于定义 Hibernate 的运行期参数。下面...
标题"hibernate--5.Hibernate配置文件详解-2"暗示我们将继续上一节的内容,进一步深入理解Hibernate配置文件的各个方面。通常,Hibernate的配置文件以`hibernate.cfg.xml`的形式存在,它是项目初始化和连接数据库的...
【Hibernate缓存深入详解】 在Java的持久化框架Hibernate中,缓存机制是提升系统性能的关键因素。它位于Hibernate应用和数据库之间,减少了对数据库的直接访问,从而提高了应用程序的运行速度。缓存中存储的是...
**hibernate包详解** Hibernate 是一款开源的对象关系映射(ORM)框架,它为Java开发者提供了一种在关系数据库和面向对象编程之间架起桥梁的解决方案。通过使用Hibernate,开发人员可以避免直接编写SQL语句,而是用...
**Hibernate详解** Hibernate是一款强大的Java持久化框架,它简化了数据库与Java对象之间的交互,使得开发者无需关注SQL的编写,可以更加专注于业务逻辑。本文将深入探讨Hibernate的核心概念、主要功能以及如何在...
### Hibernate 参数配置详解 #### 一、概述 Hibernate 是一款流行的 Java 持久层框架,它通过提供一套丰富的 API 和配置选项来简化数据库访问。本文档将深入探讨 Hibernate 的核心配置参数及其功能,帮助开发者更...
在MyEclipse中,选择Hibernate Specification为Hibernate 2,并选择对应的库。 6. **配置SessionFactory**: 在Spring的配置文件applicationContext.xml中,定义SessionFactory bean,这是与Hibernate交互的关键。...
Hibernate 是一个流行的对象关系映射(ORM)框架,它允许开发者使用Java对象来操作数据库,消除了手动编写SQL语句的需要。在本篇中,我们将深入探讨Hibernate的配置,特别是涉及一对一、一对多和多对多关系的配置。 ...
### hibernate.properties详解 在Java开发环境中,Hibernate作为一款优秀的对象关系映射(ORM)框架,简化了数据库操作。为了使Hibernate能够正确地与数据库进行交互,必须通过配置文件来指定连接信息及运行时参数...
【hibernate问题详解】 Hibernate是一款强大的Java对象关系映射(ORM)框架,它通过将数据库操作转换为对Java对象的操作,简化了开发过程。在理解Hibernate时,我们需要对比其他两种常见的数据访问技术:JDBC和...
Hibernate是一款强大的Java持久化框架,它使得开发者可以方便地将对象模型映射到关系数据库,极大地简化了数据库操作。在Hibernate中,注解是用于替代传统的XML配置文件,以元数据的形式标注在类、属性或者方法上,...
### Hibernate映射关联详解 #### 一、理解一对多双向关联关系 在关系型数据库设计中,一对多关联是常见的数据组织方式之一。而在面向对象编程语言中,这种关系则通常通过集合(如Set或List)来实现。本文将重点...
映射文件定义了类的属性与数据库字段的对应关系,以及主键生成策略等。例如: ```xml <!-- 更多属性映射... --> ``` 四、注解映射 除了XML映射,Hibernate还支持使用Java注解进行映射。这样可以将...
12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...
一。实体Bean 每个持久化POJO类都是一个实体Bean, 通过在类的定义中使用 @Entity 注解来进行声明。 声明实体Bean @Entity public class Flight implements ...属性对应field类别,方法的 getXxx()对应property类别。
### Hibernate注解配置详解 #### 一、概述 Hibernate 是一款强大的对象关系映射(ORM)框架,它简化了Java应用程序与数据库之间的交互过程。在Hibernate中,通过使用注解来定义实体类与数据库表之间的映射关系,...
本篇文章将深入探讨Hibernate中的一对多、多对多和多对一等表关系映射的配置。 首先,我们来看基本的Hibernate配置文件`hibernate.cfg.xml`。这个文件用于定义数据库连接参数,例如数据库URL、驱动类、用户名和密码...