Hibernate Projections / Lazy Loading用于非必需的1对1映射

时间:2015-04-21 11:02:08

标签: java spring hibernate hibernate-mapping hibernate-criteria

我有以下2个课程(为这篇文章修剪过)

public class ApplicationVO implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -3314933694797958587L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Integer id;


    @OneToOne(fetch = FetchType.LAZY, mappedBy = "application")
    @Cascade({ CascadeType.ALL })
    @JsonIgnore
    private ApplicationHomeScreenVO applicationHomeScreen;

...
...
... 
}


public class ApplicationHomeScreenVO implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -9158898930601867545L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    @JsonProperty("id")
    private Integer id;

    @OneToOne(fetch = FetchType.LAZY)
    @Cascade({ CascadeType.SAVE_UPDATE })
    @JoinColumn(name="application_id")
    @JsonProperty("application")
    protected ApplicationVO application;    

    ...
    ...
    ...
}

我正在尝试加载applicationById wuthout加载applicationHomeScreen 不幸的是,延迟加载似乎没有用。我查看了其他帖子,他们建议在@OneToOne注释上设置option = false标志但不幸​​的是,applicationHomeScreen可以是可选的

我现在正在尝试使用预测,但这对我来说也不起作用

当我调用以下方法时

 public ApplicationVO findApplicationById(Integer applicationId) {

        Criteria criteria = currentSession().createCriteria(ApplicationVO.class);
        criteria.add(Restrictions.eq("id", applicationId));

        criteria.setProjection(Projections.projectionList()
                .add(Projections.property("applicationHomeScreen"), "applicationHomeScreen"))
              .setResultTransformer(Transformers.aliasToBean(ApplicationVO.class));

        ApplicationVO applicationVO = (ApplicationVO) criteria.uniqueResult();

        return applicationVO;
    }

我得到了堆栈跟踪

java.lang.ArrayIndexOutOfBoundsException: 0
    at org.hibernate.loader.criteria.CriteriaLoader.getResultRow(CriteriaLoader.java:168)
    at org.hibernate.loader.criteria.CriteriaLoader.getResultColumnOrRow(CriteriaLoader.java:148)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:754)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:953)
    at org.hibernate.loader.Loader.doQuery(Loader.java:921)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
    at org.hibernate.loader.Loader.doList(Loader.java:2554)
    at org.hibernate.loader.Loader.doList(Loader.java:2540)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
    at org.hibernate.loader.Loader.list(Loader.java:2365)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1682)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
    at org.hibernate.internal.CriteriaImpl.uniqueResult(CriteriaImpl.java:402)
    at com.dao.database.impl.ApplicationDAOImpl.findApplicationById(ApplicationDAOImpl.java:349)

任何人都可以推荐一种我可以使用的方法 a)使延迟加载适用于不需要关联的一对一映射 b)让预测工作,所以如果不需要,我不需要加载任何子关联

谢谢 达明

2 个答案:

答案 0 :(得分:2)

唯一的选择是遵循以下步骤:

  1. @LazyToOne添加到一对一关联:

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "application")
    @Cascade({ CascadeType.ALL })
    @JsonIgnore
    @LazyToOne(value = LazyToOneOption.NO_PROXY)
    private ApplicationHomeScreenVO applicationHomeScreen;
    
  2. 配置bytecode enhancement

答案 1 :(得分:1)

可选的@OneToOne关系不能是懒惰的,因为关系的非拥有方现在不知道子节点是否存在(可选) - Hibernate不知道是否放置代理对象或{{1 }}。有几种解决方法。这个link详细解释了一些变通方法