Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Hibernate将实体映射到物化视图_Java_Sql_Hibernate_Postgresql_Materialized Views - Fatal编程技术网

Java 使用Hibernate将实体映射到物化视图

Java 使用Hibernate将实体映射到物化视图,java,sql,hibernate,postgresql,materialized-views,Java,Sql,Hibernate,Postgresql,Materialized Views,我需要使用Hibernate将(PostgreSQL)物化视图映射到@Entity。如果hbm2ddl配置为update值,则Hibernate总是尝试创建新的SQL表。只有在视图是物化的情况下才会发生这种情况,否则(对于非物化视图),它就可以正常工作 映射实体 @Entity @Immutable @Cache (usage=CacheConcurrencyStrategy.READ_ONLY) @Table(name = "quasar_evaludated_function") publi

我需要使用Hibernate将(PostgreSQL)物化视图映射到
@Entity
。如果
hbm2ddl
配置为
update
值,则Hibernate总是尝试创建新的SQL表。只有在视图是物化的情况下才会发生这种情况,否则(对于非物化视图),它就可以正常工作

映射实体

@Entity
@Immutable
@Cache (usage=CacheConcurrencyStrategy.READ_ONLY)
@Table(name = "quasar_evaludated_function")
public class EvaluatedAuditor {

    private long id;

    private boolean qsAuditor;

    // getter setters ...

}
SQL物化视图

CREATE materialized VIEW quasar_evaludated_function
AS SELECT a.id AS id,
          (SELECT Count(code)
           FROM   quasar_qs_auditor_code code
           WHERE  code.auditor_id = a.id
                  AND code.is_granted = TRUE) > 0 AS is_qs_auditor
   FROM   quasar_auditor a;  
日志

ERROR 2015-01-14 21:16:17 SchemaUpdate:execute(line 261) - HHH000388: Unsuccessful: create table quasar_evaludated_function (id int8 not null, is_clinical_expert boolean, is_product_assessor_a boolean, is_product_assessor_r boolean, is_product_specialist boolean, is_qs_auditor boolean, is_responsible_clinician boolean, is_technical_expert boolean, primary key (id))
ERROR 2015-01-14 21:16:17 SchemaUpdate:execute(line 262) - ERROR: relation "quasar_evaludated_function" already exists
如果将
hbm2ddl
选项配置为
validate
将引发异常


谢谢您的帮助。

在这种情况下,您不应该使用
hibernate.hbm2ddl.auto
。实际上,您应该始终支持在自动化数据库更新过程的同时使用

因为您使用的是特定于数据库的物化视图,所以hibernate模式生成器对您毫无帮助。因此,您唯一的选择是特定于数据库的增量脚本


您仍然可以为PostgreSQL和内存数据库(例如HSQLDB或H2)中的集成测试维护单独的脚本。

这不是一个好的解决方案,但它可以工作。我刚刚创建了一个新视图,它引用了一个物化视图。如果不需要自动生成架构,则应查看

CREATE MATERIALIZED VIEW quasar_evaludated_function_mv AS select 
                a.id as id,
                (select count(f) from quasar_qs_auditor_code f where  f.auditor_id = a.id and f.is_granted = true) >  0 as is_qs_auditor,
                (select count(f) from quasar_product_assessor_a_code f where  f.auditor_id = a.id and f.is_granted = true) >  0 as is_product_assessor_a,
                (select count(f) from quasar_product_assessor_r_code f where  f.auditor_id = a.id and f.is_granted = true) >  0 as is_product_assessor_r, 
                (select count(f) from quasar_product_specialist_code f where  f.auditor_id = a.id and f.is_granted = true) >  0 as is_product_specialist,
                (select count(f) from quasar_technical_expert_code f where  f.auditor_id = a.id and f.is_granted = true) >  0 as is_technical_expert,
                (select count(f) from quasar_clinical_expert_code f where  f.auditor_id = a.id and f.is_granted = true) >  0 as is_clinical_expert, 
                (select count(f) from quasar_responsible_clinician_code f where  f.auditor_id = a.id and f.is_granted = true) >  0 as is_responsible_clinician
                from quasar_auditor a;

    CREATE VIEW quasar_evaludated_function  AS SELECT mv.* from quasar_evaludated_function_mv mv;

当然,在生产中是用来验证价值的。但在本例中,验证失败。您认为将PostgreSQL物化视图映射到
@Entity
是不可能的吗?是可能的。Hibernate已经支持了十多年。但是您不能使用自动模式生成,即使是用于验证。用增量脚本替换它。是的,谢谢。但我会寻找另一个可能的解决方案(如果有的话),因为hdm2ddl是一个非常舒适的特性,毕竟它并没有那么糟糕。非常感谢。