如何通过id Hibernate检索fk

时间:2015-12-09 18:13:46

标签: java hibernate

我的GWT + Hibernate应用程序有问题。我不知道如何通过id检索fk值。

这是我的课程:

Circle.java

public class Circle implements Serializable {

    private long id_circle;
    private String circle_name;
    private String description;
    private Set<Account> accounts;
    //getters setters

Account.java

public class Account implements Serializable {

    private Long id_account;
    private String login;
    private String password;
    private String name;
    private String lastName;
    private String party;
    private String description;
    private int account_type;
    private int if_blocked;
    private String education;
    //getters setters

我的hbm.xml文件:

Circle.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.serwis.domain.Circle" table="CIRCLE">
        <id name="id_circle" column="ID_CIRCLE" type="java.lang.Long">
            <generator class="identity" />
        </id>
        <property name="circle_name" type="java.lang.String" />
        <property name="description" type="java.lang.String" length="10000"/>
        <set name="accounts" cascade="all">
            <key column="fk_circle"/>
            <one-to-many class="com.serwis.domain.Account"/>    
        </set>
    </class>
</hibernate-mapping>

Account.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.serwis.domain.Account" table="ACCOUNT">
        <id name="id" column="ID_ACCOUNT" type="java.lang.Long">
            <generator class="identity" />
        </id>
        <property name="login" type="java.lang.String" unique="true" />
        <property name="password" type="java.lang.String" />
        <property name="name" type="java.lang.String" />
        <property name="lastName" type="java.lang.String" />
        <property name="party" type="java.lang.String" />
        <property name="description" type="java.lang.String" />
        <property name="account_type" type="java.lang.Integer"></property>
        <property name="if_blocked" type="java.lang.Integer"></property>
        <property name="education" type="java.lang.String" length="2000"/>
    </class>
</hibernate-mapping>

我试图这样做:

Query query = session.createQuery("SELECT A.fk_circle FROM Account A WHERE A.id_account=:accountId");
            query.setParameter("accountId", accountId);

但是我收到以下错误:

org.hibernate.QueryException: could not resolve property: fk_circle of: com.serwis.domain.Account [SELECT A.fk_circle FROM com.serwis.domain.Account A WHERE A.id_account=:accountId]

我知道这是因为我的帐户对象并不包含fk_circle但我不知道如何链接它。

2 个答案:

答案 0 :(得分:0)

如果您可以使用Criteria代替HQL,请尝试以下操作:

Criteria crit = session.createCriteria(Circle.class);
crit.createAlias("accounts", "accounts");
crit.add(Restrictions.eq("accounts.id_account", accountId));
return crit.list();

这将在Circle上编写查询,加入Account并在帐户ID上添加where子句。

答案 1 :(得分:0)

我解决了我的问题。我是这样做的:

Query query = session.createQuery("SELECT c FROM Circle c WHERE (:account in elements(c.accounts))");
query.setParameter("account", account);
result = ((Circle)query.uniqueResult()).getId_circle();