-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: sequence name generation on Postgresql ?
PostPosted: Sat Jan 03, 2004 6:04 pm 
Newbie

Joined: Sat Jan 03, 2004 5:31 pm
Posts: 3
Location: Sweden/Stockholm
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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 03, 2004 7:12 pm 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
You are making it very complicated for yourself. Using serial as the column type is not needed.


Try:

Create Table

Code:
create table Document (
   documentId INT8 not null,
...
   primary key (documentId)
);



Mapping

Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="com.company.hibernate.Document"
        table="Document"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="documentId"
            column="documentId"
            type="long"
        >
            <generator class="sequence">
                <param name="sequence">DocumentSeq</param>
            </generator>
        </id>

[snip]

    </class>

</hibernate-mapping>


You will not have to touch the sequence at all.


Cheers,


Sherman


Top
 Profile  
 
 Post subject: and one more step
PostPosted: Sat Jan 03, 2004 7:31 pm 
Regular
Regular

Joined: Wed Dec 31, 2003 4:26 am
Posts: 108
Location: Berkeley, CA
you need to issue a CREATE SEQUENCE for the hibernate_sequence of course.

== Ezra Epstein


Top
 Profile  
 
 Post subject: not hibernate specific.
PostPosted: Sun Jan 04, 2004 4:15 am 
Newbie

Joined: Sat Jan 03, 2004 5:31 pm
Posts: 3
Location: Sweden/Stockholm
Hi,

I know I can create a sequence for hibernates key and in the other sample use another name for the sequence (one that I specify).

But I want to use hibernate to access and existing database that will be use for other purpouses then hibernate not create a hibernate only database thus I would prefer to have a more generic solution one that will function transparently with other access gateways.

Also as I understand it the "default way" to create sequences is using <table name>_<column name>_seq so using another name for it seems "wrong".

And the other idea
Code:
<generator class="sequence">
                <param name="sequence">DocumentSeq</param>
            </generator>

suffers from the same problem I have as it is not "normalized" as it uses information that is already avalible (ie. the sequence is named the same as <table name>Seq so my proposed solutions abpove would benefit this version also).


// Michael Medin


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 8:02 am 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
The "default way" for sequence naming is driven by your use of the "serial" column in your table definition. See http://www.postgresql.org/docs/current/interactive/datatype.html#DATATYPE-SERIAL

Hibernate does not understand that Postgres your "identity" column, based on the dialect definition. It understands using sequences to populate an ID column, though. When it inserts a row in a table for an object with a "sequence" id, it will run a query to get the next sequence value, set the id in the object, and then save the object with another query. See http://www.hibernate.org/hib_docs/reference/html/or-mapping.html#or-mapping-s1-4-sequences

Postgres sequences use bigint arithmetic. See http://www.postgresql.org/docs/current/interactive/sql-createsequence.html.. Any column being populated from a sequence should be an INT8, and a Long in a Hibernate schema mapping.

If you change your id column to an INT8 rather than serial in the CREATE TABLE, and create the sequence separately, then the scheme I gave before will work.


Sherman


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 3:49 pm 
Newbie

Joined: Sat Jan 03, 2004 5:31 pm
Posts: 3
Location: Sweden/Stockholm
Yes, but again the database is not designed for hibernate but rather hibernate is beeing "designed" for my database thus I cannot (or rather willnot as I feel it to be the "wrong way") alter the database. And besids the type does not matter Serial is as you say an "integer" type with a default sequence added for convenience no mater what type I will still call my sequence <table name>_<colum name>_seq as that is what is recomended by postgresql.

I also still feel that a persistance framework should not force the database to use names that are persistance layer specific thus I wanted an abstracted solution. Something you get by allowing the table name to be specified in the configuration. I canot se any benefit to having "hibernate_sequence" as the default (and only generic) sequence naming. As the name is by defenition hibernate specific. Again I could be misunderstanding this.


But I sopose I should stick with my own generator then if there is no way to make hibernate work "my way". As that allows me to have a generic database that is mapped to other persistance frameworks as well as hibernate.

Anyways, thanx for the input...


// Michael Medin


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 6:57 pm 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
One thing you might want to try is creating a subclass of the PostgreSQLDialect that implements the supportsIdentityColumns and related methods. Grab the Hibernate2 source and look at what the DB2Dialect does. Then you could use sequence="identity" in your mapping.

You may also want to put in a feature request to the Hibernate JIRA.


Sherman


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.