hibernate Annotation 双向one2many

主控方:House.java

package com.yazuo.entity.annotation.one2many;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="house")
@SequenceGenerator(name="house_id_seq",sequenceName="house_id_seq",allocationSize=1)
public class House implements Serializable{

    /**
     *
     */
    private static final long serialVersionUID = 4889427366089042926L;
   
    @Id
    @GeneratedValue(generator="house_id_seq",strategy=GenerationType.SEQUENCE)
    private Integer id;
   
    private String address;
   
    @ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
    @JoinColumn(name="person_id")

    private Person person;

    public Integer getId() {
        return id;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Person getPerson() {
        return person;
    }

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

}
 

被控方:Person.java

package com.yazuo.entity.annotation.one2many;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="person")
@SequenceGenerator(name="person_id_seq",sequenceName="person_id_seq",allocationSize=1)
public class Person implements Serializable{

    /**
     *
     */
    private static final long serialVersionUID = 4865848211361074151L;
   
    @Id
    @GeneratedValue(generator="person_id_seq",strategy=GenerationType.SEQUENCE)
    private Integer id;
   
    private String name;
   
    @OneToMany(mappedBy="person")
    private Set<House> houses = new HashSet<House>();

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Set<House> getHouses() {
        return houses;
    }

    public void setHouses(Set<House> houses) {
        this.houses = houses;
    }

}
 

Test.java

package com.yazuo.entity.annotation.one2many;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;


public class Test {

    @org.junit.Test
    public void add() throws Exception {
        AnnotationConfiguration cfg = new AnnotationConfiguration();
        SessionFactory sf = cfg.configure().buildSessionFactory();
        Session session = sf.openSession();
       
        Person person = new Person();
        person.setName("person");
       
        House house1 = new House();
        House house2 = new House();
        house1.setAddress("address1");
        house2.setAddress("address2");
       
        house1.setPerson(person);
        house2.setPerson(person);
       
        Set<House> houses = new HashSet<House>();
        houses.add(house1);
        houses.add(house2);
       
       
        session.save(house1);
   
        person.setHouses(houses);
        session.flush();
        session.close();
        sf.close();
    }
   
    @org.junit.Test
    public void findPersonById() throws Exception {
        AnnotationConfiguration cfg = new AnnotationConfiguration();
        SessionFactory sf = cfg.configure().buildSessionFactory();
        Session session = sf.openSession();
       
        String hql = "from Person p where p.id = 3";
        Person person = (Person) session.createQuery(hql).list().get(0);
        System.out.println(person.getName());
        System.out.println(person.getHouses().size());
   
        session.flush();
        session.close();
        sf.close();
    }
   
    @org.junit.Test
    public void findHouseById() throws Exception {
        AnnotationConfiguration cfg = new AnnotationConfiguration();
        SessionFactory sf = cfg.configure().buildSessionFactory();
        Session session = sf.openSession();
       
        String hql = "from House h where h.id = 1";
        House house = (House) session.createQuery(hql).list().get(0);
        System.out.println(house.getAddress());
//        System.out.println(house.getPerson().getName());
   
        session.flush();
        session.close();
        sf.close();
    }
   
    @org.junit.Test
    public void deleteHouseById() throws Exception {
        AnnotationConfiguration cfg = new AnnotationConfiguration();
        SessionFactory sf = cfg.configure().buildSessionFactory();
        Session session = sf.openSession();
       
        String hql = "from House h where h.id = 1";
        House house = (House) session.createQuery(hql).list().get(0);
        session.delete(house);
   
        session.flush();
        session.close();
        sf.close();
    }
   
    @org.junit.Test
    public void deletePersonById() throws Exception {
        AnnotationConfiguration cfg = new AnnotationConfiguration();
        SessionFactory sf = cfg.configure().buildSessionFactory();
        Session session = sf.openSession();
       
        String hql = "from Person p where p.id = 3";
        Person person = (Person) session.createQuery(hql).list().get(0);
        session.delete(person);
   
        session.flush();
        session.close();
        sf.close();
    }
   
}
 

小结:

终于看到 fetch=FetchType.LAZY 的效果了
在 one-to-many 双向关联中
双方都不加 fetch=FetchType.LAZY
执行 findPersonById() 时,不会查找 person.house,只有在显示的调用 person.getHouse()
才会打印出 select sql。
执行 findHouseById() 时,会自动查找 house.person
当将house 的 many-to-one(fetch=FetchType.LAZY),则不会自动查找 house.person,只有在显示的调用 house.getPerson() 才会打印出 select sql
 

你可能感兴趣的:(Hibernate,import)