English 中文(简体)
JPA - 实体从计算栏目中分类财产?
原标题:JPA - Setting entity class property from calculated column?

I m 刚刚在位于玻璃鱼3的简便的 Java网关上日本宇宙航行局(Persistence Provide is EclipseLink)。 迄今为止,我真心会对此表示欣赏(在净豆/渔类之间相互交流的尝试除外),但我想做的是,我不敢肯定如何做。

我将一个实体类别(第1条)绘制成数据库表。 我试图对数据库进行查询,将计算一栏归来,但我可以说明如何确定某类财产,以便在我提出质询时,该财产以一栏价值填充。

如果我定期进行“假冒、称职、从文章中找人”的询问,我获得一份物品的罚款清单,其中填满了id、所有权和遗体。 罚款。

然而,如果我采取以下行动:

Query q = em.createNativeQuery("select id,title,shorttitle,datestamp,body,true as published, ts_headline(body,q, ShortWord=0 ) as headline, type from articles,to_tsquery( english ,?) as q where idxfti @@ q order by ts_rank(idxfti,q) desc",Article.class);

(这是使用Pogres的tsearch2进行全文检索,它具有专用功能,因此使用土著Query进行Im)。

您可以看到一栏计算,称为头线。 我如何在我的物品类别中增加一线财产,以便它能够因这种争s而居住?

至今,我尝试把它定为“透明”,但这只是随着时间的推移而结束。

最佳回答

这样做可能没有很好的方法,只有人工操作:

Object[] r = (Object[]) em.createNativeQuery(
    "select id,title,shorttitle,datestamp,body,true as published, ts_headline(body,q, ShortWord=0 ) as headline, type from articles,to_tsquery( english ,?) as q where idxfti @@ q order by ts_rank(idxfti,q) desc","ArticleWithHeadline")
    .setParameter(...).getSingleResult();

Article a = (Article) r[0];
a.setHeadline((String) r[1]);

-

@Entity
@SqlResultSetMapping(
    name = "ArticleWithHeadline",
    entities = @EntityResult(entityClass = Article.class),
    columns = @ColumnResult(name = "HEADLINE"))
public class Article {
    @Transient
    private String headline;
    ...
}
问题回答

AFAIK, JPA don t 提供计算特性的标准化支持。 在Hibernate,人们将使用Formula ,但EclipseLink没有直接等量。 James Sutherland在Re: 虚拟栏目(Hibernate@Formula of Hibernate)上提出了一些建议。

There is no direct equivalent (please log an enhancement), but depending on what you want to do, there are ways to accomplish the same thing.

EclipseLink defines a TransformationMapping which can map a computed value from multiple field values, or access the database.

You can override the SQL for any CRUD operation for a class using its descriptor s DescriptorQueryManager.

You could define a VIEW on your database that performs the function and map your Entity to the view instead of the table.

You can also perform minor translations using Converters or property get/set methods.

也可查阅enhance request,该申请在评论中采用DescriptorEventListener的解决办法。

这当然都是非标准联合军。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签