java – Hibernate Projections/Lazy加载非必需的1对1映射

前端之家收集整理的这篇文章主要介绍了java – Hibernate Projections/Lazy加载非必需的1对1映射前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有以下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",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)使预测工作,所以如果不需要,我不需要加载任何子关联

谢谢
达米安

最佳答案
唯一的选择是遵循以下步骤:

>将@LazyToOne添加到一对一关联:

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

>配置bytecode enhancement

原文链接:https://www.f2er.com/spring/432163.html

猜你在找的Spring相关文章