简体   繁体   中英

How to retrieve fk by id Hibernate

I have problem with my GWT + Hibernate application. I don't know how can I retrieve fk value by id.

Here is my classes:

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

And my hbm.xml files:

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>

I'm trying to do this like this:

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

But I get the following error:

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]

I know thats because my Account object doesn't contain fk_circle but I don't know how to link this.

If you can use Criteria instead of HQL , try this:

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

This will write a query on Circle , join Account and add a where clause on account id.

I have solution for my problem. I did this like that:

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

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