2016-03-16 68 views
0

我在linux环境下部署postgres时遇到问题,但我不确定它是否有关系。Liquibase and spring jpa

  • 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中创建(prblematic)环境表定义看起来如下:

-- 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 config看起来像这样:

<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 cr成功地吃了,但ID看起来不一样

我在做什么错,为什么行为是不同的?

+0

您发布'RolePermission'的实体与'rbac_role'表无关。可能是,你想发布一个与'rbac_role'表有关的不同实体吗? –

+0

你是对的,谢谢,我编辑了这个职位 – Igal

+0

更新:在Linux上更新到版本9.5似乎是问题,解决了(Id仍然是串行) – Igal

回答

0

在你的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是不一样的版本...

请,解决您的changelog,并把下面的

<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"/> 

ŧ他的方式,它应该在两个系统上生成相同的表格。

+0

谢谢!检查它,与此同时我更新版本 Linux版本:9.3.11 Windows版本:9.5 – Igal