5

How can I map object to materialized view and keep <prop key="hibernate.hbm2ddl.auto">validate</prop>?

When launch webapp, I get this exception on startup:

Caused by: org.hibernate.HibernateException: Missing table: subjects_lp at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1302) at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155) at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:512) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1797) ...

According to sources, it looks like hibernate asks only for types "VIEW" and "TABLE" in DatabaseMetadata#getTableMetadata, while postgresql stores it like "MATERIALIZED VIEW" (checked with plain jdbc).

Surprisingly, I couldn't find in google some info about this error. Is there way to keep hibernate validation on startup, maybe there is way to keep one table/entity unvalidated?

Hibernate version: 4.2.12.Final
Postgresql driver: 9.3-1101-jdbc41

Entity doesn't have any specific annotation, only

@javax.persistence.Entity @javax.persistence.Table(name = "table_name", schema = "schema")

astafev.evgeny
  • 466
  • 4
  • 21

2 Answers2

3

It's not a driver problem, it's a hibernate bug.

See this bug https://hibernate.atlassian.net/browse/HHH-9602

user2435612
  • 136
  • 1
  • 1
  • 6
2

For those coming here using spring-boot-starter-data-jpa and using ddl-auto: validate in their application.yml file, you'll need to add hibernate.hbm2dll.extra_physical_table_types: "MATERIALIZED VIEW" to your properties:

spring:
  datasource:
    ~~~
  jpa:
    hibernate:
      ddl-auto: validate
    ~~~
    properties:
      hibernate:
        ~~~
        hbm2dll:
          extra_physical_table_types: "MATERIALIZED VIEW"

I mention this because it took me a number of tries to get that put in the right place.

ScrappyDev
  • 2,307
  • 8
  • 40
  • 60
  • 1
    Thanks. Exactly what I needed, but for some reason this info is not easy to find. – Joel Dec 08 '21 at 13:36
  • This didn't work for me as I have a plain old 'view' and I could not find alternative values that would work instead of "MATERIALIZED VIEW" such as "VIEW" Is there a place where those valid literals are listed? The @ Subselect instead of @ Table worked for me to pass the 'validate' rule. – Randy Mar 15 '22 at 15:24