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. Any ideas? I've tried different flavors of GenerationType.SEQUENCE and GenerationType.AUTO but nothing works. I definitely need help.
[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] Entity code
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] Exception
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.