8

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.

Thilo-Alexander Ginkel
  • 6,898
  • 10
  • 45
  • 58
  • see my answer here: https://stackoverflow.com/questions/6212144/how-to-disable-schema-validation-in-hibernate-for-certain-entities/47377231#47377231 – tomerz Nov 19 '17 at 13:13

3 Answers3

0

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.

Kurt Du Bois
  • 7,550
  • 4
  • 25
  • 33
0

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.

Radu Cosma
  • 11
  • 3
0

What you can do is take control of the entire Schema generation to give you a string of the sql to be executed, and then remove the lines referencing your table.

Its a big answer, but that is the way I have it now.

It involves adjusting how Hibernate executes the generated DDL, serving it to you in a file for you to execute. This file is opened, lines iterated and some removed.

mjs
  • 21,431
  • 31
  • 118
  • 200