How to define allocationSize and initialValue for all entities - java

On all my entities, I defined allocationSize and initialValue manually, just like this :
#Id
#SequenceGenerator(name = "ID_GENERATOR", sequenceName = "SEQUENCE_ID", allocationSize = 1, initialValue = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_GENERATOR")
#Column(name = "ID")
private Long id;
Is it possible to set these attributes globally for all my entities ?
If so, how ?

According to the SequenceGenerator's documentation:
The scope of the generator name is global to the persistence unit
(across all generator types).
So you can define one at in package-info.java as stated in the reference documentation:
#GenericGenerators({
#GenericGenerator(
name = "uuid2",
strategy = "uuid2"
)
})
package your.package;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.GenericGenerators;
Then you need to add you package to the SessionFactory Configuration:
configuration.addPackage("your.package");
And then you can reuse these common generators in all your entities:
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_GENERATOR")
#Column(name = "ID")
private Long id;

Generator and GeneratedValue are two different things. Once Generator is set (via annotation or xml) it is accessible for whole persistence unit. GeneratedValue describes how given id should be generated (in your case it points to one of the generators).
You cannot set allocationSize nor initialValue for all Generators in advance, but you can set it for one generator and use it for many entities.
Please also note that Hibernate has default value of 50 for allocationSize.

Related

How to achieve Batch Inserts with Spring data and SQLite?

I am trying to perform Batch Insert with spring data jpa and Hibernate but the problem is Hibernate only supports batch inserts when the generated value strategy is SEQUENCE but somehow sqlite does not support this strategy so I resort to Identity which works fine but does not support batching with hibernate. Is there any solution or workaround for this.
Entity
#Entity
#Table(name = "party")
public class PartyEntity implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "party_id", nullable = false)
private Long id;
#Column(name = "party_name", unique = true)
#NonNull
private String name;
#Column(name = "party_phone_number", unique = true)
private Long number;
}
SQLite do have this table called 'sqlite_sequence' which I try to use with #SequenceGenerator but hibernate tries to create this table which we cannot as this is a reserved table. And same goes for #TableGenerator.
Annotation for sequence used
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "party_seq")
#SequenceGenerator(name = "party_seq", sequenceName = "sqlite_sequence", allocationSize = 1)
Annotation for table used
#GeneratedValue(strategy = GenerationType.TABLE, generator = "sqlite_generator")
#TableGenerator(name = "sqlite_generator", table = "sqlite_sequence", pkColumnName = "name",
valueColumnName = "seq", pkColumnValue = "party", initialValue = 1, allocationSize = 1)
DDL script for the table
CREATE TABLE party (
party_id INTEGER PRIMARY KEY ASC AUTOINCREMENT,
party_name VARCHAR (256) UNIQUE
NOT NULL,
party_phone_number INTEGER (10) UNIQUE
);

problem to get auto-incremented primary key with hibernate / postgres

Using hibernate 5.4, postgres 10 with IntelIiJ Ultimate, impossible to get auto-incremented primary key
with this following code, the sequence "warehouses_id_seq" is created but the primary key is not incremented
#Entity
#Table(name = "warehouses")
#SequenceGenerator(name = "SEQUENCE_WAREHOUSE", sequenceName = "warehouses_id_seq", allocationSize = 1)
public class Warehouse implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_WAREHOUSE")
#Column(name = "id")
private Integer id;
I added below line to fix the problem:
#ColumnDefault("nextval('public.warehouses_id_seq')");
But I'm quite desapointed, I expected Hibernate to do it automaticaly with the SequenceGenerator...

Error in the #GeneratedValue JPA Tag

I am Creating a class in a project using JPA,and i want to map the class into a database using JPA TAGs, but i am getting an error, in this part:
#Entity
#Table(name= "SIGAC_TIPUS_VALORACIO")
public class SigacTipusValoracio implements Serializable, DibaEntity, DibaSelectItem {
/**
* The Constant serialVersionUID.
*/
private static final long serialVersionUID = 6560479364918284265L;
/**
* The tva id tipus valoracio.
*/
#Id
#SequenceGenerator(name= "SIGAC_TIPUS_VALORACIO_TVAIDTIPUSVALORACIO_GENERATOR", sequenceName = "SIGAC_SEQ_GEN", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SIGAC_TIPUS_VALORACIO_TVAIDTIPUS_GENERATOR")
#Column(name = "TVA_ID_TIPUS", precision= 12)
private Long tvaIdTipusValoracio;
exactly in generator attribute of the #GeneratedValue tag.
does anyone know why ?
#SequenceGenerator(name= "SIGAC_TIPUS_VALORACIO_TVAIDTIPUSVALORACIO_GENERATOR", sequenceName = "SIGAC_SEQ_GEN", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SIGAC_TIPUS_VALORACIO_TVAIDTIPUS_GENERATOR")
The Name attribute of SequenceGenerator is
"SIGAC_TIPUS_VALORACIO_TVAIDTIPUSVALORACIO_GENERATOR".
The generator attribute of GeneratedValue is
"SIGAC_TIPUS_VALORACIO_TVAIDTIPUS_GENERATOR".
It isn't a same ids. I recommend not use such big Ids.

JPA #MappedSuperclass different Id counters for subclasses

I've got a #MappedSuperclass which is my base-class for all my entities (#Entity, direct or indirect through multiple sub-classing).
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#XmlAttribute(required = true)
private Long primaryKey;
The Id is generated like shown above.
My problem is that the #Id-counter is the same for each #Entity. In fact thats not a big problem because it would take a while to reach the Long.MAX_VALUE. But reaching that maximum value is a lot easier because there is only one counter for all entities. How can I use a different #Id-counter without having to add the above code to all #Entity-classes?
(If it matters for your answer: I'm using a H2-Database.)
If your database and tables support AUTO_INCREMENT change the annotation to this #Id #GeneratedValue(strategy=GenerationType.IDENTITY). Then the id will be generated during commit.
There is another way via TABLE or SEQUENCE strategy, but it needs to be explicitly defined per Entity which is problem for abstract BaseEntity. Just hava a look:
#Entity
#TableGenerator(name="tab", initialValue=0, allocationSize=50)
public class EntityWithTableId {
#GeneratedValue(strategy=GenerationType.TABLE, generator="tab")
#Id long id;
}
EDIT:
Well, so it is possible!
MappedSuperclass - Change SequenceGenerator in Subclass
#Id
#Column(name = "ID", unique = true, nullable = false)
#GeneratedValue(strategy = GenerationType.TABLE, generator = "SEQ_AAAAAAAAAAA")
#TableGenerator(
name = "SEQ_AAAAAAAAAAA",
table = "SEQ_ENTITY" /*<<<==YOUR TABLE NAME FOR SAVE NEXT VALUES HERE*/,
pkColumnName = "ENTITY",
initialValue = 1,
valueColumnName = "NEXT_ID",
pkColumnValue = "packageee.PACK.PAK.YOURCLASSNAME",
allocationSize = 1)
private Long id;

Set id from sequence vs manualy insert using JPA Hibernate

I have entity where id generated using sequence
#Id
#SequenceGenerator(name = "ENTITY_SEQ", sequenceName = "ENTITY_SEQ", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ENTITY_SEQ")
#Column(name = "ID", unique = true, nullable = false)
public Long getId() {
return id;
}
And it work OK. But when i cretate test and set id manualy, sequence rewrite value and set it's own value. Is any posibility to change priority of setting value to ID?

Categories