jpa call readonly composite table but getting “Exc

2019-07-20 14:27发布

问题:

In a Spring 3 app a controller is calling a JpaCollectorManager with calls a JpaCollectorInfoDao to get a list which is defined by a nativequery. The query calls 2 seperate tables which uses sql and jpql because I need to use a postgresql feature not implemented in jpql. When the controller tries to file the list I get the following error message:

Exception [EclipseLink-6007] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.QueryException Exception Description: Missing descriptor for [CollectorInfo].
Query: ReadAllQuery(referenceClass=CollectorInfo sql="select distinct ON ( col.collector_id,pst.process_source_type ) col.*,pst.process_source_timestamp,pst.process_source_type from perform_schema.collector col join perform_schema.process_set pst on pst.collector_id = col.collector_id order by col.collector_id, pst.process_source_type,pst.process_source_timestamp desc ")

The controller Java has the following call:

List<CollectorInfo> ps = this.collectorInfoManager.getLatestCollectorInfo();

The JpaCollectorInfoManager.java has this:

public List<CollectorInfo> getLatestCollectorInfo()
{
  return collectorInfoDao.getLatestCollectorInfo();
}

The JpaCollectorInfoDao.java:

  @Override
  @Transactional
  public List<CollectorInfo> getLatestCollectorInfo() {
    Query query = entityManager.createNativeQuery( ( "select     distinct ON ( col.collector_id," +
    "pst.process_source_type ) " + 
    "col.*," +
    "pst.process_source_timestamp," + 
    "pst.process_source_type " + 
    "from      perform_schema.collector col " +
    "join      perform_schema.process_set pst " +
     "on       pst.collector_id = col.collector_id " +
    "order by  col.collector_id, " +
    "pst.process_source_type," +
    "pst.process_source_timestamp desc " ),
     CollectorInfo.class );
    return ( (List<CollectorInfo>) query.getResultList() );
  }

The CollectorInfo class does not have an @Entity defined. If I set the @Entity defined then it tells me that the Table cannot be resolved (which is correct since the there is no actual table). I have tried all sorts of permutations and cannot seem to make this needle thread.

回答1:

Not sure what you are trying to do exactly?

You need to map the class as an Entity in order to be able to select instances of it.

Either, do not include the class, the native SQL query will an Object[] of the data, which you can map in your own code to your class.

Or map it as an Entity excepting the data that you are returning. The @Table will not be relevant as you are mapping the object to the query results. This should not cause any errors though, unless you are auto creating table or using integrity checker.

Or map the objects to the table correctly. Then use a fetch join, or batch fetch to optimize your retrieval if required.