0

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

    // 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 of: com.xunfang.em_mallServer.bean.DetailType [select new DetailVoMain(d.d_id,d.d_name,m.m_name)from com.xunfang.em_mallServer.bean.MainType m, com.xunfang.em_mallServer.bean.DetailType d where d.d_main_id = m.m_id] at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83) at 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.DotNode 的 org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:490)。getDataType(DotNode.java:616) at org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:267) at 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章.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.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168) 在 org.hibernate.internal 的 org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:80)。 AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221) at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199) at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778) at com.xunfang.em_mallServer .util.Test.main(Test.java:39)hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778) 在 com.xunfang.em_mallServer.util.Test.main(Test.java:39)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>

谢谢你帮助我!

4

1 回答 1

0

您应该编写HQL正确的,例如使用映射属性dId(而不是d_id)。
您需要添加实体之间的关系(即OneToMany, ManyToOne)。
之后,您可以在您的hql.

于 2013-09-02T23:12:03.603 回答