Hibernate Projections / Lazy Download for optional 1 to 1 mappings

I have the following 2 classes (trimmed for this entry)

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; ... ... ... } 

I'm trying to load applicationById without loading the application. HomeScreen Unfortunately, lazy loading does not seem to work. I looked at other posts and they recommend setting the option = false checkbox in the @OneToOne annotation, but unfortunately applicationHomeScreen may be optional

Now I am trying to use forecasts, but this does not work either for me or for

When I call the following method

  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; } 

I get a stack trace

 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) 

Can anyone recommend an approach that I can use for a) getting lazy loading, working properly for one to one mapping, where no association is required b) getting work on projections, so I don’t need to load child associations if they don’t are needed

Thanks. Damien

+6
source share
2 answers

The only option is to follow these steps:

  • Add @LazyToOne to the one-to-one association:

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

+2
source

The optional @OneToOne relationships cannot be lazy because the party that does not own the relationship does not know if a child exists (optional) - Hibernate does not know whether to put a proxy object or null . There are several workarounds for this. This link provides a detailed explanation of some workarounds.

+1
source

Source: https://habr.com/ru/post/985594/


All Articles