0

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?

Xavi López
  • 26,413
  • 11
  • 94
  • 146
mrddter
  • 59
  • 8
  • You'll need to have the association mapped with Hibernate (not just the FK field) in order to be able to perform a join with the Criteria API. It seems possible to achieve it by means of a subquery, though. See Pierre Pretorius' answer to [Hibernate criteria: Joining table without a mapped association](http://stackoverflow.com/q/720502/851811). – Xavi López Jul 08 '13 at 14:29
  • Nice, but in this case I get a subquery with a select-in and not a join, is a bit different (like ordering or the possibility to return fields for table t0 and/or t1 .. or null for no t1 association). For example, I would obtain a resultset like { t0.fieldA, t0.column2, t1.fieldC, t1.fieldD } ordered by t1.id. – mrddter Jul 08 '13 at 15:29

1 Answers1

1

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? ;-)

Buurman
  • 1,679
  • 14
  • 25
  • for my purpose mapping an entity (i know all about my db) is not the same to mapping all realationships between them, and this is because i *developer* need to choice when and how create relationship and CriteriaAPI is a good path to follow if you want attach this criteria with a dynamic-tool to give at user possibility to cross some data (Statistics and mathematics).. Am I wrong? JPA is a good choice. I'm analyzing and testing -in this days- hibernate vs eclipselink vs objectdb (last isn't an orm but implements JPA, for now i prefer stay on hibernate or eclipselink). Some advise? thanks. – mrddter Jul 09 '13 at 18:39
  • Ok, i moved on eclipselink. With EL is possible make a join based on cause "ON", the only problem is that is possible with JPQL (d'ho) but it's better than nothing. Now i need to investigate if CriteriaAPI is usable or not in this case. – mrddter Jul 09 '13 at 23:44