facebook

Postgresql sequences and hibernate mapping generation.

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #252259 Reply

    Hi folks,

    Consider the following SQL used to generate a table:

    
    create table volunteer_sheet (
            volunteer_sheet_id      integer not null
                                    default nextval('volunteer_sheet_id_seq')
                                    primary key,
            trial_id                integer not null
                                    references trial(trial_id)
                                    on delete cascade
                                    on update cascade,
            person_id               integer not null
                                    references person(person_id)
                                    on delete cascade
                                    on update cascade
    );
    
    
    
    

    Of particular interest is the primary key column. You will note that it uses a sequence to generate its next value. When I use the Database Explorer to generate my *.hbm.xml file I get the following:

    
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 
        Mapping file autogenerated by MyEclipse - Hibernate Tools
    -->
    <hibernate-mapping>
        <class name="org.agilitystewards.hbm.tables.VolunteerSheet" table="volunteer_sheet" schema="public">
            <id name="volunteerSheetId" type="integer">
                <column name="volunteer_sheet_id" />
                <generator class="native" />
            </id>
            <many-to-one name="trial" class="org.agilitystewards.hbm.tables.Trial" fetch="select">
                <column name="trial_id" not-null="true" />
            </many-to-one>
            <many-to-one name="person" class="org.agilitystewards.hbm.tables.Person" fetch="select">
                <column name="person_id" not-null="true" />
            </many-to-one>
            <set name="volunteerDaies" inverse="true">
                <key>
                    <column name="volunteer_sheet_id" not-null="true" />
                </key>
                <one-to-many class="org.agilitystewards.hbm.tables.VolunteerDay" />
            </set>
            <set name="volunteerJobs" inverse="true">
                <key>
                    <column name="volunteer_sheet_id" not-null="true" />
                </key>
                <one-to-many class="org.agilitystewards.hbm.tables.VolunteerJob" />
            </set>
        </class>
    </hibernate-mapping>
    
    
    

    which is just ducky except for one annoyance. If I run my code with that configuration I get an error where Hibernate is compliaing about a lack of a sequence called “hibernate_sequence” or some such. (I have to confess… I’ve not run into that problem in a while and you’ll see why in a second.)

    Being the clever chap that I can be I did some research and somewhere on the “net” I foiund a fix for the problem in that I can change the configuration file for the following:

    
           <id name="volunteerSheetId" type="integer">
                <column name="volunteer_sheet_id" />
                <generator class="native" >
                   <param name="sequence">volunteer_sheet_id_seq</param>
               </generator>
            </id>
    

    This is a great workaround, but heaven help me if I make changes to my schema (and I do that a lot on this project) and regenerate my mapping files and POJOs.

    Is there some magic foo that I can do to automagically put the sequence names into the mapping files?

    #252262 Reply

    Haris Peco
    Member

    Peter,

    MyEclipse will support adding generator’s parameter in future releases
    However, you can use ‘identity’ generator and it work without parameter

    create table command (generated with new table wizard in MyEclipse 5.0M1)

    
    
        create table "dbtest"."test_identity"(
            "id"  serial not null,
           "name" varchar(30),
            constraint "test_identity_pkey" primary key ("id")
        )
    

    you can generate this with ‘new table wizard’ in MyEclipse 5.0M1 (choose type ‘integer’ and check auto increment for primary key)

    mapping (generated with MyEclipse and choosing ‘identity’ for generator type)

    
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 
        Mapping file autogenerated by MyEclipse - Hibernate Tools
    -->
    <hibernate-mapping>
        <class name="com.genuitec.test.TestIdentity" table="test_identity" schema="dbtest">
            <id name="id" type="integer">
                <column name="id" />
                <generator class="identity" />
            </id>
            <property name="name" type="string">
                <column name="name" length="30" />
            </property>
        </class>
    </hibernate-mapping>
    

    test code :

    
    public void testIdentity() {
            Session session = HibernateSessionFactory.currentSession();
            Transaction t = session.beginTransaction();
            TestIdentity test = new TestIdentity();
            test.setName("name");
            session.save(test);
            TestIdentity test1 = new TestIdentity();
            test1.setName("name1");
            session.save(test1);
            t.commit();
            session.close();
    }
    

    PostgreSQL (and hibernate) will add and use sequence automatic.

    Best regards

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Postgresql sequences and hibernate mapping generation.

You must be logged in to post in the forum log in