This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Auto Composite key with Id and version

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys:

I am new to Hibernate mapping and wondering if I can have a primary key like below.

Keyid, version

So everytime I enter a new record it creates an entry like (1,1)
everytime I modify on that record it just updates the version (1,2), (1,3)....

How can I achieve it using auto generated keys? Or can I have it using autogenerated keys? Or I need to manually increment the version.
Please advise.

Thanks
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Auto generated mechanism is a way of generating keys, but I don't think that is good for your business logic in here. You can use that for your keys and have your own logic for version maintenance.

But if those versions have no business meaning other than just an incremented values then you can make the table for versioning have auto-incremented field which increments for each record inserted.
 
Karan Jain
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.

The key id and version have no business logic as such other than they shows all the actions on a record.

So I would like to have records with ids

1 1 ----- data --- new records
1 2 ------ data ---- modification
1 3 ------ data ------ another modification

I understand that I can auto-generate the main key but still not getting how to increment the version automatically using JPA/hibernate annotations?

is it possible?

Thanks
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This site is your friend:

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

esp.

2.2.1.2. Versioning for optimistic locking

You can add optimistic locking capability to an entity using the @Version annotation:

@Entity
public class Flight implements Serializable {
...
@Version
@Column(name="OPTLOCK")
public Integer getVersion() { ... }
}
The version property will be mapped to the OPTLOCK column, and the entity manager will use it to detect conflicting updates (preventing lost updates you might otherwise see with the last-commit-wins strategy).

The version column may be a numeric (the recommended solution) or a timestamp. Hibernate supports any kind of type provided that you define and implement the appropriate UserVersionType.

The application must not alter the version number set up by Hibernate in any way. To artificially increase the version number, check in Hibernate Entity Manager's reference documentation LockModeType.OPTIMISTIC_FORCE_INCREMENT or LockModeType.PESSIMISTIC_FORCE_INCREMENT.


I would suggest you study that entire link, (or later if using another version).
It contains a ton of tips and "gotchas" which can frustrate new Hibernate developers.

Pat
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does Hibernate does auto-insert on a version update? I don;t think so. The version number in Hibernate is used for locking updates to the row. What the OP is trying to do is do inserts instead of updates; ie; whenever a record needs to be "updated", the app will insert a new record with an incremented version number. A totally different ball game!
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jayesh i don't think that Karan wants to do inserts instead of updates. And if he want to do that then it shoudln't cause it's a very bad ideea. This will be like an database audit but done in the whorst way possible. So in a normal situation he could use just the hibernate versioning system. If he actually needs an database audit he should use hibernate incerceptors mechanism and a special table in which he can save the changes, or other proper mechanisms.
 
Karan Jain
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sharing your thoughts guys.

I maintain an audit table for these kind of scenarios.
But what I generally do is get a new key for the row and then manually increment the version by querying the last inserted row.
Then just increment the version. Update the main table and insert into audit.

So the question is what is the best way to do that.

But auto increment version what I meant was to get a new row with just a new version and not the new id itself.
So if (1,1) is there... Can I get automatically get (1,2) or I need to query (1,1) and then update to (1,2) and insert (1,2).
reply
    Bookmark Topic Watch Topic
  • New Topic