Java 没有基础表的 JPA 实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28945146/
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-11 07:02:50  来源:igfitidea点击:

JPA entity without underlying table

javaspringhibernatejpa

提问by Nikhil

I want to create a class that can be mapped to a result extracted from the database using JPA native query. Is there a way to map an entity without an underlying table to the result? I referred to thislink which allows it for hibernate. Can this be done using JPA instead?

我想创建一个可以映射到使用 JPA 本机查询从数据库中提取的结果的类。有没有办法将没有基础表的实体映射到结果?我提到了这个链接,它允许它休眠。这可以使用 JPA 来完成吗?

This is my class for which I want the result to be mapped.

这是我想要映射结果的类。

import java.math.BigDecimal;
import javax.persistence.Entity;
@Entity
public class OpUsage {  
    String username;    
    BigDecimal number_of_clicks;    
    String accordion;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public BigDecimal getNumber_of_clicks() {
        return number_of_clicks;
    }
    public void setNumber_of_clicks(BigDecimal number_of_clicks) {
        this.number_of_clicks = number_of_clicks;
    }

    public String getAccordion() {
        return accordion;
    }

    public void setAccordion(String accordion) {
        this.accordion = accordion;
    }
}

采纳答案by Master Slave

JPA 2.1 specificationdefines the means to return the result from a native query to a non entity class

JPA 2.1 规范定义了将结果从本机查询返回到非实体类的方法

You should checkout the heading 3.10.16.2 Returning Unmanaged Instancesespecially the

您应该查看标题3.10.16.2 Returning Unmanaged Instances,尤其是

3.10.16.2.2 Constructor Results

3.10.16.2.2 构造函数结果

The mapping to constructors is specified using the ConstructorResultannotation element of the SqlResultSetMappingannotation. The targetClasselement of the ConstructorResultannotation specifies the class whose constructor corresponds to the specified columns. All columns corresponding to arguments of the intended constructor must be specified using the columns element of the ConstructorResultannotation in the same order as that of the argument list of the constructor. Any entities returned as constructor results will be in either the new or the detached state, depending on whether a primary key is retrieved for the constructed object.

到构造函数的映射是使用SqlResultSetMapping注释的ConstructorResult注释元素指定的。ConstructorResult注释的 targetClass元素指定其构造函数对应于指定列的类。必须使用ConstructorResult注释的列元素以与构造函数的参数列表相同的顺序指定与预期构造函数的参数相对应的所有列。任何作为构造函数结果返回的实体都将处于新状态或分离状态,具体取决于是否为构造的对象检索了主键。

example

例子

Query q = em.createNativeQuery(
        "SELECT c.id, c.name, COUNT(o) as orderCount, AVG(o.price) AS
        avgOrder" +
        "FROM Customer c, Orders o " +
                "WHERE o.cid = c.id " +
                "GROUP BY c.id, c.name",
        "CustomerDetailsResult");

@SqlResultSetMapping(name = "CustomerDetailsResult",
        classes = {
                @ConstructorResult(targetClass = com.acme.CustomerDetails.class,
                        columns = {
                                @ColumnResult(name = "id"),
                                @ColumnResult(name = "name"),
                                @ColumnResult(name = "orderCount"),
                                @ColumnResult(name = "avgOrder", type = Double.class)})
        })