【问题标题】:Liquibase and spring jpaLiquibase 和 spring jpa
【发布时间】:2016-03-16 01:18:36
【问题描述】:

我在 linux 环境中部署 postgres 时遇到问题,但我不确定它是否相关。

  • Linux 版本:9.3.11
  • Windows 版本:9.5

我得到的错误:

2016-03-15_19:19:40.478 [http-nio-9090-exec-3] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 42P01
2016-03-15_19:19:40.479 [http-nio-9090-exec-3] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ERROR: relation "rbac_roles" does not exist
  Position: 125
2016-03-15_19:19:40.520 [http-nio-9090-exec-3] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
org.postgresql.util.PSQLException: ERROR: relation "rbac_roles" does not exist
  Position: 125

在我的由 Liquibase 创建的 windows 环境表定义如下:

-- Table: public.rbac_roles

-- DROP TABLE public.rbac_roles;

CREATE TABLE public.rbac_roles
(
  tenantid character varying(255) NOT NULL,
  id integer NOT NULL DEFAULT nextval('rbac_roles_id_seq'::regclass),
  name character varying(255) NOT NULL,
  urlprefix character varying(255),
  CONSTRAINT pk_rbac_roles PRIMARY KEY (id),
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public.rbac_roles
  OWNER TO postgres;

在我的 linux(问题)环境中,由 Liquibase 创建的表定义如下:

-- Table: public.rbac_roles

-- DROP TABLE public.rbac_roles;

CREATE TABLE public.rbac_roles
(
  tenantid character varying(255) NOT NULL,
  id serial NOT NULL,
  name character varying(255) NOT NULL,
  urlprefix character varying(255),
  CONSTRAINT pk_rbac_roles PRIMARY KEY (id),
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public.rbac_roles
  OWNER TO postgres;

Spring Jpa 对象如下所示:

@Entity(name = "rbac_roles")
public class Role implements HasTenantId {
    @Id
    @SequenceGenerator(name="roles_seq", sequenceName = "rbac_roles_id_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "roles_seq")
    private int id;

...

Liquibase 配置如下所示:

 <createTable tableName="rbac_roles">
            <column name="tenantid" type="varchar(255)">
                <constraints primaryKeyName="pk_roles" nullable="false" />
            </column>
            <column name="id" autoIncrement="true" type="integer">
                <constraints primaryKey="true" nullable="false" />
            </column>
            ...
        </createTable>

成功创建表 rbac_roles 但 ID 看起来不同

我做错了什么,为什么行为不同?

【问题讨论】:

  • 您发布的实体RolePermissionrbac_role 表无关。可能是,您想发布与rbac_role 表相关的不同实体?
  • 你说得对,谢谢,我已经编辑了帖子
  • 更新:在 linux 上更新到 9.5 版似乎问题已解决(Id 仍然是串行的)

标签: spring hibernate postgresql jpa liquibase


【解决方案1】:

在您的 JPA 注释中,您使用的是序列生成器,但在您的变更日志中,ID 属性有一个自动增量类型

<createTable tableName="rbac_roles">
    <column name="tenantid" type="varchar(255)">
        <constraints primaryKeyName="pk_roles" nullable="false" />
    </column>

    <!-- HERE you specify autoincrement -->
    <column name="id" autoIncrement="true" type="integer">
        <constraints primaryKey="true" nullable="false" />
    </column>

    ...

</createTable>

很混乱,不知道为什么这段代码在windows和linux上生成不同的表,可能你的postgresql不是同一个版本...

请修复您的更改日志并输入以下内容

<createTable tableName="rbac_roles">
    <column name="tenantid" type="varchar(255)">
        <constraints primaryKeyName="pk_roles" nullable="false" />
    </column>

    <!-- Removed autoincrement -->
    <column name="id" type="integer">
        <constraints primaryKey="true" nullable="false" />
    </column>

    ...

</createTable>

<!-- HERE I specify sequence -->
<createSequence sequenceName="rbac_roles_id_seq" incrementBy="1"/>

这样,它应该在两个系统上生成相同的表。

【讨论】:

  • 谢谢!检查它,同时我用版本Linux版本更新帖子:9.3.11 Windows版本:9.5
猜你喜欢
  • 2021-06-12
  • 2017-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多