简体   繁体   English

Select 来自 QueryDSL 中的自定义表

[英]Select from custom table in QueryDSL

I am using QueryDSL-JPA on JBoss and want at one point select an entity from a different table than the one I am normally using and annotating via @Table.我在 JBoss 上使用 QueryDSL-JPA,并希望在某一时刻 select 来自与我通常使用的表不同的表中的实体,并通过 @Table 进行注释。 I do not want to create a separate Java class since in the end, it should return me the same class I am using.我不想创建一个单独的 Java class 因为最后它应该返回我使用的相同 class 。

Any idea on how to achieve this?关于如何实现这一目标的任何想法? Alternatively via the EntityManager or QueryDSL-SQL or Hiberate.或者通过 EntityManager 或 QueryDSL-SQL 或 Hiberate。 Using this approach for accessing historic tables should not be so uncommon...使用这种方法来访问历史表应该不会那么罕见......

The best way I could see was via custom SQL in JPA.我能看到的最好方法是通过 JPA 中的自定义 SQL。 QueryDSL does not allow this: QueryDSL 不允许这样做:

   final AbstractEntityPersister persister = getEntityPersister();
   final String[] idColumnNames = persister.getIdentifierColumnNames();
   Validate.isTrue(idColumnNames.length == 1);

   final String sql = "select * from " + getEntityPersister().getTableName() + HIST_TABLE_SUFFIX + " where " + idColumnNames[0] + " = ?";
   @SuppressWarnings("unchecked")
   final List<E> histList = em.createNativeQuery(sql, getEntityClass()).setParameter(1, id).getResultList();

using使用

@PersistenceContext(name = "MyDS")
protected EntityManager em;

protected AbstractEntityPersister getEntityPersister() {
    final MetamodelImplementor mm = (MetamodelImplementor) em.getEntityManagerFactory().getMetamodel();
    return (AbstractEntityPersister) mm.entityPersister(getEntityClass());
}

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

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