Hi,
I dont know if I'm missing something but it looks pretty strange to me that the "default" sequence name is "hibernate_sequence".
AFAIK the "default way" to name sequence in Postgresql is <table name>_<column name>_seq and thus that ought to be the default name ?
But I'm new to postgres as well as to hibernate so I could be mistaken.
To summarize my problem:
My create sql command:
Code:
CREATE TABLE public.templates
(
id serial NOT NULL,
CONSTRAINT templates_pkey PRIMARY KEY (id),
) WITH OIDS;
This gives a default value:
Code:
nextval('public.templates_id_seq'::text);
Which really is <table name>_<column name>_seq (and not as hibernate anticipates hibernate_sequence).
The following generator mapping works like a charm:
Code:
<generator class="sequence">
<param name="sequence">templates_id_seq</param>
</generator>
But none of the following one does:
Code:
<generator class="sequence"/>
Code:
<generator class="native" />
Both generate the faulty SQL query:
Code:
select nextval ('hibernate_sequence')
(which generates an SQL error)
Perhaps there is a way to configure the name ? (though I have yet to find it)
I, to see if it was "imposible to solve", quickly wrote my own generator that works as I belive it "should" by changing the following line:
Code:
this.sequenceName = PropertiesHelper.getString(SEQUENCE, params, "hibernate_sequence");
into:
Code:
this.sequenceName =
PropertiesHelper.getString(
SEQUENCE,
params,
params.getProperty("target_table") + "_" +
params.getProperty("target_column") + "_seq");
Which seem to work "like a charm" ofcource perhaps this way should be made optional and configureable but I only wanted to get things working so it is a crude way to fix the problem. I think that a way to specify the default name using variables would be the preferable solution.
ie something along the following:
<property name="hibernate.sequence_name">${target_table}_${target_column}_seq</property>
in the hibernate.cfg.xml file ?
This would then ofcource fallback to "hibernate_sequence"... But as I dont know my way around hibernate I solved it the "simple way".
Anyways if anyone know how I can make hibernate to behave as I want it or if there is something wroing with my reasoning point me at how I should change things please let me know.
// Michael Medin