我有以下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 总结
以上是内存溢出为你收集整理的java – Hibernate Projections/Lazy加载非必需的1对1映射全部内容,希望文章能够帮你解决java – Hibernate Projections/Lazy加载非必需的1对1映射所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)