2017-05-09 95 views
2

我在休眠4.2.2項目休眠忽略@Fetch(FetchMode.JOIN)語句甚至session.load()

import javax.persistence.CascadeType; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.OneToOne; 
import javax.validation.constraints.NotNull; 
import org.hibernate.annotations.Fetch; 
import org.hibernate.annotations.FetchMode; 

@Entity 
public class Parent { 

    @Id 
    @NotNull 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    @OneToOne(cascade = CascadeType.ALL) 
    @Fetch(FetchMode.JOIN) 
    private Child child; 

    private String name; 

    public Long getId() { 
     return id; 
    } 

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

    public Child getChild() { 
     return child; 
    } 

    public void setChild(Child child) { 
     this.child = child; 
    } 

    public String getName() { 
     return name; 
    } 

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

} 



import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.validation.constraints.NotNull; 

@Entity 
public class Child { 

    @Id 
    @NotNull 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    private String name; 

    public Long getId() { 
     return id; 
    } 

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

    public String getName() { 
     return name; 
    } 

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

} 

以下實體和寫了一個小測試

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.context.transaction.TransactionConfiguration; 
import org.springframework.transaction.annotation.Transactional; 

import by2.server.common.test.Child; 
import by2.server.common.test.Parent; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "file:WebContent/WEB-INF/applicationContext.xml" }) 
@TransactionConfiguration(transactionManager = "hibernateTransactionManager") 
@Transactional() 
public class JoinFetchTest { 

    @Autowired 
    SessionFactory sessionFactory; 

    @Test 
    public void test() { 
     Session s = sessionFactory.getCurrentSession(); 
     Parent p = new Parent(); 
     p.setChild(new Child()); 
     p.setName("parnetname"); 
     p.getChild().setName("childname"); 
     s.save(p); 
     s.flush(); 
     s.clear(); 
     Parent result = (Parent) s.load(Parent.class, p.getId()); 
     System.out.println(result.getName()); 
     System.out.println(result.getChild().getName()); 
    } 
} 

日誌看起來像這樣

main 09/05/2017 13:52:52,697 | DEBUG | org.hibernate.SQL | logStatement | select nextval ('hibernate_sequence') 
main 09/05/2017 13:52:52,710 | DEBUG | org.hibernate.SQL | logStatement | select nextval ('hibernate_sequence') 
main 09/05/2017 13:52:53,012 | DEBUG | org.hibernate.SQL | logStatement | insert into Child (name, id) values (?, ?) 
main 09/05/2017 13:52:53,022 | DEBUG | org.hibernate.SQL | logStatement | insert into Parent (child_id, name, id) values (?, ?, ?) 
main 09/05/2017 13:52:53,945 | DEBUG | org.hibernate.SQL | logStatement | select parent0_.id as id1_66_0_, parent0_.child_id as child3_66_0_, parent0_.name as name2_66_0_ from Parent parent0_ where parent0_.id=? 
main 09/05/2017 13:52:53,948 | DEBUG | org.hibernate.SQL | logStatement | select child0_.id as id1_25_0_, child0_.name as name2_25_0_ from Child child0_ where child0_.id=? 
parnetname 
childname 

爲什麼沒有加入子類?當設置爲1

回答

1

的問題是一個配置值

hibernate.max_fetch_depth=0 

它工作