7
votes

I have a JPA entity that I use as the result class of a native query. As such, the entity is not valid per se (as it does not have a table). I use Hibernate 4.1.x as my JPA provider, which performs a schema validation during startup and consequently fails (I did not specify an explicit table):

org.hibernate.HibernateException: Missing table: MyEntity
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1272)
    [...]

Is there a way to turn off schema validation for just a single entity (using JPA or hibernate annotations or a change of persistence.xml)?

Edit: I can completely avoid using any entity as a result, but then Hibernate will return a List<Object[]> as query result, which technically works, but is a little ugly to use:

Query query = entityManager.get().createNativeQuery("SELECT node, last_update FROM mm_repl_monitoring.my_mm_nodes");
List<Object[]> statuses = query.getResultList();

In other words: It would be nice if there was some mapping support that can be used even for native queries that map to non-Entity classes.

2

2 Answers

0
votes

I'm not sure as to why you would register that class as being an entity: it's a custom class and as such it does not have to be attached nor detached from the hibernate/jpa session.

0
votes

The solution that worked for me was to remove the hibernate.hbm2ddl.auto property from persistence.xml. Of course, this disables schema validation for all the other entities, but I couldn't find something else in JPA 2.0.