Hibernate 4延迟加载不起作用

时间:2014-01-02 02:59:22

标签: java spring hibernate

我正在使用spring 3和hibernate 4开发Web应用程序。 我无法延迟加载用户所属组的一组授权权限。 从数据库中检索用户对象时,即使显式调用user.getGroup()。getPermissions(),其Group对象也没有任何权限。

我注意到在调试模式下,如果我将鼠标悬停在User对象上,然后导航到User对象内的Group对象,然后导航到其权限,我可以看到它的类型显示为PersistentSet,并展开它将正确加载权限。但在非调试模式下,权限永远不会延迟加载。我在这里错过了什么?提前谢谢。

以下是实体之间的关系: 用户与组之间存在一对多关系,并且组与权限之间存在多对多关系。

这是UserDaoImpl类的定义

@Repository
@Transactional
public class UserDaoImpl implements UserDao  {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public User get(String username) {
        Session session = getSession();
        User user = (User) session.createCriteria(User.class).add(Restrictions.eq("username", username)).uniqueResult();

        Group g = user.getGroup();

        // calling g.getGrantedPermissions() doesn't load any permission
        Set<Permission> permissions = g.getGrantedPermissions();
        return user;
    }
}

以下是Permission类的定义

@Entity
public class Permission {
    @Id
    @GeneratedValue
    private Long id;

    @Column
    private String name;

    @Column
    private String description;

    public Permission()
    {

    }

    public Long getId() {
        return id;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

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

这是Group类的定义

@Entity
@Table(name="\"Group\"")
public class Group {
    public static final String ROLE_VALID_USER = "ROLE_VALID_USER";

    @Id
    @GeneratedValue
    private Long id;

    @Column
    private String description;

    @Column
    private String role;

    @ManyToMany
    @JoinTable(name = "granted_permission", 
            joinColumns = {@JoinColumn(name = "GROUP_ID", nullable = false, updatable = false) }, 
            inverseJoinColumns = { @JoinColumn(name = "PERMISSION_ID", nullable = false, updatable = false) })
    private Set<Permission> grantedPermissions;

    public Group()
    {

    }

    public Long getId() {
        return id;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public Set<Permission> getGrantedPermissions() {
        return grantedPermissions;
    }

    public void setGrantedPermissions(Set<Permission> grantedPermissions) {
        this.grantedPermissions = grantedPermissions;
    }
}

以下是User类的定义

@Entity
public class User {
    @Id
    @Column(name="USERNAME")
    private String username;

    @Column(name="PASSWORD")
    private String password;

    @Transient
    private final boolean enabled = true;

    @Column
    private String name;

    @Column(name="EMAIL")
    private String email;

    @Column(name="PHONE")
    private String phone;

    @ManyToOne
    private Group group;

    @ManyToOne
    @JoinColumn(name="ORIGINAL_BRANCH_ID")
    private Branch originalBranch;

    @ManyToOne
    @JoinColumn(name="ID_OF_RESPONSIBLE_BRANCH")
    private Branch responsibleBranch;

    public User()
    {

    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRole() {
        return group.getRole();
    }

    public boolean isEnabled() {
        return enabled;
    }

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public Branch getOriginalBranch() {
        return originalBranch;
    }

    public void setOriginalBranch(Branch originalBranch) {
        this.originalBranch = originalBranch;
    }

    public Branch getResponsibleBranch() {
        return responsibleBranch;
    }

    public void setResponsibleBranch(Branch responsibleBranch) {
        this.responsibleBranch = responsibleBranch;
    }
}

这是持久性配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

       <tx:annotation-driven />

       <context:annotation-config />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <context:component-scan base-package="net.acme.prs" />

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="mysqlDataSource" />
        <property name="packagesToScan" value="net.acme.prs" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQL5InnoDBDialect
                </prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>

    <bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/prs" />
        <property name="username" value="" />
        <property name="password" value="" />
    </bean>
</beans>

2 个答案:

答案 0 :(得分:3)

我无法理解您的问题是什么,因为您没有提到您遇到的任何错误。如果您只是发现没有SQL来获取grantedPermissions,那么它就是延迟获取的内容:它仅在访问时获取,如果未访问它,则不会获取它。 / p>

您在调试期间看到它被提取的原因是因为当您在调试器中检查grantedPermissions时,它正在访问该属性,这将触发延迟提取。但是,对于非调试模式,如果没有访问该属性的代码,则不会提取它应该执行的操作。


如果您要问的是,您的代码处于非调试模式,将访问grantedPermissions但是在访问时延迟获取失败,那么这是由于访问超出事务:Hibernate需要有一个活动的Session来进行延迟抓取。如果延迟提取发生在事务之外,它将失败,因为没有打开的Session。您应该考虑

重新审视您的设计
  1. 交易范围:不应在DAO周围进行交易,而应将交易边界置于适当的工作单位级别:也许是您的应用服务或控制器
  2. 正确加入fetch / eager fetch,以便在事务外不会发生延迟抓取
  3. EclipseLink是另一个JPA实现,它允许在会话结束后进行延迟提取。

答案 1 :(得分:0)

关注http://docs.oracle.com/javaee/5/api/javax/persistence/FetchType.html
示例:如果要在“组”中自动加载“grantedPermissions”,则必须将注释更改为@ManyToMany(fetch = FetchType.EAGER)