开发者

How to create a Java(6) Hibernate(3.6) Entity or other construct to create a unique combination of a string + int

开发者 https://www.devze.com 2023-02-16 01:49 出处:网络
I\'m working on a desktop application in Java6 using H2 as the db and Hibernate 3.6. Because of a construct with a third-party library involving JNI and some interesting decisions made a priori, I am

I'm working on a desktop application in Java6 using H2 as the db and Hibernate 3.6.

Because of a construct with a third-party library involving JNI and some interesting decisions made a priori, I am unable to pass around long identifiers in their index code, and can only pass int. These indexes are generated quickly and repeatedly(not my choice), and get handed around via callbacks. However, I can split my expected dataset along the lines of a string value, and keep my id size at int without blowing out my id's. To this end, I'm keeping a long value as pk on the core object, and then using that as a one-to-one into another table, where it maps the int id back to the core entity, which when combined with the string, is unique.

So I've considered embedded compound keys and such in hibernate, but what I REALLY want is to just have this "extra" id that is unique within the context of the extra string key, but not necessarily universally unique.

So something like(not adding extraneous code/annotations):

@Entity
public class Foo{
  ...
  @Id
  public Long getId(){...}
  ...
  @OneToOne
  @PrimaryKeyJoinColumn
  public ExtraKey getExtra(){...}
}

@Entity
public class ExtraKey{
  ...
  @Id
  public Long getFooId(){...}
  ...
  public Integer getExtraId(){...}
  ...
  pub开发者_JS百科lic String getMagicString(){...}
}

In that case, I could really even remove the magicString, and just have the fooId -> extraId mapping in the table, and then have the extraId + magicString be in another where magicString is unique. However, I want hibernate to allow the creation of new magicString's at whim(app requirement), ideally one per row in a table, and then have hibernate just update the extraId associated to that magicString via incrementation/other strategy.

Perusing all of the hibernate manuals and trying a few tests on my own in a separate environment has not quite yielded what I want(dynamically created named and sequential id's basically), so I was hoping for SO's input. It's entirely possible I'll have to hand-code all of it myself in the db with sequences or splitting a long and doing logic on the upper and lower, but I'd really rather not, as I might have to maintain this code someday(really likely).

Edit/Addendum

As a sneaky way of getting around this, I'm just adding the extraId to the Foo object(ditching the extraKey class), and generating it from another object singleton, that at load time, does a group by select over the backing Foo table, returning magicKey, and the max(extraId). When I create a new Foo, I ask that object(multithread safe) to hand me the next extraId for the given magicKey and push that into Foo, and store it, thus updating my effective extraId for each magicKey on next app reload without an extra table. It costs me one group by query on the first request for a new extraId, which is suboptimal, but it's fast enough for what I need, simple enough to maintain in the future, and all contained in an external class, so I COULD replace it in one place if I ever come up with something more clever. I do dislike having the extra "special query" in my dao for this purpose, but it's easy enough to remove in the future, and well-documented.


Maybe I still didn't understand your problem properly, but I think you can consider using Hibernate's hilo algorithm. It will generate unique identifier for the whole database, based on a table that Hibernate creates and manages. More details here:

http://docs.jboss.org/hibernate/core/3.5/reference/en/html/mapping.html#mapping-declaration-id

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号