Results 1 to 5 of 5

Thread: Generated value from sequence for non primary key property

  1. #1
    Join Date
    Feb 2007
    Posts
    3

    Default Generated value from sequence for non primary key property

    Hi there,

    In my model, I have a unique reference ID which is used for business purposes and I also have a primary key for the record used by the internal system. Both use a sequence to generate the unique value.

    In hibernate, seems that you can't attach a generator to a property - but I probably wouldn't want to have the DB auto generate the value anyway since my service should be the one to understand how to retrieve the reference ID.

    So, I'm just wondering, is the following code the best way to explicitly get the next value from a sequence?

    Code:
        Number nextval = (Number)getHibernateTemplate().execute(
        new HibernateCallback()
        {
            public Object doInHibernate(Session session)
            throws HibernateException, SQLException
            {
                return session.createSQLQuery(
                    "select REFERENCE_ID_SEQUENCE.nextval from dual")
                    .uniqueResult();
            }
        });
    
        result = nextval.longValue();
    Thanks,
    J

  2. #2
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Just form quriosity - what is the reason to have 2 autogenerated values - or one of them is modifiable (so autogenerated value is suggestion only)?

    And concerning your question - I likely would fallback to JDBC for such simple query.
    Quote Originally Posted by joey View Post
    Hi there,

    In my model, I have a unique reference ID which is used for business purposes and I also have a primary key for the record used by the internal system. Both use a sequence to generate the unique value.

    In hibernate, seems that you can't attach a generator to a property - but I probably wouldn't want to have the DB auto generate the value anyway since my service should be the one to understand how to retrieve the reference ID.

    So, I'm just wondering, is the following code the best way to explicitly get the next value from a sequence?

    Code:
        Number nextval = (Number)getHibernateTemplate().execute(
        new HibernateCallback()
        {
            public Object doInHibernate(Session session)
            throws HibernateException, SQLException
            {
                return session.createSQLQuery(
                    "select REFERENCE_ID_SEQUENCE.nextval from dual")
                    .uniqueResult();
            }
        });
    
        result = nextval.longValue();
    Thanks,
    J

  3. #3
    Join Date
    Feb 2007
    Posts
    3

    Default

    Thanks for the response. I was just curious to know if spring had anything built in to call hibernate sequence generators - I'll go with what I've got

    Regarding your question, the auto generated primary key is used by the internal service to identify a given record in the DB. The other generated value is a reference ID which is given to the client. When you model it out the client's reference ID is part of the logical data model but the primary key ID is not so it seems wrong to pass the client an internal ID.

  4. #4
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Hibernate generators are just part of Hibernate API - public classes in the org.hibernate.id package. If you will you may instatiate and use any of them to generate your keys. No special suppost from Spring is needed.

    And for keys - if both keys are autogenerated and not modifiable then I see absolutely no value in separating them.

    Quote Originally Posted by joey View Post
    Thanks for the response. I was just curious to know if spring had anything built in to call hibernate sequence generators - I'll go with what I've got

    Regarding your question, the auto generated primary key is used by the internal service to identify a given record in the DB. The other generated value is a reference ID which is given to the client. When you model it out the client's reference ID is part of the logical data model but the primary key ID is not so it seems wrong to pass the client an internal ID.

  5. #5
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Is it absolutely no way to wire with generator annotations on a simple property (non-primary key) with Hibernate? What of Hibernate 3.5.1 and JPA2, does it have such capabilities?

    We have the same problem with our code, but we want Hibernate to fetch this value and set it automatically as it also does for the primary key.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •