简体   繁体   中英

JPA Audit entity direct query

I would like to query audit entity in my JPA environment.

At first I do AuditQuery and it works well, but now I need more advance query witch I can't do with AuditQuery .

Now I need something like

 this.em.createQuery("FROM ENTITY_AUD").getResultList();

but I get error :

 QuerySyntaxException: ENTITY_AUD is not mapped [ENTITY_AUD]

I understand that this is due that I don't have entity with all properties, but I don't want to have entity because it is audit entity.

Is there a way around it? For me it would be ok to get List of Object.

You can always create native SQL query in JPA. Replace createQuery with createNativeQuery in your code:

List<Object[]> list = 
    this.em.createNativeQuery("SELECT * FROM ENTITY_AUD").getResultList();

Add a NamedQuery in your Audit class

@Entity
@Table(name = "ENTITY_AUD")
@NamedQueries({
@NamedQuery(name = "Audit.findAll", query = "SELECT a FROM Audit a")})
public class Audit implements Serializable {
         ...
}

then use the named query

List<Audit> auditList = entityManager.createNamedQuery("Audit.findAll").getResultList();

Read : Implementing a JPA Named Query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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