1

似乎是一件足够简单的事情。我过去在 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] 持久性.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
4

1 回答 1

0

改变

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

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

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

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

于 2012-12-06T15:56:01.550 回答