Java - Openjpa: how to specify sequence generator starting from hibernate hbm - java

I've to switch persistence of a project using HIBERNATE to OPENJPA and I started from entities and hbm files which define type of columns, etc.
I've an Id on hibernate generated in this way:
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="sequence">
<param name="sequence">seq_illness</param>
</generator>
</id>
how can I "translate" it ointo Jpa annotation to my entity class, in particular how can I represent sequence generator?
I'm new to this feature and I don't understand well usage of
#GeneratedValue(strategy = GenerationType.SEQUENCE)
how can I reproduce sequence parameter and define the correct sequence generator?

In JPA, the mapping for this column would look like:
#Id
#SequenceGenerator(name="ID_GEN" sequenceName="NAME_OF_SEQ_IN_DB")
#GeneratedValue(generator="ID_GEN")
private Integer id;
See the following documentation for further information:
#SequenceGenerator
#GeneratedValue

Related

How do I implement idbag with element in annotations?

I'm working on a refactor where I have to change my classes from hibernate xml to annotations (JPA annotations preferred but hibernate okay). One of my entities uses the hibernate idbag feature combined with the element feature and a join table.
hibernate xml:
<class name="com.my.package.EntityA" table="table_a">
<id name="id" column="table_a_id" type="long" unsaved-value="null">
<generator class="sequence">
<param name="sequence">table_a_seq</param>
</generator>
</id>
<idbag name="entityBIds" table="table_a_b" cascade="all" lazy="false">
<collection-id column="table_a_b_id" type="long">
<generator class="org.hibernate.id.SequenceGenerator">
<param name="sequence">table_a_b_seq</param>
</generator>
</collection-id>
<key column="fk_table_a_id" />
<element column="fk_table_b_id" type="long"/>
</idbag>
</class>
The class looks like this:
public class EntityA {
Long id;
Collection<Long> entityBIds;
}
Schema:
table_a
table_a_id number(13,0)
table_b
table_b_id number(13,0)
table_a_b
table_a_b_id number(13,0)
fk_table_a_id number(13,0)
fk_table_b_id number(13,0)
How would I use annotations to implement this? Note that this is a pretty complex system and I want to minimize the changes that I have to make aside from the annotations.
What you want is a one-to-many relation with a join table.
See https://en.wikibooks.org/wiki/Java_Persistence/OneToMany ยง1.5 Join Table
Your table model is not practical with both tables table_a and table_a_b. But changing it would be costly I guess.
I hope your java model is more flexible...
Define an EntityB with only a Long id. In EntityA have a Collection<EntityB> entityBs and adapt the implementation of your getEntityBIds / setEntityBIds / addEntityBId / etc. to access it and convert as required. Of course hoping the field entityBIds was private and thus not used outside the entity.
I discovered the answer!
#ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
#CollectionTable(name = "TABLE_A_B", joinColumns = #JoinColumn(name="fk_table_b_id"))
#Column(name = "fk_table_a_id")
private Collection<Long> entityBIds;
Of course doing this is less than elegant, but I needed the simplest way to convert to annotations without breaking the complex code surrounding the entities.

Migrating Hibernate 3 to 5: relation hibernate_sequence does not exist

I am migrating an application running with Hibernate 3 to Hibernate 5.
I have a strange error:
ERROR: relation hibernate_sequence does not exist
We are using *.hbm.xml mapping files and everything was working fine until I changed the Hibernate version. I mean we have a pretty straight forward mapping with ID column and DB sequence generator and still Hibernate wasn't able to pick the correct config.
<hibernate-mapping>
<class name="com.boyan.MyClass" table="my_class">
<id name="id" type="long">
<column name="id" />
<generator class="sequence">
<param name="sequence">my_class_seq</param>
</generator>
</id>
...
</class>
</hibernate-mapping>
I started digging in the Hibernate code and saw that SequenceGenerator is deprecated and the new versions use SequenceStyleGenerator. I was very confused when I noticed that in the new version the property telling which is the sequence name is changed from sequence to sequence_name. So finally when I changed:
<param name="sequence">my_class_seq</param>
to:
<param name="sequence_name">my_class_seq</param>
everything worked.
I bumped in to the same problem and I was using annotations. Solution was the accepted answer JPA GenerationType.AUTO not considering column with auto increment. If using annotations following should be used.
#GenericGenerator(name = "my_seq", strategy = "native", parameters = {
#Parameter(name = "sequence_name", value = "mydb_seq")
})

Hibernate unsaved-value annotation

I would like to force Hibernate (4.2) checking object persistence by the field VERSION instead of the field ID. The xml below should work:
<id name="id" column="ID">
<generator class="assigned" />
</id>
<version name="version" column="VERSION" unsaved-value="null" />
But I want to use annotations. Is this java code enough?
#Id
public String getId() {
return this.id;
}
#Version
public Integer getVersion() {
return this.version;
}
How to specify the unsaved-value tag? I don't find any annotation for this.
I've read somewhere, that Hibernate is smart enough to guess that an object is persistent or not, but it was not too convincing.
And what if I want to use -1 instead of null? There must be a way to achieve this, isn't it?
<version name="version" column="VERSION" unsaved-value="negative" />
Thanks for your help.
Edited. Hibernate 3.6 ref guide says:
"The unsaved-value attribute is almost never needed in Hibernate3 and indeed has no corresponding element in annotations."
and
"The assigned generator makes Hibernate use unsaved-value="undefined". This forces Hibernate to go to the database to determine if an instance is transient or detached, unless there is a version or timestamp property, or you define Interceptor.isUnsaved()."
I belive #Version will be sufficient for me.

Hibernate GUID local generator

I use hibernate to generate ids for my MySQL data-tables:
<class name="XXXX" table="XXXX">
<id name="Id" column="Id" type="string">
<generator class="guid"/>
</id>
....
</class>
it works fine.
however, when i profiling the sql queried, there are 2 sqls for 1 insert:
1).select uuid() and then 2).insert ....
I have 3 questions:
why not hibernate generates the "GUID"s locally?
how much is the overhead for "select uuid()" than "UUID.randomUUID()" for one insert?
can i config a "local" generator in hibernate?
AFAIK the GUID generator is deprecated and you should use the new(er) UUIDGenerator instead. See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#d0e5294.
But to answer your questions
That is how the GUID generator functions it calls the database and the result is passed into the id field of the object
No idea, measure, however I guess the impact is negligible as the only thing you do additionally is return a simply value
Yes but why as it is already supported by Hibernate (see the documentation)

How to prevent Hibernate from using 0 as ID?

I am using
#TableGenerator(name="tab",initialValue=2,allocationSize=50)
on Entities and define the ID with
#Id
#GeneratedValue(generator="tab",strategy=GenerationType.TABLE)
private int id;
yet Hibernate still uses 0 as an ID.
I cannot use #GenericGenerator because the annotations do not come with Hibernate4 that ships with Jboss AS7.
Is there a simple solution or do I have to write a custom Generator?
Hibernate is creating ids with id 0 because you have a primitive type. Try using Integer id instead of int id. Remember primitives can't hold a null value.
If you want to generate the custom id generator, you can use a SEQUENCE in DB to generate the id if the object.
<id ....>
<generator class="sequence">
<param name="sequence">YOUR_SEQUENCE _NAME</param>
</generator>
</id>
Read the API about generator classes here.

Categories