我需要将结果字段映射到实体字段。
@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
}
我知道JPA会忽略@Transient
但是当我删除它时我得到了这个例外:
org.hibernate.HibernateException: Missing column: customName in my_db.my_table
我的查询是这样的:
em.createNativeQuery("SELECT * FROM MyView WHERE customer=:c","MyResult")
.setParameter("c", 12345).getResultList();
表:
id int(11) AI PK
reference varchar(16)
model smallint(5)
content text
hash char(64)
我的观点:
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;
如何从ViewResult映射customName?