简体   繁体   English

JPA、@Transient 字段和本地查询映射

[英]JPA, @Transient field and Native Query mapping

I have this entity:我有这个实体:

@Entity
@Table(name = "entry")
@SqlResultSetMapping(
    name = "viewEntry",
    entities =
    @EntityResult(entityClass = ViewEntry.class,
            fields = {
                    @FieldResult(name="id", column = "id"),
                    @FieldResult(name="guid", column = "guid"),
                    @FieldResult(name="link", column = "link"),
                    @FieldResult(name="descr", column = "descr"),
                    @FieldResult(name="pubDate", column = "pub_date"),
                    @FieldResult(name="read", column = "my_read")
            }
    )
)
public class ViewEntry implements Serializable {
    @Id
    private Integer id;
    private String guid;
    private String link;
    private String descr;
    private Date pubDate;
    @Transient
    private Boolean read;
}

The field read resides in another table, So I made it transient, to prevent JPA mapping errors. read的字段位于另一个表中,因此我将其设为瞬态,以防止 JPA 映射错误。 To retrieve entity's content I want to use native query that looks like this:要检索实体的内容,我想使用如下所示的本机查询:

select id,guid,link,descr,pub_date,feed_id,user_id,is_read as my_read from entry join user_to_entry ....
-- skipped dynamic part of query

The problem is that I have no idea how to map native queries to my entity.问题是我不知道如何将本机查询映射到我的实体。 In particular I don't know if the @Transient field will be ignored by EntityManager.特别是我不知道 EntityManager 是否会忽略 @Transient 字段。 Help, please.请帮忙。

To map results of a native query you can use SqlResultSetMapping http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html 要映射本机查询的结果,可以使用SqlResultSetMapping http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html

Without a mapping if the columns in the native query matches the attribute name or if they are mapped with @Column in the Entity then you don't need a mapping. 如果本机查询中的列与属性名称匹配,或者如果它们与Entity中的@Column映射,则没有映射。则不需要映射。

You can map the field in another table by using @SecondaryTable in your entity. 您可以通过使用实体中的@SecondaryTable来映射另一个表中的字段。 Something like this: 像这样:

...
@SecondaryTable(name="user_to_entry", 
        pkJoinColumns=@PrimaryKeyJoinColumn(name="entry_id"))
public class ViewEntry implements Serializable {
    @Id
    private Integer id;
    private String guid;
    private String link;
    private String descr;
    @Column(name="pub_date")
    private Date pubDate;
    @Column(table = "user_to_entry")
    private Boolean read;
}

If you can't do that for some reason, you can map the native SQL results to entity like this 如果由于某种原因无法执行此操作,则可以将本机SQL结果映射到这样的实体

em.createNativeQuery("<native SQL>", ViewEntry.class)

but I don't think this will map a transient field (I could be wrong, haven't tested it). 但是我认为这不会映射瞬态场(我可能是错的,尚未测试)。

Third option is to use @SqlResultSetMapping , but I'm also not sure this will work with transient fields. 第三种选择是使用@SqlResultSetMapping ,但我也不确定这是否适用于瞬态字段。 Check this for an example. 查看示例。

In the worst case, you can create a view with your join and map your view like a normal table.在最坏的情况下,您可以使用连接创建一个视图,并像普通表一样映射您的视图。 You just need to delete that new table that hibernate will create for you.您只需要删除 hibernate 将为您创建的新表。

https://www.w3schools.com/sql/sql_view.asp https://www.w3schools.com/sql/sql_view.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM