1.学习hibernate最重要的是关系,也就是*.hbm.xml文件的配置,不管struts,hibernate,spring还是标准的web应用,xml文件都是了解框架最直接的方法,它在某些程度上相当与注册表的功能。
2.pojo类(javabean的另一个商业名字),数据库表,他们都可以单独存在单独运行,没有问题。能够把他们有机联合起来的就是*.hbm.xml文件
3.下面来讲关系,我下面的关系都是双向的,这样好理解
3.1一对一
方式1:唯一的外键
<many-to-one unique="true"> (唯一的外键)
<one-to-one>
例子:
public class Address {
private Long id;
private String city;
private Customer customer;
...set/get 方法省略
public class Customer {
private Long id;
private String name;
private Address homeAddress;
...set/get 方法省略
Address.hbm.xml
<hibernate-mapping>
<class name="com.xx.one2one.foreignkey.Address"
table="ADDRESSES">
<id name="id" type="long" column="ID">
<generator class="increment" />
</id>
<property name="city" />
<one-to-one name="customer"
class="com.xx.one2one.foreignkey.Customer"/>
</class>
</hibernate-mapping>
Customer.hbm.xml
<hibernate-mapping >
<class name="com.xx.one2one.foreignkey.Customer">
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>
<property name="name" />
<many-to-one name="homeAddress"
class="com.xx.one2one.foreignkey.Address"
unique="true"/>
</class>
</hibernate-mapping>
方式2:共享主键
<one-to-one>
<one-to-one constrained="true"> (既是主键又是外键),主键生成方式需要用:foreign
例子:
public class Person {
private int id;
private String name;
private IdCard idCard = new IdCard();
...set/get 方法省略
public class IdCard {
private int id;
private int cardNb;
private Person pp ;
...set/get 方法省略
IdCard.hbm.xml
<hibernate-mapping>
<class name="com.xx.one2one.IdCard">
<id name="id">
<generator class="native" />
</id>
<property name="cardNb"></property>
<one-to-one name="pp" class="com.xx.one2one.Person"></one-to-one>
</class>
</hibernate-mapping>
Person.hbm.xml
<hibernate-mapping>
<class name="com.xx.one2one.Person">
<id name="id">
<generator class="foreign">
<param name="property">idCard</param>
</generator>
</id>
<property name="name"></property>
<one-to-one name="idCard" constrained="true"
class="com.xx.one2one.IdCard">
</one-to-one>
</class>
</hibernate-mapping>
3.2多对一
例子:
public class Dept{
private Integer deptno;
private String dname;
private String loc;
private Set emps = new HashSet();
...set/get 方法省略
public class Emp{
private Integer empno;
private Dept dept=new Dept();
private String ename;
...set/get 方法省略
dept.hbm.xml
<hibernate-mapping>
<class name="com.xx.one2many.Dept">
<id name="deptno">
<column name="DEPTNO"/>
<generator class="assigned" />
</id>
<property name="dname"/>
<property name="loc"/>
<set name="emps" inverse="true">
<key>
<column name="DEPTNO"/>
</key>
<one-to-many class="com.xx.one2many.Emp" />
</set>
</class>
</hibernate-mapping>
emp.hbm.xml
<hibernate-mapping>
<class name="com.xx.one2many.Emp">
<id name="empno" >
<column name="EMPNO" />
<generator class="assigned" />
</id>
<many-to-one name="dept" class="com.xx.one2many.Dept"/>
<property name="ename"/>
</class>
</hibernate-mapping>
3.3多对多
例子:
public class Student{
private String sid;
private String sname;
private Set<Department> departments = new HashSet<Department>(0);
...set/get 方法省略
public class Department{
private String did;
private String dname;
private Set<Student> students = new HashSet<Student>(0);
...set/get 方法省略
student.hbm.xml
<hibernate-mapping package="com.xx.manytomany">
<class name="Student" >
<id name="sid" >
<column name="SID" />
<generator class="assigned" />
</id>
<property name="sname"/>
<set name="departments" table="stu_dept">
<key>
<column name="SID" length="10" />
</key>
<many-to-many class="Department" column="DID"></many-to-many>
</set>
</class>
</hibernate-mapping>
department.hbm.xml
<hibernate-mapping package="com.xx.manytomany">
<class name="Department">
<id name="did"/>
<generator class="assigned" />
</id>
<property name="dname"/>
<set name="students" inverse="true" table="stu_dept">
<key>
<column name="DID"/>
</key>
<many-to-many column="SID" class="Student"></many-to-many>
</set>
</class>
</hibernate-mapping>
4.你的pojo有几个属性,*.hbm.xml就几个说明项.说明项就3种类型:主键,属性,关系
5.完全理解上面的关系后在看hibernate其它部分,如果自己觉得不好理解,其实有个笨但有效的方法,那就是背下来,我对学习新技术的核心部分就采取这种方法,直接有效
6.强烈推荐看原版的hibernate-reference;hibernate in action,感觉就是不一样。
2.pojo类(javabean的另一个商业名字),数据库表,他们都可以单独存在单独运行,没有问题。能够把他们有机联合起来的就是*.hbm.xml文件
3.下面来讲关系,我下面的关系都是双向的,这样好理解
3.1一对一
方式1:唯一的外键
<many-to-one unique="true"> (唯一的外键)
<one-to-one>
例子:
public class Address {
...set/get 方法省略
public class Customer {
...set/get 方法省略
Address.hbm.xml
<hibernate-mapping>
</hibernate-mapping>
Customer.hbm.xml
<hibernate-mapping >
</hibernate-mapping>
方式2:共享主键
<one-to-one>
<one-to-one constrained="true"> (既是主键又是外键),主键生成方式需要用:foreign
例子:
public class Person {
...set/get 方法省略
public class IdCard {
...set/get 方法省略
IdCard.hbm.xml
<hibernate-mapping>
</hibernate-mapping>
Person.hbm.xml
<hibernate-mapping>
</hibernate-mapping>
3.2多对一
例子:
public class Dept{
...set/get 方法省略
public class Emp{
...set/get 方法省略
dept.hbm.xml
<hibernate-mapping>
</hibernate-mapping>
emp.hbm.xml
<hibernate-mapping>
</hibernate-mapping>
3.3多对多
例子:
public class Student{
...set/get 方法省略
public class Department{
...set/get 方法省略
student.hbm.xml
<hibernate-mapping package="com.xx.manytomany">
</hibernate-mapping>
department.hbm.xml
<hibernate-mapping package="com.xx.manytomany">
</hibernate-mapping>
4.你的pojo有几个属性,*.hbm.xml就几个说明项.说明项就3种类型:主键,属性,关系
5.完全理解上面的关系后在看hibernate其它部分,如果自己觉得不好理解,其实有个笨但有效的方法,那就是背下来,我对学习新技术的核心部分就采取这种方法,直接有效
6.强烈推荐看原版的hibernate-reference;hibernate in action,感觉就是不一样。