Hibernate复习-关系映射

在公司好久不用hibernate了,怕hibernate忘记了,算是写个笔记等用的时候来查询

  1. hibernate配置
    这个主要参考hibernate的doc文档就行 有一个hibernate.cfg.xml文件,然后在配置一个hibernateUtil工具获得sessionFactory就行
    而且今后使用全部用注解,xml配置忽略了,像hdm.xml这种文件也不学习了

一对一

一对一单向

Person类

package com.hfview.bean;

import javax.persistence.*;

import org.hibernate.annotations.ForeignKey;
/** * 单个的人 * @author zhw * */

@Entity
@Table(name="T_Person")
public class Person {

    public int id;
    public String name;
    public String psw;
    private Card card;

    @OneToOne()
    @JoinColumn(name="cardId")//指定数据库里面card在peron表里面的名字
    public Card getCard() {
        return card;
    }

    public void setCard(Card card) {
        this.card = card;
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPsw() {
        return psw;
    }

    public void setPsw(String psw) {
        this.psw = psw;
    }

}

身份证类

package com.hfview.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/** * 人的身份证号码 * @author zhw * */

@Entity
@Table(name="T_Card")
public class Card {
    private int id;
    private String cardNum;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCardNum() {
        return cardNum;
    }

    public void setCardNum(String cardNum) {
        this.cardNum = cardNum;
    }

}

生成的sql语句

  • create table T_Card (
    id int identity not null,
    cardNum varchar(255) null,
    primary key (id)
    )
    create table T_Person (
    id int identity not null,
    name varchar(255) null,
    psw varchar(255) null,
    cardId int null,
    primary key (id)
    )
    alter table T_Person
    add constraint FK459B3DC0AC77181B
    foreign key (cardId)
    references T_Card

一对一双向映射

Person类还是上面的那个,但是card类做出改变

package com.hfview.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/** * 人的身份证号码 * @author zhw * */

@Entity
@Table(name="T_Card")
public class Card {
    private int id;
    private String cardNum;
    private Person person;
    //这里如果不写mappedBy,那么会在card里面生成一个指向person的外键
    //这句话就是指映射是在person类中 card(具体是getXXX的xxx部分)字段完成,这里的映射hibernate就不要关了
    @OneToOne(mappedBy="card")
    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCardNum() {
        return cardNum;
    }

    public void setCardNum(String cardNum) {
        this.cardNum = cardNum;
    }

}

这里和上一个生成的建表sql是一样的

组件映射

person类

package com.hfview.bean;

import javax.persistence.*;

import org.hibernate.annotations.ForeignKey;
/** * 单个的人 * @author zhw * */

@Entity
@Table(name="T_Person")
public class Person {

    public int id;
    public String name;
    public String psw;
    private Card card;
    @Embedded  //可写可不写
    private Country country;

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    @OneToOne()
    @JoinColumn(name="cardId")
    public Card getCard() {
        return card;
    }

    public void setCard(Card card) {
        this.card = card;
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPsw() {
        return psw;
    }

    public void setPsw(String psw) {
        this.psw = psw;
    }

}

country类

package com.hfview.bean;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Country {

    private int age;

    private String name;

    @Column(name="countryAge")
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Column(name="countryName")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

特别需要注意的是如果country里面的age、name不使用column来指定名字的话,那么就会和person里面的重合,在数据库里面就会忽略country的age和name

另外一种指定名字的方式是:

@Embedded
//@AttributeOverrides并不是官网中的放在这里,放在这里不起作用
    private Country country;

     @AttributeOverrides( {
         @AttributeOverride(name="age", column = @Column(name="countryAge2") ),
         @AttributeOverride(name="name", column = @Column(name="countryName2") )
    } )
    public Country getCountry() {
        return country;
    }

多对一

Flight 航班类

package com.hfview.bean;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Flight {
    private int id;
    private String name;
    private Company company;

    @ManyToOne
    @JoinColumn(name="companyId")//这里指定所属公司的字段名
    public Company getCompany() {
        return company;
    }

    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCompany(Company company) {
        this.company = company;
    }
}

航空公司类Company

package com.hfview.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Company {
    private int id;
    private String name;

    @Id @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

一对多

一对多中,必须用集合保存多的那一方
比如 城市类

package com.hfview.bean;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

@Entity
public class City {
    private int id;
    private String name;
    private Set<Street> street;

    @OneToMany
    @JoinColumn(name="cityId")
    public Set<Street> getStreet() {
        return street;
    }

    public void setStreet(Set<Street> street) {
        this.street = street;
    }

    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

街道类

package com.hfview.bean;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Street {
    private int id;
    private String name;

    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

这里必须要注意,如果city中的属性 街道 那里没有设置@joinCoulmn那么,hibernate将会生成三张表,类似于多对多来处理

一对多和多对一双向关联

city类

package com.hfview.bean;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class City {
    private int id;
    private String name;
    private Set<Street> street;

    @OneToMany(mappedBy="city")
    public Set<Street> getStreet() {
        return street;
    }

    public void setStreet(Set<Street> street) {
        this.street = street;
    }

    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

街道类

package com.hfview.bean;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Street {
    private int id;
    private String name;
    private City city;

    @ManyToOne
    @JoinColumn(name="cityId")
    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

这里一定要注意,双向关联一定要设置mappedBy,设计数据库的时候就是在多的那一方加上属于一的那个字段就行,这个时候多的那方(street)就是主导,mappedBy要写在一的那方(city)

多对多单向

Boss类

package com.hfview.bean;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Boss {
    private int id;
    private String name;

    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

员工类

package com.hfview.bean;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class Employee {

    private int id;
    private String name;

    private Set<Boss> boss;

    @ManyToMany
    @JoinTable(
            name="Employee_Boss",
            joinColumns=@JoinColumn(name="employeeId", referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="bossId", referencedColumnName="id")
            )//修改中间表的字段
    public Set<Boss> getBoss() {
        return boss;
    }

    public void setBoss(Set<Boss> boss) {
        this.boss = boss;
    }
    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

多对多双向

Boss类

package com.hfview.bean;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Boss {
    private int id;
    private String name;
    private Set<Employee> employee;

    @ManyToMany(mappedBy="boss")
    public Set<Employee> getEmployee() {
        return employee;
    }

    public void setEmployee(Set<Employee> employee) {
        this.employee = employee;
    }

    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Employee 类

package com.hfview.bean;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class Employee {

    private int id;
    private String name;

    private Set<Boss> boss;

    @ManyToMany
    @JoinTable(
            name="Employee_Boss",
            joinColumns=@JoinColumn(name="employeeId", referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="bossId", referencedColumnName="id")
            )
    public Set<Boss> getBoss() {
        return boss;
    }

    public void setBoss(Set<Boss> boss) {
        this.boss = boss;
    }
    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

集合映射

这部分不需要记了,大部分用set,需要排序用list。
map更麻烦。

你可能感兴趣的:(Hibernate,关系映射)