【问题标题】:Simple JPA to Postgresql 9.1 HibernateEx简单的 JPA 到 Postgresql 9.1 HibernateEx
【发布时间】:2012-11-23 06:04:02
【问题描述】:

看起来很简单。我过去在 MySQL 和 Oracle 中使用过 Hibernate 序列,但现在在 Postgresql 9.1 中使用 JPA2/Hibernate4 并不断收到异常。有任何想法吗?我尝试了不同风格的 GenerationType.SEQUENCE 和 GenerationType.AUTO,但没有任何效果。我绝对需要帮助。

[1] Postgresql 9.1 DDL

    -- removed  CACHE 100
create sequence my_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 START 1;
create table my_table (
  id              integer NOT NULL DEFAULT nextval('my_seq'),
  ident           text NOT NULL CONSTRAINT ident_uniq UNIQUE,
  cname           text NOT NULL,
  created         timestamp WITH TIME ZONE NOT NULL DEFAULT ('now'::text)::timestamp(6) with time zone,
  lastmodified    timestamp WITH TIME ZONE NOT NULL DEFAULT ('now'::text)::timestamp(6) with time zone,

  CONSTRAINT my_table_pkey PRIMARY KEY(id)
);


insert into my_table (ident, cname) values ('test001', 'Test 001');

[2] Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0" >

    <persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <description>Persistence unit</description>

        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/mydb" />
            <property name="javax.persistence.jdbc.user" value="user" />
            <property name="javax.persistence.jdbc.password" value="password" />

            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />
            <property name="prefer_sequence_per_entity" value="true" />

        </properties>
    </persistence-unit>
</persistence>

[3]实体代码

package com.mypackage;

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Transient;

@Entity(name="my_table")
public class MyTable {

    @SequenceGenerator(name="foo", sequenceName="my_seq", allocationSize=1, initialValue=1)
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "foo")
    @Id
    @Column(name = "id")
    private long id;

    private String ident;   
    private Timestamp created;
    private Timestamp lastmodified; 
}

[5] JUnit

public class Test_MyTable {

    @Test
    public void test_1() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPersistenceUnit");
        EntityManager em = emf.createEntityManager();

        List<MyTable> list = em.createQuery("from my_table", MyTable.class).getResultList();
        int size = ( (list == null) || (list.size() <= 0) ) ? 0 : list.size();

        if (size == 0) {
            System.out.println("Didn't find any MY_TABLE records");
        } else {
            System.out.println("Found [" + size + "] MY_TABLE records");
        }

        em.close();
        emf.close();
    }
}

[3] 异常

javax.persistence.PersistenceException: [PersistenceUnit: MyPersistenceUnit] Unable to build EntityManagerFactory
Caused by: org.hibernate.HibernateException: Wrong column type in public.my_table for column id. Found: serial, expected: int8

【问题讨论】:

    标签: java jpa-2.0 postgresql-9.1 hibernate-4.x


    【解决方案1】:

    改变

    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "foo")
    

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "foo")
    

    否则它不会使用序列策略,而是使用身份策略。

    并尝试删除PK列的默认值,并将该列定义为bigint,因为int只有4个字节,而Java longs有8个字节。

    【讨论】:

      猜你喜欢
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多