H2 in-memory database and custom #GenericGenerator strategy - java

I've tried to use custom id-generator as in Bypass GeneratedValue in Hibernate (merge data not in db?) and it works fine while working with Postgres DB. My code is equals to the code in example.
But while running test with H2 in-memory database I faced the problem, that id is not generated automaticaly.
Without custom generator
#Column(name = "id", nullable = false)
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Generated create table script
create table db.schema.entity (id bigserial not null...
With custom generator
#Column(name = "id", nullable = false)
#Id
#GeneratedValue(generator = "idGenerator", strategy = GenerationType.IDENTITY)
#GenericGenerator(name="idGenerator", strategy = "...UseIdOrGenerate")
private Long id;
Generated create table script
create table db.schema.entity (id int8 not null...
As a result tests don't work.

Solved by changing #Column
#Column(name = "id", nullable = false, columnDefinition = "bigserial")

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
);

How to use UUID in MongoDB + Java (JPA)

I know how to generate UUID in a relational database.
#Id
#GeneratedValue(generator = "UUID")
#GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
#Column(name = "id", updatable = false, nullable = false)
private UUID fileId;
However, GeneratedValue and GenericGenerator cannot be imported.
How can I do it in Mongo?
Mongo has ObjectId, It is a powerful data type that is incredibly useful as a unique identifier in MongoDB Documents.
For more information please check https://www.mongodb.com/developer/products/mongodb/bson-data-types-objectid/

Hibernate association tables

I have 2 entities that have Id's annotated but those Id's aren the primary keys in the tables. I am still mapping the PK's in to the entity for now to limit the initial impact of the change. But the association table that uses the PK's to associate the many to many relationship is throwing the following error:
java.lang.IllegalArgumentException: Provided id of the wrong type for class. Expected: class java.util.UUID, got class java.lang.Long
The entity #Id is the UUID but the table PK which is a Long is mapped as the #JoinColumn
The composite key for the association entity
#Embeddable
public class CustomerAccountId implements Serializable {
private static final long serialVersionUID = 1L;
#Column(name = "user_id", nullable = false)
private Long customerId;
#Column(name = "account_id", nullable = false)
private Long accountId;
The association entity:
#EmbeddedId
#AttributeOverrides({
#AttributeOverride(name = "customerId", column = #Column(name = "user_id", nullable = false)),
#AttributeOverride(name = "accountId", column = #Column(name = "account_id", nullable = false))
})
private CustomerAccountId id;
#ManyToOne
#JoinColumn(name = "user_id", insertable = false, updatable = false, referencedColumnName = "user_id")
private Customer customer;
#ManyToOne
#JoinColumn(name = "account_id", insertable = false, updatable = false, referencedColumnName = "id")
private Account account;
The failing entity:
#Column(name = "user_id")
private Long serialId; // also the primary key in the table
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#org.hibernate.annotations.Type(type="pg-uuid")
#Column(name = "uid", updatable = false)
private UUID id;
Does anyone know if this is even possible? or am I going to be forced to update the content in the association table when I push this change?
put #Id and #GeneratedValue on the field that represent the table data then hibernate will map a long (sgbd) whit a long (table)
or
your (sgbd) table data type must be compatible with the (java) uuid type.
Why these 2 keys on your table?
I think it's not possible to have 2 PK for one entity. At most you can have a composite key base on your serialID and the UUID.
see How to map a composite key with Hibernate? for that
Or mark as #Id the real PK in the SGBD. Use the other in Java as a classic value in the table's point of view
The solution I decided to go with is the following:
#ManyToOne
#JoinColumns ( value = {
#JoinColumn(name = "user_id", insertable = false, updatable = false, referencedColumnName = "user_id"),
#JoinColumn(name = "user_uid", insertable = false, updatable = false, referencedColumnName = "uid")
})
private Customer customer;
In a nutshell I simply added the UUID to the association table and used both columns for the Customer Entity.
To address #Tokazio's question about using UUID and serial Id, the data warehouse conversion is impacted significantly so I need to slowly move from serial Id's to UUID's to minimize impacts and prevent outages.

#SequenceGenerator is not working

Today I got very strange behavior. I have declared a model with a primary key that uses #SequenceGenerator:
#SequenceGenerator(name="EMP_SEQ_GEN", sequenceName="EMP_SEQ")
#Id
#GeneratedValue(generator="EMP_SEQ_GEN_GEN")
#Column(name = "EMP_ID", unique = true, nullable = false, precision = 22, scale = 0)
public Long getEmpId() {
return this.empId;
}
It works locally but it doesn't work on the server. I have connected to the same database from both environments.
I think your #GeneratedValue should look like #GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EMP_SEQ_GEN").

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