简体   繁体   English

jpa非管理实体

[英]jpa non managed entities

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 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 . audit_log未作为JPA enity映射到任何类,我严格地不能为n*_audit_logs创建n个类。

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. 试图选择新功能,但不确定...因此任何帮助表示赞赏。

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. 由于审计日志表共享相同的列,因此可以创建一个“统一”这些表并将单个Java类映射到该视图的视图。 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. 作为替代方案,使用本机查询将是一个不错的选择。

EDIT : 编辑

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. 1)如果您的审核日志已经是视图,则可以根据其他视图创建视图,如果您不想为每个视图创建映射Java类。 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. 只记得添加一个值为1的虚拟列,如果该行来自“第一个”审核日志,则为2如果它来自第二个,依此类推,这样您就可以将它们分开。

2) In order to use native queries, assuming your persistence provider is Hibernate, you can do like in this example: 2)为了使用本机查询,假设您的持久性提供程序是Hibernate,您可以在此示例中执行以下操作:

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);

where MyResult is declared as follows: 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;
    }

}

and the persons table is like this (MySQL syntax): persons表是这样的(MySQL语法):

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`)
);

You can easily adapt this example to your needs, just replace persons and MyResult with what you want. 您可以轻松地根据您的需求调整此示例,只需将personsMyResult替换为您想要的内容即可。

The aliases in the sql query is automatically converted to upper case and its looking for the setter in Upper case as a result org.hibernate.PropertyNotFoundException Exception is thrown. sql查询中的别名会自动转换为大写,并以大写形式查找setter作为结果org.hibernate.PropertyNotFoundException抛出异常。 Any suggestions would be greatly appreciated. 任何建议将不胜感激。

For instance, the below statement is looking for the setter ID instead of Id/id (Could not find setter for ID on class Data) 例如,下面的语句是查找setter ID而不是Id / id(无法在类Data上找到ID的setter)

List<Data> result = entityManager.unwrap(Session.class) 
            .createSQLQuery("Select id as id from table") 
            .setParameter("day", date.getDayOfMonth()) 
            .setParameter("month", date.getMonthOfYear()) 
            .setParameter("year", date.getYear()) 
           .setResultTransformer(Transformers.aliasToBean(Data.class))
           .list();

class Data {
  Integer id;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
   this.id = id;
  }
}

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

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