hibernate:可以使用Criteria API左外连接吗?

时间:2013-07-08 14:27:17

标签: java hibernate criteria-api outer-join

我是Hibernate的新手,请原谅我的平庸,但我找不到任何与我的问题相关的答案(我试图搜索docs& on SO)。

如果可能的话,我会在不使用HQL的情况下从两个表(t0t1)创建左外连接;就我而言,我只想使用Criteria API。

t0 { id, fieldA, fieldB }

t1 { id, fieldC, fieldD }

我不知道哪些字段将用于连接,用户可以自行决定。

我发现Criteria API有一些很好的功能,例如createAliascreateCriteria但是如果我使用这些方法,我就无法运行连接。

每个表都有一个类(用特定的hbm.xml映射),如下所示:

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

实体类是这样的:

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

是否可以通过编程方式(使用Criteria API)创建左外连接,而无需在hbm.xml(或特定HQL)中指定要使用哪些字段?

1 个答案:

答案 0 :(得分:1)

没有。你不能使用hibernate api来创建一个基于映射的连接,hibernate对此一无所知。它首先打败了使用hibernate的目的。为什么不写sql呢? ; - )