【问题标题】:Error when using sequence for primary key in postgres and Hibernate在 postgres 和 Hibernate 中使用主键序列时出错
【发布时间】:2020-08-30 20:19:31
【问题描述】:

我使用 JPA/Hibernate 和 postgres 作为我的数据库

我在 postgres 中创建了一个这样的序列:

CREATE SEQUENCE player_sequence
INCREMENT 20
START 1;

我希望 Hibernate 使用上述序列作为主键。

实体的id部分是:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "player_seq")
@SequenceGenerator(name = "player_seq", sequenceName = "player_sequence", allocationSize = 20)
private Integer id;

当我创建一个新播放器并尝试通过EntityManger.persist 方法保存它时,我收到以下错误:

Hibernate:从 player_sequence 中选择 next_val 作为 id_val 进行更新 org.hibernate.id.enhanced.TableStructure$1$1 执行 错误:无法读取 hi 值 org.postgresql.util.PSQLException:错误:列“next_val”不存在

我不明白我做错了什么

编辑

这是persistence.xml的内容:

【问题讨论】:

  • 能否请您显示您的休眠配置。你用的是什么休眠版本?
  • 我在上面添加了有问题的persistence.xml文件内容

标签: postgresql hibernate jpa


【解决方案1】:

尝试以这种方式更正您的persistence.xml

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
             version="2.2">

    <persistence-unit name="my-persistence-unit">
        <description>...</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>


        <properties>
            <!--  It was wrong !!!  -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL10Dialect" />

            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/basketball" />
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <property name="javax.persistence.jdbc.password" value="1234" />
            
            <property name="hibernate.show_sql" value="true" />

        </properties>

    </persistence-unit>

</persistence>

【讨论】:

    猜你喜欢
    • 2017-09-06
    • 2015-08-03
    • 1970-01-01
    • 2014-07-04
    • 2012-05-13
    • 2012-11-18
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多