15.12.2020

Hibernate Sequence Generator Non Primary Key

Hibernate Sequence Generator Non Primary Key Rating: 8,3/10 9367 reviews

Is it possible to use a DB sequence for some column that is not the identifier/is not part of a composite identifier?

As you’ve seen, JPA offers 4 different ways to generate primary key values: AUTO: Hibernate selects the generation strategy based on the used dialect, IDENTITY: Hibernate relies on an auto-incremented database column to generate the primary key, SEQUENCE: Hibernate requests the primary key value from a database sequence. Dec 06, 2017  When you’re working with legacy database, you probably had the situation that you had to map a many-to-one association that used a non-primary key.

I’m using hibernate as jpa provider, and I have a table that has some columns that are generated values (using a sequence), although they are not part of the identifier.

I'm using hibernate as jpa provider, and I have a table that has some columns that are generated values (using a sequence), although they are not part of the identifier. What I want is to use a sequence to create a new value for an entity, where the column for the sequence is NOT (part of) the primary key. Jan 27, 2012  The generator in hibernate are basically used to generate unique identifiers and generally used for populating the primary key of the objects. As mentioned in the hibernate docs sequence uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int.

What I want is to use a sequence to create a new value for an entity, where the column for the sequence is NOT (part of) the primary key:

Then when I do this:

the id will be generated, but the mySequenceVal property will be also generated by my JPA provider.

Just to make things clear: I want Hibernate to generate the value for the mySequencedValue property. I know Hibernate can handle database-generated values, but I don’t want to use a trigger or any other thing other than Hibernate itself to generate the value for my property. If Hibernate can generate values for primary keys, why can’t it generate for a simple property?

Answers:

Looking for answers to this problem, I stumbled upon this link

It seems that Hibernate/JPA isn’t able to automatically create a value for your non-id-properties. The @GeneratedValue annotation is only used in conjunction with @Id to create auto-numbers.

The @GeneratedValue annotation just tells Hibernate that the database is generating this value itself.

The solution (or work-around) suggested in that forum is to create a separate entity with a generated Id, something like this:

Answers:

I found that @Column(columnDefinition='serial') works perfect but only for PostgreSQL. For me this was perfect solution, because second entity is “ugly” option.

Answers:

Hibernate definitely supports this. From the docs:

“Generated properties are properties which have their values generated by the database. Typically, Hibernate applications needed to refresh objects which contain any properties for which the database was generating values. Marking properties as generated, however, lets the application delegate this responsibility to Hibernate. Essentially, whenever Hibernate issues an SQL INSERT or UPDATE for an entity which has defined generated properties, it immediately issues a select afterwards to retrieve the generated values.”

For properties generated on insert only, your property mapping (.hbm.xml) would look like:

For properties generated on insert and update your property mapping (.hbm.xml) would look like:

Unfortunately, I don’t know JPA, so I don’t know if this feature is exposed via JPA (I suspect possibly not)

Alternatively, you should be able to exclude the property from inserts and updates, and then “manually” call session.refresh( obj ); after you have inserted/updated it to load the generated value from the database.

This is how you would exclude the property from being used in insert and update statements:

Again, I don’t know if JPA exposes these Hibernate features, but Hibernate does support them.

Hibernate Composite Primary Key

Answers:

I know this is a very old question, but it’s showed firstly upon the results and jpa has changed a lot since the question.

The right way to do it now is with the @Generated annotation. You can define the sequence, set the default in the column to that sequence and then map the column as:

Answers:
Questions:

Although this is an old thread I want to share my solution and hopefully get some feedback on this. Be warned that I only tested this solution with my local database in some JUnit testcase. So this is not a productive feature so far.

I solved that issue for my by introducing a custom annotation called Sequence with no property. It’s just a marker for fields that should be assigned a value from an incremented sequence.

Hibernate Sequence Generator Non Primary Key Mean

Using this annotation i marked my entities. Windows 2012 product key generator.

To keep things database independent I introduced an entity called SequenceNumber which holds the sequence current value and the increment size. I chose the className as unique key so each entity class wil get its own sequence.

The last step and the most difficult is a PreInsertListener that handles the sequence number assignment. Generate public key mac terminal. Note that I used spring as bean container.

As you can see from the above code the listener used one SequenceNumber instance per entity class and reserves a couple of sequence numbers defined by the incrementValue of the SequenceNumber entity. If it runs out of sequence numbers it loads the SequenceNumber entity for the target class and reserves incrementValue values for the next calls. This way I do not need to query the database each time a sequence value is needed.
Note the StatelessSession that is being opened for reserving the next set of sequence numbers. You cannot use the same session the target entity is currently persisted since this would lead to a ConcurrentModificationException in the EntityPersister.

Hope this helps someone.

Answers:

I run in the same situation like you and I also didn’t find any serious answers if it is basically possible to generate non-id propertys with JPA or not.

My solution is to call the sequence with a native JPA query to set the property by hand before persisiting it.

This is not satisfying but it works as a workaround for the moment.

Mario

Answers:

I fixed the generation of UUID (or sequences) with Hibernate using @PrePersist annotation:

Answers:

I’ve found this specific note in session 9.1.9 GeneratedValue Annotation from JPA specification:
“[43] Portable applications should not use the GeneratedValue annotation on other persistent fields or properties.”
So, I presume that it is not possible to auto generate value for non primary key values at least using simply JPA.

Answers:

I’ve been in a situation like you (JPA/Hibernate sequence for non @Id field) and I ended up creating a trigger in my db schema that add a unique sequence number on insert. I just never got it to work with JPA/Hibernate

Answers:

“I don’t want to use a trigger or any other thing other than Hibernate itself to generate the value for my property”

In that case, how about creating an implementation of UserType which generates the required value, and configuring the metadata to use that UserType for persistence of the mySequenceVal property?

Answers:

This is not the same as using a sequence. When using a sequence, you are not inserting or updating anything. You are simply retrieving the next sequence value. It looks like hibernate does not support it.

Tags: hibernate, jpa