开发者

Hibernate Criteria API - create automatic join

I have the following setup

Seizure {
   private SeizureI18n seizureI18n;

   @OneToOne(fetch = FetchType.LAZY)
   @PrimaryKeyJoinColumn
   public SeizureI18n getSeizureI18n() {
     return this.seizureI18n;
   }
}

SeizureI18n {
  private Seizure seizure;

  @OneToOne(fetch = FetchType.LAZY, mappedBy = "seizureI18n")
  public Seizure getSeizure() {
    return this.seizure;
  }

}

When I issue a query with the following Criteria query (observe the usage of an alias)

 criteria = metaDataConsole.session.createCriteria(Seizure.class,"Seizure");

 criteria.createCriteria("Seizure.seizureI18n");

 criteria.list();

it results in the following SQL query.

FROM   incbszdb.seizure this_ 
  INNER JOIN incbszdb.seizure_i18n seizurei18x1_ 
ON this_.id = seizurei18x1_.id 
  LEFT OUTER JOIN incbszdb.seizure seizure4_ 
ON seizurei18x1_.id = seizure4_.id 

Where does the s开发者_C百科econd join to seizures

LEFT OUTER JOIN incbszdb.seizure seizure4_ 
ON seizurei18x1_.id = seizure4_.id 

comes from?


I think Hibernate adds this join because, when loading a SeizureI18n, it's unable to know if the SeizureI18n has an associated Seizure or not. So it's unable to know if he must populate the seizure field with null or with a Seizure instance (or proxy to a Seizure instance). Of course, in this particular query, it seems strange. But if you were just loading SeizureI18n without their Seizure, it would feel more normal.

If a SeizureI18n always has an associated Seizure, you should mark the association as non-optional (optional = false). I think it should avoid the additional join (since Hibernate would know that there is an associated Seizure, and that the property is thus never null).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜