Java:如何将 Hibernate 命名 SQL 查询(在 XML 文件中)的结果映射到带注释的实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20979585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:42:06  来源:igfitidea点击:

Java: How to map results of Hibernate Named SQL Query (in XML file) to an annotated Entity

javasqlxmlhibernatenamed-query

提问by Shaun Scovil

I'm trying to map the results of a Hibernate Named SQL Query (stored in an XML file) to an annotated Entity object. I've read through countless docs and tutorials and support threads but I still can't seem to get it working.

我正在尝试将 Hibernate 命名 SQL 查询(存储在 XML 文件中)的结果映射到带注释的实体对象。我已经阅读了无数的文档和教程以及支持线程,但我似乎仍然无法让它工作。

Here's what I've got so far...

这是我到目前为止所得到的......

In my persistence.xmlfile, I've declared a mapping file called test.xml, and in test.xmlI've got the following:

在我的persistence.xml文件中,我声明了一个名为 的映射文件test.xml,其中test.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>
    <sql-query name="Asset.Test">
        <![CDATA[ SELECT a.id, a.name FROM assets a WHERE a.id = 1 ]]>
    </sql-query>

</hibernate-mapping>

This works fine for retrieving results in a generic Object, using this code:

这适用于检索通用对象中的结果,使用以下代码:

Object entity = session.getNamedQuery("Asset.Test").list().get(0);

But when I try to map these results to annotated Entity object, I keep getting an exception Caused by: org.hibernate.HibernateException: Errors in named queries: Asset.Test.

但是当我尝试将这些结果映射到带注释的 Entity 对象时,我不断收到异常Caused by: org.hibernate.HibernateException: Errors in named queries: Asset.Test

Here is my Entity:

这是我的实体:

import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
import java.math.BigInteger;

@Entity
public class AssetTestEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private BigInteger id;
    private String name;

    public BigInteger getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

And here is my modified Named Query:

这是我修改后的命名查询:

<sql-query name="Asset.Test">
    <return alias="assets" class="AssetTestEntity" />
    <![CDATA[ SELECT assets.id AS {assets.id}, assets.name AS {assets.name} FROM assets WHERE assets.id = 1 ]]>
</sql-query>

I've tried several variations of this, with and without the curly braces, using different aliases, etc.

我已经尝试了几种变体,有和没有花括号,使用不同的别名等。

I've RTFM, including this: http://docs.jboss.org/hibernate/stable/core.old/reference/en/html/querysql-namedqueries.html

我有 RTFM,包括这个:http: //docs.jboss.org/hibernate/stable/core.old/reference/en/html/querysql-namedqueries.html

I've seen dozens of examples like this: http://examples.javacodegeeks.com/enterprise-java/hibernate/hibernate-named-query-example/

我见过很多这样的例子:http: //examples.javacodegeeks.com/enterprise-java/hibernate/hibernate-named-query-example/

Can anyone see what I'm doing wrong here? Do you have to define the entity in XML as well?

谁能看到我在这里做错了什么?您是否也必须在 XML 中定义实体?

Thanks in advance!

提前致谢!

UPDATE

更新

I did manage to get it to work by defining the Entity class in the XML file, in addition to having the Entity object. The working mapping XML looks like this:

除了拥有 Entity 对象外,我确实通过在 XML 文件中定义 Entity 类来使其工作。工作映射 XML 如下所示:

<hibernate-mapping>

    <class name="com.asset.AssetTestEntity" table="assets">
        <id name="id" type="java.math.BigInteger">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="name" />
        </property>
    </class>

    <sql-query name="Asset.Test">
        <return alias="assets" class="com.asset.AssetTestEntity" />
        <![CDATA[ SELECT a.id, a.name FROM assets a WHERE a.id = 1 ]]>
    </sql-query>

</hibernate-mapping>

I was hoping Hibernate could handle just using the annotated Entity without having to do this, but apparently not. I even tried adding @Column annotations to the entity...

我希望 Hibernate 可以只使用带注释的实体而不必这样做,但显然不是。我什至尝试向实体添加@Column 注释...

采纳答案by Marcel St?r

The fact that it works if you define the OR mapping alongside the named query in the mapping files leads to a simple conclusion (for me): Hibernate doesn't know about your @Entityannotated class.

如果您在映射文件中的命名查询旁边定义 OR 映射,它会起作用这一事实导致一个简单的结论(对我而言):Hibernate 不知道您的带@Entity注释的类。

Make sure the persistence.xmllists your entity classes.

确保persistence.xml列出您的实体类

回答by Wayne Baylor

Hibernate can also automatically populate beans from query results:

Hibernate 还可以从查询结果中自动填充 bean:

List<AssetTestEntity> results = session.getNamedQuery("Asset.Test")
       .setResultTransformer(Transformers.aliasToBean(AssetTestEntity.class))
       .list();

However the results are not "managed" like non-native query results.

然而,结果不像非本地查询结果那样“受管理”。