简体   繁体   中英

hibernate: left outer join with Criteria API, is possible?

I'm new on Hibernate so excuse me for banality but I can't find any answer related to my problem (i tried to search on docs & on SO).

If possible, I would create a left outer join from two tables ( t0 and t1 ) without using HQL; in my case I'd like to use only the Criteria API.

t0 { id, fieldA, fieldB }

t1 { id, fieldC, fieldD }

I don't know which fields will be used for the join, the user can decide it.

I see that the Criteria API has some nice functions such as createAlias or createCriteria but if I use these methods, I can't run a join.

Every table has a class (mapped with a specific hbm.xml ) like this:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">

<class name="it.class.T0" table="t0">
    <meta attribute="class-description">Table 0</meta>
    <!-- Unique id -->
    <id name="id" type="long" column="id">
        <generator class="native"/>
    </id>
    <!-- Natural key -->
    <natural-id mutable="true">
        <property name="fieldA" type="string" column="fieldA" not-null="true" />
        <property name="fieldB" type="string" column="fieldB" not-null="true" />
    </natural-id>
    <!-- Fields -->
    <property name="column1" type="long" column="columnd1" not-null="true" />
    <property name="column2" type="string" column="column2" not-null="false" />
</class>

</hibernate-mapping>

The entity class is like this:

public class T0 implements Serializable
{
   private static final long serialVersionUID = -8123118569358735984L;

   public long               id;

   public String             fieldA;
   public String             fieldB;
   public long               column1;
   public String             column2;

   public T0()
   {
      super();
   }
}

Is it possible to create a left outer join programmatically (with the Criteria API) without specifying in the hbm.xml (or in a specific HQL) which fields to use?

No. You can't use a hibernate api to create a join based on a mapping hibernate doesn't know anything about. It kind of defeats the purpose of using hibernate in the first place. Why not just write sql then? ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM