zoukankan      html  css  js  c++  java
  • SpringBoot连接PostgreSQL

    这个 org.postgresql.jdbc.PgConnection.createClob() 方法尚未被实作


    application.properties

        spring.datasource.platform=postgres  
        spring.datasource.url=jdbc:postgresql://10.18.35.155:5432/test1?useSSL=false  
        spring.datasource.username=abc  
        spring.datasource.password=abc  
        spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false  
        spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
    ---------------------

    参考https://stackoverflow.com/questions/49683434/spring-boot-app-fails-method-org-postgresql-jdbc4-jdbc4connection-createclob-i

    https://github.com/pgjdbc/pgjdbc/issues/1102

    ---------------------------------------

    参考:http://www.cnblogs.com/rim99/p/8672909.html

    首先,配置好POM,使用默认配置启动的时候会报一个错误。

    java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented.

    根据网上查到的资料,这个是由于:Hibernate尝试验证PostgreSQL的CLOB特性,但是PostgreSQL的JDBC驱动并没有实现这个特性,所以抛出了异常。

    解决方法是:关闭这个特性的检测。在配置文件里添加如下内容:

    spring:
      jpa:
        # Disable feature detection by this undocumented parameter. Check the org.hibernate.engine.jdbc.internal.JdbcServiceImpl.configure method for more details.
        properties.hibernate.temp.use_jdbc_metadata_defaults: false
        # Because detection is disabled you have to set correct dialect by hand.
        database-platform: org.hibernate.dialect.PostgreSQL9Dialect

    接下来,服务可以正常启动了。但是在存表的时候却有一个报错:

    org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist

    这个报错的原因是领域类里面设定了主键为自增类型,标记了注解@GeneratedValue。这个注解的默认自增类型是GenerationType.AUTO。如果注解的自增类型是AUTO,对于PostgreSQL,Hibernate会默认识别为GenerationType.SEQUENCE。这时就需要一张序列表。如果Hibernate找不到指定的序列表或者没有指定序列表,就会查找名为hibernate_sequence的序列表。如果数据库里没有这张序列表,就会报上面这个错。

    不像MySQL的列可以有AUTO_INCREMENT属性表示自增,PostgreSQL默认使用的是序列sequence来实现类似功能。sequence是一种特殊的单行表,用于生成序列数字。

    看这里:"hibernate_sequence" does not exist

    哎,本来一张表的事情,要用两张表来完成,好麻烦~~~

    不过,PostgreSQL已经完善了这个问题,提供一种“伪“类型——serial。在建表的时候将主键声明为此类型,然后将注解的自增类型修改为GenerationType.IDENTITY,问题就解决了。

    为什么说是“伪“类型呢?PostgreSQL的文档说的很清楚。

    建表时候,使用语句

    CREATE TABLE tablename (
        colname SERIAL
    );

    其实就相当于

    CREATE SEQUENCE tablename_colname_seq;
    CREATE TABLE tablename (
        colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
    );
    ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

    完全可以把serial看作是PostgreSQL的语法糖。

    ------------------------------------

    检索数据时出现relation ‘table_name‘’ does not exist的问题

  • 相关阅读:
    通过JDBC连接hive
    大数据技术Hadoop入门理论系列之一----hadoop生态圈介绍
    lightning mdb 源代码分析(5)-事务控制
    lightning mdb 源代码分析(4)—MVCC/COW
    lightning mdb 源代码分析(1)
    lightning mdb 源代码分析(2)
    Ubuntu 15.10安装elementary desktop
    lightning mdb 源代码分析系列(3)
    async和enterproxy控制并发数量
    Express4.x API (一):application (译)
  • 原文地址:https://www.cnblogs.com/Amos-Turing/p/9939316.html
Copyright © 2011-2022 走看看