Error during primary key generation with SPRING JPA - spring

Hi all i have a weird problem with the generation of PK calling the jpa method save.
Here and exmpale of the snippet i have implemented:
Entity
#Table(name = "MYTABLE")
#Entity
public class EEMYTABLE{
#Id
#Column(name = "IDMYTABLE")
#GenericGenerator(name="generator" , strategy="increment")
#GeneratedValue(generator="generator")
private Long id;
...
Repository
public interface MyTableRepository JpaRepository<MYTABLE,Long> {}
Service
#Autowired
private MyTableRepository myTableRepository;
public void saveNewRecord() {
EEMYTABLE newRecord = new EEMYTABLE();
myTableRepository.save(newRecord);
Problem Steps:
i run save method from my local server. It's all ok.
i run the ssave method from my remote server. It fires the following excepion: " ORA-00001: unique constraint violated "
now if i run again the method from the server is all ok.
if i follow with another call from my local env i have the exception: " ORA-00001: unique constraint violated "
It seems that if i run the method in more than 1 env there is a desync in the internal spring generator value.
Someone with ideas to fix it?
I try some solutions read on internet but i ave not solved the problem.

The "increment" hibernate strategy is not suitable if you have several environments using the same database since it relies on incrementation of an integer inside the JVM.
This explains why your environments generate the same ids.
In your case, you have to rely on a database mechanism, e.g. sequences.
See Hibernate generator class increment vs sequence? for more details.

Related

SpringBoot2 data JPA throwing ConstraintViolationException

I am using Spring Boot 2 for creating microservices. I have a scenario to save an entity. In entity for Id column I have added like below
#Id
#GeneratedValue(Strategy=GenerationType.Auto, generator="increment")
#GenericGenerator(name="increment", strategy="increment")
#Column(name="Id", insertable=false)
private Integer id;
Above works sometimes and it throws Primary Key Constraint Violation Exception sometimes. It is not consistent though. I am running it as two instances with different ports.
Error I get is unique constraint violated:
ConstraintViolationException: could not execute statement; constraint [primary_key_cons]; nested exception is ConstraintViolationException.
Only option I have is to change the strategy to sequence.
Did you insert some data manually? Maybe hibernate is generating id values which are already existing in db. If you can, just clear that table and test it again
Don't use generic generator with strategy increment. It is not recommended to use in a cluster.
increment
generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster.
for more info - here
Use GenerationType.SEQUENCE and crate a SequenceGenerator.
Example,
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "YourSequenceGenerator")
#SequenceGenerator(sequenceName = "YourSequence", name = "YourSequenceGenerator", allocationSize = 1)
private Integer id;

Spring MVC Entities Id (Generated value) separate counter

developping a new Java Spring MVC microservice i have encountered a minor issue.
When i send a creation request for any entity, the id generated always follows the previous one.
My event entity id configuration
My user entity id configuration
For example, this is what i got from these 2 requests
User creation request (you can see the id value is 1)
Event creation request (you can see the id value is 2)
The created event Id is the last created user Id + 1 which i obviously do not want to happen.
I want separate Id values for each entity. I want to know what i am doing wrong.
Thank you
Your solution worker pretty well ;)
I finally used it and added #SequenceGenerator annotation to initialize the count at 0.
#SequenceGenerator(name = "seq", initialValue = 0)
public class ClassName {
#Id
#GeneratedValue(generator = "seq")
private Integer id;
}
Thank you very much Daniel, that's kind of you.
You are using #GeneratedValue without providing a strategy.
Therefore it uses the AUTO strategy by default which indicates that the persistence provider should pick an appropriate strategy for the particular database.
As both ID columns share the same name I assume that both entities share one and the same generator.
Which results in
Create event entity with ID = 1 as the generator started at one
Create user entity with ID = 1 + 1 as new generated value is requested
You should think about using different sequences for generating separate IDs for each entity.
Following uses a DB sequence to generate an ID
#Id
#GeneratedValue(generator = "my_entity_name_seq")
private long id;
If I would not specify a concrete generator in the annotation hibernate ,for example will, create a default sequence called hibernate_sequence which is then used for all entities which use #GeneratedValue without specifying a generator. This then leads to incremented values over all tables / entities.

How to get DB sequence value when using micronaut-data with JDBC

I'm using micronaut-data with JDBC in on my application(no hibernate)
I need to generate my primary key value using Oracle DB sequences
As per their official doc(https://micronaut-projects.github.io/micronaut-data/1.0.x/guide/#jdbc) Section:9.1.4.1 SQL Annotations
Only some of JPA annotations are supported and I didn't find #GeneratedValue and #SequenceGenerator in the list(So not sure whether these are supported or not)
Moreover the doc says,
Section 9.1.4.2 ID Generation
If you wish to use sequences for the ID you should invoke the SQL that generates the sequence value and assign it prior to calling save().
So, what would be the best way to query Oracle database to get sequence value ?(As I don't have any session/entity manager here unlike JPA).
Already tried generating sequence using JPA annotations:
#GeneratedValue and #SequenceGenerator
Also using,
#GenerateValue present in micronaut-data library
(io.micronaut.data.annotation.GeneratedValue)
Sample Code:
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_SEQ")
#SequenceGenerator(sequenceName = "EMPLOYEE_ID_SEQ", allocationSize = 1, name = "ID_SEQ")
private Long employeeId;
Above code failed with:
Caused by: java.lang.NullPointerException: null
at oracle.jdbc.driver.OraclePreparedStatement.setupDbaBindBuffers(OraclePreparedStatement.java:3194)
Sequence generators are not supported yet but are on the TODO list

Avoid duplicate primary keys after database intialization in spring-boot 2

The default #GeneratedValue strategy used to work in a spring boot 1.5 web app, without duplicate id conflicts of any type
... using a simple entity such as this one
// in my/package/Car.java
// ...
#Entity
public class Car {
private long id;
private String company;
private String model;
#Id
#GeneratedValue
public long getId() {
return id;
}
// ... more getters and setters
}
... and initializing the DB at start-up with
# in src/main/resources/import.sql
insert into car values (1, 'Tesla', 'Roadster');
... and later inserting another car with
Car c = new Car();
c.setCompany("Ford");
c.setModel("Pinto");
entityManager.persist(c);
entityManager.flush();
// expect no issue inserting, and a valid ID
log.info("Assigned ID is " + c.getId());
... used to result in a new Car with id 2. I do not really care about the generated ID, as long as there is no conflict. However, this same code now throws the following exception:
org.hsqldb.HsqlException: integrity constraint violation: unique constraint or index violation; SYS_PK_10095 table: CAR
(the DB is HSQL, and I would much rather not have to replace it)
... because the default sequence generation in hibernate 5.2 now does not take existing inserts into account.
What are my possible work-arounds to still allow the database to be initialized via import.sql? I know I can
use very large ids at initialization time (but this is just kicking the can down the road, and not a real solution: eventually the sequence will catch up and break things)
write my own sequence generator (but there has to be a much easier way of initializing a DB!)
use the old sequence generation (but again, why did they change it if there was no advantage to doing so? hibernate developers surely had some better way of initializing things in mind!).
somehow specify a starting value for new IDs (how do I do this in a fail-safe way? is there a property that can go into my application.properties to keep this centralized?)
I want to use this in the context of a spring-boot web app, and to keep it as simple and close to best practices as possible. Suggestions?
From version 5 SEQUENCE is used instead of IDENTITY for id generation. Migration from Hibernate 4 to 5
What happened?
You inserted record with ID 1 using script. Sequence remains at 1. It wants to insert 1 what is causing unique PK violation.
Solution
Don't use generation type auto. Use IDENTITY. Then inserting records by script, IDENTITY will be automatically increased. Also you don't need to insert ID value:
DECLARE temp_id INTEGER;
INSERT INTO CUSTOMERS VALUES (DEFAULT, firstname, lastname, CURRENT_TIMESTAMP);
SET temp_id = IDENTITY();
INSERT INTO ADDRESSES VALUES (DEFAULT, temp_id, address);

GenerationType.SEQUENCE generates ID's which are already existing

I have a spring boot application which is already running online for several months without any problems until today. I have a Entity with id generation type sequence:
#Entity
#ComponentScan
public class MyEntity {
/**
*
*/
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
}
Since today im getting errors when a new entity is created and stored:
Unique index or primary key violation: "PRIMARY_KEY_D92 ON PUBLIC.MyEntity(ID) VALUES (3713, 250)"; SQL statement:
Everytime when this error occurs the generated id (3713 in this case) is already existing in the database. So why all of a sudden the GenerationType.SEQUENCE is generating ids which are already existing?
EDIT
I use the H2 Database version 1.4.191
I've encountered this problem with Hibernate, but we had explicit #SequenceGenerator annotations. The problem is that the default SequenceGenerator in JPA has an allocationSize of 50, where as the default database sequence increments by 1. Those two values need to be the same. One solution is to define your SequenceGenerator and explicitly set that allocationSize.
#SequenceGenerator(name = "my_entity_gen", sequenceName = "my_entity_id_seq", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_entity_gen")
Generators must be unique, unless you want two tables to share them.
The other solution is to use a different generation strategy.
#GeneratedValue(strategy = GenerationType.IDENTITY)
This works if the database already knows to query the sequence upon inserting, as they usually do, unless you've explicitly created the sequence after the table.
The first approach will trigger two queries to the database for every insert, so it's almost certainly less efficient.

Resources