JPA:SqlResultSetMapping 和瞬态字段

Ath*_*nor 5 java mapping jpa

我需要将结果字段映射到实体字段。

@Entity
@Table (name="my_table")
@Cacheable(true)
@Data
@SqlResultSetMapping(name="MyResult", 
    entities = {
        @EntityResult(entityClass=MyClass.class,
        fields   = {@FieldResult(name="customName", column="customName")})
    }, 
    columns  = {@ColumnResult(name="customName")})
public class MyClass implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private short model;
    private String reference;
    @Lob
    @Column(columnDefinition="TEXT")
    private String content = "";
    @Column(length=64, columnDefinition="CHAR(64)")
    private String hash;

    @Transient
    private String customName = ""; //this is the column I want to map
}
Run Code Online (Sandbox Code Playgroud)

我知道@TransientJPA 会忽略它,但是当我删除它时,我得到了这个异常:

org.hibernate.HibernateException: Missing column: customName in my_db.my_table
Run Code Online (Sandbox Code Playgroud)

我的查询是这样的:

em.createNativeQuery("SELECT * FROM MyView WHERE customer=:c","MyResult")
    .setParameter("c", 12345).getResultList();
Run Code Online (Sandbox Code Playgroud)

桌子:

id  int(11) AI PK
reference   varchar(16)
model   smallint(5)
content text
hash    char(64)
Run Code Online (Sandbox Code Playgroud)

我的看法:

SELECT id,model,reference,content,hash,name AS customName,customer FROM (
(SELECT p.*, NULL AS name, customer FROM p_part p, p_order o, general_order go WHERE o.part=p.id AND o.id=go.id)
UNION
(SELECT p.*, name, customer FROM p_part p, p_order o, p_name n, general_order go WHERE o.part=p.id AND n.part=p.id AND o.id=go.id)
) u ORDER BY
    model, reference;
Run Code Online (Sandbox Code Playgroud)

如何从 ViewResult 映射 customName?