如何使用hibernate hql在两个表中选择数据?

时间:2013-09-02 16:52:06

标签: java hibernate

我的代码是这样的: 我想在两个表格中选择数据

    // ArrayList<DetailType> list = null;
    SessionFactory sf = null;
    Session ses = null;
    try {

        Configuration cfg = new Configuration().configure();
        ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
                cfg.getProperties()).buildServiceRegistry();
        sf = cfg.buildSessionFactory(sr);
        ses = sf.openSession();
        // 执行查询语句

        String hql = "select new DetailVoMain(d.d_id,d.d_name,m.m_name)"
                + "from MainType m, DetailType d "
                + "where d.d_main_id = m.m_id";
        Query query = ses.createQuery(hql);

        List<?> list = query.list();
        for (int i = 0; i < list.size(); i++) {
            DetailVoMain detailType = (DetailVoMain) list.get(i);
            System.out.println("dId-------->" + detailType.getdId());
            System.out.println("dNam-------->" + detailType.getdName());
            System.out.println("mNam-------->" + detailType.getmName());
        }
    } catch (HibernateException e) {
        System.out.println("hibernate exception");
        e.printStackTrace();

    } finally {
        ses.close();
        sf.close();
    }

和myeclipese给我错误信息:

hibernate exception

org.hibernate.QueryException:无法解析属性:d_id:com.xunfang.em_mallServer.bean.DetailType [从com.xunfang.em_mallServer中选择新的DetailVoMain(d.d_id,d.d_name,m.m_name)。 bean.MainType m,com.xunfang.em_mallServer.bean.DetailType d其中d.d_main_id = m.m_id]     在org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)     在org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)     在org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1968)     在org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:313)     在org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:490)     在org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:616)     在org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:267)     在org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:214)     在org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:119)     在org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:115)     在org.hibernate.hql.internal.ast.tree.DotNode.resolveSelectExpression(DotNode.java:678)     在org.hibernate.hql.internal.ast.HqlSqlWalker.resolveSelectExpression(HqlSqlWalker.java:893)     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2208)     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.constructor(HqlSqlBaseWalker.java:2502)     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2275)     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2145)     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1451)     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:571)     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)     在org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:249)     在org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:184)     在org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:137)     在org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:105)     在org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:80)     在org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)     在org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)     在org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)     在org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778)     在com.xunfang.em_mallServer.util.Test.main(Test.java:39)

请帮助我,我整夜测试!

我的MainType实体是这样的:

public class MainType {
private String mId; // 编号
private String mName; // 类别名称

public String getmId() {
    return mId;
}

public void setmId(String mId) {
    this.mId = mId;
}

public String getmName() {
    return mName;
}

和这样的映射:

<hibernate-mapping package="com.xunfang.em_mallServer.bean">

<class name="MainType" table="t_main_type"> <!-- 设置表名和字段对应数据 -->
    <id name="mId" column="m_id" type="string">

    </id>
    <property name="mName" column="m_name" type="string"></property>

</class>

DetailType实体,如下所示:

public class DetailType {
/**
 * 小类编号
 */
private String dId;
/**
 * 小类名称
 */
private String dName;
/**
 * 归属大类编号
 */
private String dMainId;

public String getdId() {
    return dId;
}

public void setdId(String dId) {
    this.dId = dId;
}

public String getdName() {
    return dName;
}

public void setdName(String dName) {
    this.dName = dName;
}

public String getdMainId() {
    return dMainId;
}

public void setdMainId(String dMainId) {
    this.dMainId = dMainId;
}

和映射:

<hibernate-mapping package="com.xunfang.em_mallServer.bean">

<class name="DetailType" table="t_detail_type"> <!-- 设置表名和字段对应数据 -->
    <id name="dId" column="d_id" type="string">

    </id>
    <property name="dName" column="d_name" type="string"></property>
    <property name="dMainId" column="d_main_id" type="string"></property>

</class>

谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

你应该写HQL正确,使用映射属性,例如dId(而不是d_id)。
您需要在实体之间添加关系(例如OneToManyManyToOne) 之后,您可以加入hql中的其他实体。