Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java / Hibernate / Reverse Engineering / @GeneratedValue

  • 10-11-2012 06:42PM
    #1
    Registered Users, Registered Users 2 Posts: 24


    Hello

    I'm having a few problems with Hibernate, and I'm hoping someone can help me spread some light on it.

    I've created a database table ( in PostgreSQL ), which has an automatically incrementing ( using a sequence ) primary key. In Netbeans ( v7.2.1 ), I created an annotated class to map to this table, which has methods corresponding to each column. The primary key has the @Id annotation, but doesn't have a @GeneratedValue type one.

    This means that whenever I try to insert a row using this class, that I cannot do so without specifying value for the id column.

    Adding the annotation @GeneratedValue(strategy= GenerationType.IDENTITY) manually allows me to use this, but in the longer term, I'll be making several changes as time goes on to these tables, and I really don't want to be manually adding that annotation every time.

    During the past 3 days searching, I've tried adding sections to the hibernate.reveng.xml file that was generated. This didn't help.
      <table name="table_name">
          <primary-key>
              <generator class="identity" />
              <key-column name="id" />
          </primary-key>
      </table>
    

    Another thing that uncle Google surfaced up, was creating a custom Reverse Engineering strategy, but I can find very little in terms of documentation on how to do that.

    I'd appreciate any pointers anyone would have in the right direction.

    Disclosure: This is part of a college assignment, but not one directly related to hibernate. The project is to build a service, and Hibernate is a tool chosen to assist with data persistence.

    Also: I may be missing something very obvious here to seasoned java developers - I've only been using Java for about a month - I spend the rest of my life so far as a sysadmin with higher level languages ( Perl, Python, etc )


Comments

  • Registered Users, Registered Users 2 Posts: 12,025 ✭✭✭✭Giblet


    Postgres uses sequences which have to be created manually and you should assign "sequence" and pass the name of the sequence.
    <generator class="sequence">
                    <param name="sequence">sequence name</param>
            </generator>
    


  • Registered Users, Registered Users 2 Posts: 24 RBX


    Giblet wrote: »
    <generator class="sequence">
                    <param name="sequence">sequence name</param>
            </generator>
    

    Thanks Giblet - I just tried that, but it's not having the desired effect. I'm still not getting any @GeneratedValue like annotations.

    Incase I'm missing something silly, here's my full hibernate.reveng.xml as it now stands:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
    <hibernate-reverse-engineering>
      <schema-selection match-catalog="ressys"/>
     
    
      
      <table name="legs" schema="ressys" class="Legs">
          <primary-key>
              <generator class="sequence">
                  <param name="sequence">legs_id_seq</param>
              </generator>
              <key-column name="id" />
          </primary-key>
      </table>
     
    </hibernate-reverse-engineering>
    


  • Registered Users, Registered Users 2 Posts: 24 RBX


    Ok - it seems this is somehow related to the use of PostgreSQL; probably in one of the hibernate/jdbc drivers used to generate the code.

    I tried the same exercise in MySQL, and the @GeneratedValue is inserted as expected.

    I now need to figure out if I'm missing something in my PostgreSQL table to let hibernate know that this should have a @GeneratedValue annotation.


  • Registered Users, Registered Users 2 Posts: 24 RBX


    For my use case, I worked around the problem in this case by switching to MySQL. This was feasible in this case since I'm time bound, and it's not a course in databases. I just documented in my writeup that if Hibernate, PostgreSQL, and reverse Engineering worked correctly, then I would have used the constraint features available in PostgreSQL, and how I'd have used them.

    Thanks anyway for the pointer Giblet.

    I guess the only thing remaining in this thread is changing [Question] to [WorkedAround] or [Solved] or something.


Advertisement