• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate sequence exposed as property

 
Richard Everhart
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the application that I currently work on generators (of type sequence) are used to create the IDs for all of our objects, so I somewhat understand how this works.

What I need for a feature I'm adding is a integer value that is incremented during a particular point of execution in the program. I was thinking that a sequence would be perfect for this. Has anyone ever done this before? I can't seem to figure out how to expose (read and set) the sequence value.

Rich
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you can make your mapping to the attribute be a "formula" attribute which can be "SELECT mySequence.nextVal FROM Dual" in Oracle for example.

Mark
 
Richard Everhart
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark.

Now my problem is creating the Hibernate configuration file. I've created the file but my sequence has no id column, which means that I can't provide an ID element withing my class element. Without the id element an XML parsing exception occurs during initialization.

Anyone have any ideas how to get around this. composite-id can also be supplied but I haven't looked into that yet.

Rich
 
Richard Everhart
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the solution. Pretty easy really.



The id is just the column holding the sequence name. This seemed to make things happy. I'm using postgresql by the way.

Rich
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Richard Everhart:
I found the solution. Pretty easy really.



The id is just the column holding the sequence name. This seemed to make things happy. I'm using postgresql by the way.

Rich


Yeah, I was about to ask what DB you were using.

Sounds like you got what you were looking for, congrats.

Mark
 
harsh anajwala
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add doubt which I am having..

can I mention sequence name in property tag also?

<property name="count" column="seq_name" >

here seq_name will generate next value for count so is it possible?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic