Hibernate.cfg.xml中的hibernate.show_sql是什么意思呢?
对象-关系映射文件中,id节点的子节点generator的class属性值为native时,主键生成方式是什么呢?
几种实体间的关系
一对一关系
一对多关系
多对多关系
一对一关系映射
应用场景: 公民 公民身份证
一对一关系映射ER图
应用场景: 公民 公民身份证
Citizen类 vs Card类 实现
package com.demo.model;
public class Citizen {
private int id;
private String name;
private Integer age;
/**
* 身份证(一对一关联)
*/
private Card card;
/*省略get与 set 方法*/ }
package com.demo.model;
public class Card {
private int id;
private String code;
private String organization;
/**
*所属公民(一对一关联)
*/
private Citizen citizen;
/*省略get与set方法。*/ }
映射文件:Citizen.hbm.xml
<hibernate-mapping
>
<class
name="com.demo.model.Citizen"
table="t_citizen"
>
<id
name="id"
column="id"
type="integer"
>
<generator class="increment">
</generator>
</id>
<one-to-one name=“card"
class="com.demo.model.Card"
cascade="all“
/>
映射文件:Card.hbm.xml
<class
name="com.demo.model.Card"
table="t_card"
>
<id
name="id"
column="id"
type="integer"
>
<generator
class="foreign">
<param
name="property">citizen
</param>
</generator>
</id>
<one-to-one name=“citizen"
class="com.demo.model. Citizen"
constrained="true“
/>
一对一关联测试运行
public void testOneToOneRelation(){
Citizen citizen = new Citizen();
citizen.setName("Tom");
citizen.setAge(20);
Card card = new Card();
card.setCode("65280119820612051");
card.setOrganization("JingLiu area");
//相互设置关联
citizen.setCard(card);
card.setCitizen(citizen);
//保存(由于citizen的配置中设置了cascade=“all”属性,因此,可以进行级联保存)
Transaction tx = session.beginTransaction();
session.save(citizen); //只需从主控方保存即可
tx.commit();
}
一对一(惟一外键)关联映射文件
映射文件:Citizen.hbm.xml
<hibernate-mapping
>
<class
name="com.demo.model.Citizen"
table="t_citizen"
>
<id
name="id"
column="id"
type="integer"
>
<generator class="increment">
</generator>
</id>
<many-to-one name=“card"
class="com.demo.model.Card"
cascade=“all"
unique=“true"
column=“card_id"
/>
映射文件:Card.hbm.xml
<class
name="com.demo.model.Card"
table="t_card"
>
<id
name="id"
column="id"
type="integer"
>
<generator class="increment“/>
</id>
<one-to-one name=“citizen"
class="com.demo.model.Citizen"
property-ref=“card"
/>
一对一(惟一外键)关联测试运行
public void testOneToOneRelation(){
Citizen citizen = new Citizen();
citizen.setName("Tom");
citizen.setAge(20);
Card card = new Card();
card.setCode("65280119820612051");
card.setOrganization("JingLiu area");
//相互设置关联
citizen.setCard(card);
card.setCitizen(citizen);
//保存(由于citizen的配置中设置了cascade=“all”属性,因此,可以进行级联保存)
Transaction tx = session.beginTransaction();
session.save(citizen); //只需从主控方保存即可
tx.commit();
}
一对多(单向)关系映射
应用场景: 用户 用户地址(家庭、公司)
User类 vs Address类的实现
package com.demo.model;
public class User {
private int id;
private String name;
private Integer age;
private Set<Address> addresses = new HashSet<Address>();
/*省略get与set方法。*/
}
package com.demo.model;
public class Address {
private int id;
private String address;
private String phone;
/*省略get与 set 方法*/
}
映射文件User.hbm.xml实现
<class
name="com.demo.model.User"
table="t_user"
>
<id
name="id"
column="id"
type="integer"
>
<generator class="increment">
</generator>
</id>
<set name="addresses"
table="t_address"
cascade="all"
>
<key column="user_id">
</key>
<one-to-many class="com.demo.model.Address"/>
</set>
映射文件Address.hbm.xml实现
<class
name="com.demo.model.Address"
table="t_address"
>
<id
name="id"
column="id"
type="integer"
>
<generator class="increment">
</generator>
</id>
<property
name="address"
type="java.lang.String"
column="address"
not-null="true"
/>
<property
name="phone"
type="java.lang.String"
column="phone"
/>
</class>
一对多(单向)关联的测试
public void testOneToManyRelation(){
User user;
user = (User)session.load(User.class, new Integer(1));
assertEquals("Tom",user.getName());
Address address = new Address();
address.setAddress("East RenMin Road 45");
address.setPhone("13551130670");
user.getAddresses().add(address);
//保存地址对象(通过主控对象进行级联更新)
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
}
一对多(双向)关系映射
应用场景: 用户 用户地址(家庭、公司)
在Address类中,加上user属性
package com.demo.model;
public class Address {
private int id;
private String address;
private User user;
/*省略get与 set 方法*/
}
映射文件User.hbm.xml实现
<class
name="com.demo.model.User"
table="t_user"
>
<id
name="id"
>
<generator class="increment">
</generator>
</id>
<set name="addresses"
table="t_address"
cascade="all“
inverse = “true”
>
<key column="user_id">
</key>
<one-to-many class="com.demo.model.Address"/>
</set>
映射文件Address.hbm.xml实现
<class
name="com.demo.model. Address"
table="t_address"
>
<many-to-one name="user"
class="com.demo.model.User"
cascade="none"
access="property"
column="user_id"
not-null="true"
>
</many-to-one>
一对多(双向)关联测试
public void testOneToManyRelation(){
User user;
user = (User)session.load(User.class, new Integer(1));
assertEquals("Tom",user.getName());
Address address = new Address();
address.setAddress("East RenMin Road 45");
address.setPhone("13551130670");
user.getAddresses().add(address);
address.setUser(user);
//保存用户对象(级联更新)
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
}
多对多关系映射
应用场景: 用户 用户组
User类 vs Group类的实现
package com.demo.model;
public class User {
private int id;
private String name;
private Set groups;
/*省略get与 set 方法*/
}
package com.demo.model;
public class Group {
private int id;
private String code;
private Set users;
/*省略get与 set 方法*/
}
映射文件User.hbm.xml
<class
name="com.demo.model. User"
table="t_user"
>
<set
name="groups"
table="t_user_group"
lazy="false"
inverse="false"
cascade="save-update"
>
<key
column=“user_id"
>
</key>
<many-to-many
class="com.demo.model.Group"
column=“group_id"
/>
</set>
映射文件Group.hbm.xml
<class
name="com.demo.model. Group"
table="t_group"
>
<set
name=“users"
table="t_user_group"
lazy="false"
inverse=“true"
cascade="save-update"
>
<key
column=“group_id"
>
</key>
<many-to-many
class="com.demo.model.User"
column=“user_id"
/>
</set>
多对多测试运行
User user1 = new User("Tom");
User user2 = new User("Jack");
Group group1 = new Group("admin");
Group group2 = new Group("develop");
group1.getUsers().add(user1);
group1.getUsers().add(user2);
group2.getUsers().add(user2);
user1.getGroup().add(group1);
user2.getGroup().add(group1);
user2.getGroup().add(group2);
try {
Transaction tx = session.beginTransaction();
//多对多关系必须同时对关联双方进行保存
session.save(user1);
session.save(user2);
session.save(group1);
session.save(group2);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
映射继承关系
Employee.hbm.xml配置
<class name="mypack.Employee" table="EMPLOYEES">
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>
<discriminator column=“employee_type" type="string" />
<subclass name=" com.demo.model.HourlyEmployee" discriminator-value=“H" >
<property name="rate" column="RATE" type="double" />
</subclass>
<subclass name=" com.demo.model.SalariedEmployee" discriminator-value=“S" >
<property name="salary" column="SALARY" type="double" />
</subclass>
</class>
多态查询
Session.find(“from Employee”)
Session.find(“from HourlyEmployee”)
Session.find(“from java.lang.Object”)