0

我在 Hibernate 3 中使用 Java。

我有一个sql如下:

select sd3.id as id_3, sd2.id as id_2, sd1.id as id_1, od.id as id_0 from developer as sd3 
  left outer join developer as sd2 on sd3.sr_developer_id = sd2.id
  left outer join developer as sd1 on sd2.sr_developer_id = sd1.id
  left outer join developer as od on sd1.sr_developer_id = od.id
where sd3.id = 812

这是一个自加入的情况。

表结构如下所示:

开发商

id  | sr_developer_id
---------------------
812 | 463
463 | 8
8   | NULL

上述查询的输出是:

id_3 | id_2 | id_1 | id_0
--------------------------
 812 |  463 |  8   | NULL

现在我想在 HQL 或条件中转换此查询。谁能帮我?

编辑 :-

开发者.java

private long id;
private long srDeveloperID;

public long getId() {
     return id;
}

public void setId(long id) {
     this.id = id;
}

public long getSrDeveloperID() {
    return srDeveloperID;
}

public void setSrDeveloper(long srDeveloperID) {
    this.srDeveloperID = srDeveloperID;
}

开发者.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 >
    <class name="com.beans.Developer" table="developer">
        <id name="id" column="id" type="long">
            <generator class="native"/>
        </id>
        <property name="srDeveloperID" column="sr_developer_id" type="long"></property>
    </class>
</hibernate-mapping>
4

2 回答 2

1

如果您只映射与 Hibernate 的关联,您想要实现的目标会更容易。所以而不是:

<property name="srDeveloperID" column="sr_developer_id" type="long"></property>

做这个:

<many-to-one name="seniorDeveloper" column="sr_developer_id" class="Developer"/>

Developer.java中,而不是:

private long srDeveloperID;

这个:

private Developer seniorDeveloper;

然后你可以把你的查询写成:

select sd3.id, sd2.id, sd1.id, od.id
from Developer sd3 
left join sd3.seniorDeveloper sd2
left join sd2.seniorDeveloper sd1
left join sd1.seniorDeveloper od
where sd3.id = 8

从这里你可以得到一个Object[]s 的列表,其中 id 将在Object[0]Object[1]...,等等。

于 2013-06-15T10:24:56.120 回答
1

试试这个......

按原样放置您的查询(即选择........id = 812)@“您的查询”;

{
     String sql = "Your Query";
     SQLQuery query = session.createSQLQuery(sql);

     query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
     return query.list();
}
于 2013-06-10T13:23:54.037 回答