简体   繁体   English

简单的JPA到Postgresql 9.1 HibernateEx

[英]Simple JPA to Postgresql 9.1 HibernateEx

Seems like a simple enough thing. 似乎很简单。 I've used Hibernate sequences to MySQL and Oracle in the past but now using JPA2/Hibernate4 to Postgresql 9.1 and keep getting exceptions. 我过去在MySQL和Oracle上使用过Hibernate序列,但现在在Postgresql 9.1上使用JPA2 / Hibernate4,并不断获取异常。 Any ideas? 有任何想法吗? I've tried different flavors of GenerationType.SEQUENCE and GenerationType.AUTO but nothing works. 我尝试了不同类型的GenerationType.SEQUENCE和GenerationType.AUTO,但是没有任何效果。 I definitely need help. 我绝对需要帮助。

[1] Postgresql 9.1 DDL [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 [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] Entity code [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 [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] Exception [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

Change 更改

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

to

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

else it won't use a sequence strategy, but an identity strategy. 否则它将不使用顺序策略,而是使用身份策略。

And try to remove the default value of the PK column, and to define the column as bigint, since int is only 4 bytes and Java longs have 8 bytes. 并尝试删除PK列的默认值,并将该列定义为bigint,因为int只有4个字节,而Java long有8个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM