jpa非管理实体 [英] jpa non managed entities

查看:158
本文介绍了jpa非管理实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我必须触发这样的查询:

Let's say I have to fire a query like this:

Select primarykey, columnname, old_value, new_value from first_audit_log;

Select primarykey, columnname, old_value, new_value from second_audit_log;

Select primarykey, columnname, old_value, new_value from third_audit_log; ...so on

audit_log 不是映射为JPA enity到任何类,我严格不能创建 n n 的数量 * _ audit_logs

audit_log is not mapped as JPA enity to any class and I strictly can't create n number of classes for n number of *_audit_logs.

使用本机查询功能,我如何才能将其映射到泛型类?尝试选择新功能,但不确定...因此,任何帮助都表示赞赏。

Using native query feature, how best I can map this to a generic class? Trying to SELECT NEW feature, but not sure... Hence any help is appreciated.

推荐答案

由于审计日志表共享在相同的列中,您可以创建一个统一这些表并将单个Java类映射到该视图的视图。我相信你可以,因为你不需要写更新,我想。

Since your audit logs tables share the same columns, you can create a view that "unifies" those tables and map a single Java class to that view. I believe you can, since you don't need to write updates, I guess.

作为替代方案,使用本机查询将是一个不错的选择。

As an alternative, using native queries would be a good choice.

编辑

1)如果审核日志已经如果您不想为每个视图创建映射Java类,则可以基于其他视图创建视图。只记得添加一个值为 1 的虚拟列,如果该行来自第一个审核日志, 2 如果它来自第二个,依此类推,那么你可以将它们分开。

1) If your audit logs are already views, you can create a view based on other views, if you don't want to create a mapping Java class for each of them. Just remember to add a dummy column that has value 1 if the row comes from the "first" audit log, 2 if it comes from the second, and so on, so you can set them apart.

2)为了使用本机查询,假设您的持久性提供程序是Hibernate,那么可以这样做:

2) In order to use native queries, assuming your persistence provider is Hibernate, you can do like in this example:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
EntityManager em = emf.createEntityManager();

Session sess = em.unwrap(Session.class); // <-- Use Hibernate-specific features
SQLQuery query = sess.createSQLQuery(
   "SELECT AVG(age) AS averageAge, AVG(salary) as averageSalary FROM persons");

query.setResultTransformer(Transformers.aliasToBean(MyResult.class));
MyResult result = (MyResult) query.list().get(0);

其中 MyResult 声明如下:

public class MyResult {

    private BigDecimal averageAge;
    private BigDecimal averageSalary;

    public BigDecimal getAverageAge() {
        return averageAge;
    }
    public void setAverageAge(BigDecimal averageAge) {
        this.averageAge = averageAge;
    }
    public BigDecimal getAverageSalary() {
        return averageSalary;
    }
    public void setAverageSalary(BigDecimal averageSalary) {
        this.averageSalary = averageSalary;
    }

}

表是这样的(MySQL语法):

and the persons table is like this (MySQL syntax):

CREATE TABLE `persons` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(255) NOT NULL,
  `lastname` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  `salary` int(11) NOT NULL,
  PRIMARY KEY (`id`)
);

您可以根据需要轻松调整此示例,只需替换 MyResult

You can easily adapt this example to your needs, just replace persons and MyResult with what you want.

这篇关于jpa非管理实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆