i have gwt application connect to postgres DB at the backend, and a java class 'Judgement' mapping the table 'judgements' in DB, when i tried to persistent a judgement into db, it threw the following errors:
Caused by: org.hibernate.exception.SQLGrammarException: could not get next sequence value
...
Caused by: org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist
my Judgement class looks like this
@Entity
@Table(name = "JUDGEMENTS")
public class Judgement implements Serializable, Cloneable {
private static final long serialVersionUID = -7049957706738879274L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "JUD_ID")
private Long _judId;
...
and my table judgements is:
Column | Type | Modifiers
-------------+-----------------------------+---------------------------------------------------------
jud_id | bigint | not null default nextval('judgements_id_seq'::regclass)
rating | character varying(255) |
last_update | timestamp without time zone |
user_id | character varying(255) |
id | integer |
Indexes:
"judgements_pkey" PRIMARY KEY, btree (jud_id)
Foreign-key constraints:
"judgements_id_fkey" FOREIGN KEY (id) REFERENCES recommendations(id)
"judgements_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(user_id)
and i have a SEQUENCE name 'judgements_id_seq' in DB
can anyone tell me what's wrong??? thanks.
This question is related to
hibernate
postgresql
Using the GeneratedValue
and GenericGenerator
with the native
strategy:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_native")
@GenericGenerator(name = "id_native", strategy = "native")
@Column(name = "id", updatable = false, nullable = false)
private Long id;
I had to create a sequence call hibernate_sequence
as Hibernate looks up for such a sequence by default:
create sequence hibernate_sequence start with 1 increment by 50;
grant usage, select on all sequences in schema public to my_user_name;
I would also like to add a few notes about a MySQL-to-PostgreSQL migration:
That's as far as I remember now, hope these tips can spare you a few minutes of trial and error fiddling!
I seem to recall having to use @GeneratedValue(strategy = GenerationType.IDENTITY)
to get Hibernate to use 'serial' columns on PostgreSQL.
Please use the following query and alter your table :
CREATE SEQUENCE user_id_seq START 1;
ALTER TABLE product.users ALTER COLUMN user_id SET DEFAULT nextval('user_id_seq');
ALTER SEQUENCE users.user_id_seq OWNED BY users.user_id;
and use the this in your entity class
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator="user_id_seq")
I got same error before,
type this query in your database CREATE SEQUENCE hibernate_sequence START WITH 1 INCREMENT BY 1 NOCYCLE;
that's work for me, good luck ~
If using Postgres, create sequence manually with name 'hibernate_sequence'. It will work.
The Database that we are using should be mentioned under search_path in Postgres SQL Configuration file. This can be done by editing Postgressql configuration file by setting search_path along with database name for example: TESTDB.
It worked for me after making the above change.
I think you already have enough answer, but I got exactly the same error and my problem was another one. And I wasted a little bit of time trying to solve it.
In my case the problem was the owner of sequence in Postgres. So, if any solution above did not solved your problem, check if the owner of sequence is the user/role which should have permission.
Follows a sample:
CREATE SEQUENCE seq_abcd
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.seq_abcd OWNER TO USER_APP;
I hope it can be useful for anyone.
For anyone using FluentNHibernate (my version is 2.1.2), it's just as repetitive but this works:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("users");
Id(x => x.Id).Column("id").GeneratedBy.SequenceIdentity("users_id_seq");
You need to set your @GeneratedId column with strategy GenerationType.IDENTITY instead of GenerationType.AUTO
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "JUD_ID")
private Long _judId;
Source: Stackoverflow.com