簡體   English   中英

簡單的JPA到Postgresql 9.1 HibernateEx

[英]Simple JPA to Postgresql 9.1 HibernateEx

似乎很簡單。 我過去在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

更改

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

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

否則它將不使用順序策略,而是使用身份策略。

並嘗試刪除PK列的默認值,並將該列定義為bigint,因為int只有4個字節,而Java long有8個字節。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM