Disabling JPA Hibernate schema validation for a single entity
Asked Answered
C

3

9

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.

Cepheus answered 26/7, 2012 at 12:51 Comment(1)
see my answer here: #6212644Laager
S
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.

Sora answered 26/7, 2012 at 12:55 Comment(1)
Hibernate won't recognize my @NamedNativeQuery if I don't register the entity it is attached to...Cepheus
F
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.

Fianna answered 11/7, 2016 at 12:31 Comment(0)
O
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.

Olivia answered 22/2, 2023 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.