即使使用session.load(),Hibernate也会忽略@Fetch(FetchMode.JOIN)

时间:2017-05-09 11:58:37

标签: java hibernate

我的Hibernate 4.2.2项目中有以下实体

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 个答案:

答案 0 :(得分:1)

问题是配置值

hibernate.max_fetch_depth=0

设置为1时可以正常工作