Hibernate cannot handle sequences with same name in differen schemas - java

After upgrading from 5.4.7 to 5.4.10 it looks like hibernate cannot handle two sequences with the same name in different db schemas anymore.
I have this entity
#Entity
#Table(name = "VM_LAUF_RICHTUNG", schema = "INFOP_FAHRPLAN")
public class VmLaufRichtung {
public static final String VM_LAUF_RICHTUNG_TABLE = "INFOP_FAHRPLAN.VM_LAUF_RICHTUNG";
#Id
#Digits(integer = 15, fraction = 0)
#SequenceGenerator(name = "InfopFahrplan.seqVmLaufRichtung", schema = "INFOP_FAHRPLAN", sequenceName = "SEQ_VM_LAUF_RICHTUNG")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "InfopFahrplan.seqVmLaufRichtung")
#Column(name = ID)
private Long id;
}
In an other schema, there's a sequence with a the same name SEQ_VM_LAUF_RICHTUNG.
When my spring boot application starts I do get
Caused by: org.hibernate.MappingException: The increment size of the [SEQ_VM_LAUF_RICHTUNG] sequence is set to [50] in the entity mapping while the associated database sequence increment size is [1].
This happens because it's picking up the wrong sequence, which has an other imcrement size.
I tried to fix the problem by setting
spring.jpa.hibernate.use-new-id-generator-mappings=true
but that did not change anything.
With hibernate 5.4.7 everyhting is working fine.
Did I miss something or is this a hibernate bug after all?

It looks that this is indeed a hibernate bug: https://hibernate.atlassian.net/browse/HHH-13322

Related

How to insert test data into H2 using sequence for default value, when Hibernate entity has GenerationType.SEQUENCE

When an entity is mapped with GenerationType.IDENTITY an sql insert into a h2 test db will have a default value for the id column pointing to a sequence. When mapped with GenerationType.SEQUENCE it won't have a default value.
We want to use GenerationType.SEQUENCE as it's more verbose and we want to show where everything comes from. During testing we've stumbled upon this problem - when using #Sql to insert a script into an embedded h2 database we get an error about a not null constraint.It also happens when we just execute the script as a native query.
This doesn't happen when using GenerationType.IDENTITY. The same script from above works properly.
If we use GenerationType.SEQUENCE and pass it the id by calling a next value function like this:
INSERT INTO example (id, name) (NEXTVAL('example_id_seq'), 'test');
the script will work without a problem.
Here is an example mapping an insert where the problem occurs:
#Entity
#Table(schema = DbSchemas.PUBLIC, name = DbTables.Example)
public class Example implements Serializable {
private static final long serialVersionUID = -8827852720381659873L;
private static final String EXAMPLE_GENERATOR = "example_generator";
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = EXAMPLE_GENERATOR)
#SequenceGenerator(name = EXAMPLE_GENERATOR, sequenceName = DbSequences.EXAMPLE_ID_SEQUENCE, allocationSize = 1)
private Integer id;
#Column(name = "name", nullable = false)
private String name;
}
INSERT INTO public.example (name)
VALUES ('test');
o.h.engine.jdbc.spi.SqlExceptionHelper : NULL not allowed for column "ID"; SQL statement:
I'd like to be able to call the insert shown above for entities mapped with GenerationType.SEQUENCE. The insert must use and increment the sequence described in the mapping.

spring data jpa unable to create primary key getting this error SQL Error: 2289, SQLState: 42000 ORA-02289: sequence does not exist

I am working with springboot, spring data jpa ,Oracle db. If i changed the schema i am getting this error, without schema change everything is working fine.
I am not using sequence
#Entity
#Table(name = "CLIENTS")
public class CLIENTS implements Serializable {
private static final long serialVersionUID = 123;
#Id
#Column(name = "ID")
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer clientId;
#Column(name = "CODE")
private Integer code;
#Column(name = "REC_ID")
private Integer recId;
//setters, getters, etc
}
"I am not using sequence". Sure you are. GenerationType.AUTO has chosen it for you out of TABLE, SEQUENCE and IDENTITY.
The sequence exists in the old schema, but if you change the schema you need to create the sequence in the new schema too.
Extend to Kayaman
Oracle #GeneratedValue(strategy = GenerationType.AUTO) Spring data jpa (or) Hibernate will create a sequence itself and it is used for all insertions , that sequence name HIBERNATE_SEQUENCE, we have to create into new schema, if not exist
To know the all sequences
SQL>select * from user_sequences;
For Creating sequence
SQL> create sequence HIBERNATE_SEQUENCE
SQL>create sequence HIBERNATE_SEQUENCE
start with 1 increment by 1 maxvalue 9999999999999999999999999999 minvalue 1 cache 20;
FOR delete Sequence
SQL> drop sequence HIBERNATE_SEQUENCE;

hibernate #Id #GeneratedValue with non auto-increment and i dont have sequence

i have situation on which i try to persist entity with id that depends on the max id value, for example the new entity id will be MAX(id)+1.
now i try to use JPA to persist this entity
#Entity
#Table(name = "product")
public class ProductDetails {
#Id
#GeneratedValue
private String id;
i used strategy = GenerationType.AUTO, strategy = GenerationType.IDENTITY,strategy = GenerationType.SEQUENCE,strategy = GenerationType.TABLE none of them work, so i think i can solve it through selecting the max id then +1 and use that value (i did not try it) what i am asking for, is there is any way to handle this situation through JPA or Hibernate.Note:the id columns is not auto-increment and the db doesn't have sequence.
Don't use String as Primary key. if you need id like "ABC123" then take 2 id columns. One as id(int) PK, second as display_id(String). You can auto-generate display_id in database level using trigger.
If you use String as a type of your Id you shouldn't use auto-increment cause String is something that can't be incremented since it's not a number type. Just leave #Idand add #GeneratedValue(generator = "uuid") - that should work.
Additionally you can add #GenericGenerator(name = "uuid", strategy = "uuid2")

JPA 2 #SequenceGenerator #GeneratedValue producing unique constraint violation

Problem Overview
At seemingly random times we get an exception "postgresql duplicate key violates unique constraint." I do think I know what our problem"s" are but I don't want to make changes to the code without having a reproducible test case. But since we haven't been able to reproduce it in any environment other than randomly in production, I'm asking assistance from SO.
In this project we have multiple postgres databases, and a primary key sequence configured for each table in each database. These sequences are created like this:
create sequence PERSONS_SEQ;
create sequence VISITS_SEQ;
etc...
We use these sequences to generate the primary keys for the entities like this:
#Entity
#Table(name = "visits")
public class Visit {
#Id
#Column(name = "id")
#SequenceGenerator(name = "seq", sequenceName = "visits_seq")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
private int id;
...
}
#Entity
#Table(name = "person")
public class Person {
#Id
#Column(name = "id")
#SequenceGenerator(name = "seq", sequenceName = "persons_seq")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
private int id;
...
}
Analysis
I think I recognize 2 problems with this configuration:
1) Both #SequenceGenerators specify the same name attribute even though they are supposed to map to different database sequences.
2) The #SequenceGenerator allocationSize attribute defaults to 50 (we're using hibernate as the JPA provider) so I think the create sequence syntax should specify how much the sequence should increment by, specifically by 50 to match the allocationSize.
Based on this guess, I think the code should be modified to something like this:
create sequence PERSONS_SEQ increment by 50;
create sequence VISITS_SEQ increment by 50;
etc...
#Entity
#Table(name = "visits")
public class Visit {
#Id
#Column(name = "id")
#SequenceGenerator(name = "visits_seq", sequenceName = "visits_seq")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "visits_seq")
private int id;
...
}
#Entity
#Table(name = "person")
public class Person {
#Id
#Column(name = "id")
#SequenceGenerator(name = "persons_seq", sequenceName = "persons_seq")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "persons_seq")
private int id;
...
}
I would just test this rather than asking the question on SO, but again, we have not been able to reproduce this production issue in any other environments. And even in production the unique constraint violation only occurs at seemingly random times.
Questions:
1) Am I correct in my analysis of what the changes should be to fix this unique constraint violation?
2) What are the best practices for using sequence generators when using hibernate as a JPA provider?
Yes, your analysis is correct. You identified correctly the problem (we had a similar problem).
And... if you gonna put that in production, don't forget to:
either generate manually the sequence table for the new sequence generator WITH the correct initial value/initial ID (otherwise hibernate will begin from 1 and you will get again )
or set that value in Code (check initalValue in #SequenceGenerator).
I am not able to enumerate the best practices, but I suppose you could lower the limit of 50. Also I do not have experience with PostgreSQL, but in MySQL you have a simple table for the seq. generator and hibernate makes the entire stuff.
Had a same problem — for some reason hibernate wasn't picked the right number from the sequence. Tried all approaches with no luck and finally came to this solution:
#Entity
#Table(name = "events")
#SequenceGenerator(name = "events_id_seq", sequenceName = "events_id_seq", allocationSize = 1)
public class Event {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "events_id_seq")
private BigInteger id;
I've had to put #SequenceGenerator on top of class, not the method, also allocation size was set to 1 (if you'll left this value as default, it will start to produce negative ids).
spring-data-jpa 2.1.2, hibernate 5.3.7, pg 42.2.5
I hade a similar problem. In my case, I imported data directly via SQL. This led to a problem with the 'hibernate_sequence'. The hibernate_sequence was by id 123 but there were rows in my table where the id was greater than 123.
I gone through the same problem. and I tried this to fix the problem. may be this is not the best solution but i hope it will solve your problem for now.
#SequenceGenerator(schema = "DS_TEST",name = "SEQ_PR_TEXT",sequenceName = "SEQ_PR_TEXT",
allocationSize = 1)
public class TextEntity {
#Id
#GeneratedValue(generator = SequenceConstant.SEQ_PR_TEXT,
strategy = GenerationType.SEQUENCE)
#Column(name = "PR_TEXT_ID")
private Long id;
}

How is my id being generated with JPA using Hibernate with the Oracle 10g dialect?

I have some code:
#Id
#SequenceGenerator(name = "SOMETHING_SEQ")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SOMETHING_SEQ")
#Column(name = "SOMETHING", nullable = false)
private Long id;
How is hibernate providing my id?
I see in my database there a single sequence named 'hibernate_sequence' and no other hibernate 'special tables'.
Actually, here your SOMETHING_SEQ is the name of sequence you configured somewhere in your hibernate config. And hibernate_sequence is the sequence name in the database. In configuration it would be looking something like below,
<sequence-generator name="SOMETHING_SEQ"
sequence-name="hibernate_sequence"
allocation-size="<any_number_value>"/>
You can completely skip this configuration by using annotation instead. Then your #SequenceGenerator annotation would need to provide few more paramters. Below is the example.
#SequenceGenerator(name="SOMETHING_SEQ", sequenceName="hibernate_sequence", allocationSize=10)
For example multiple entity classes would do something like below,
#Entity
public class Entity1 {
#Id
#SequenceGenerator(name = "entity1Seq", sequenceName="ENTITY1_SEQ", allocationSize=1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "entity1Seq")
#Column(name = "ID", nullable = false)
private Long id;
...
...
}
#Entity
public class Entity2 {
#Id
#SequenceGenerator(name = "entity2Seq", sequenceName="ENTITY2_SEQ", allocationSize=10)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "entity2Seq")
#Column(name = "ID", nullable = false)
private Long id;
...
...
}
How is hibernate providing my id?
Well, you explicitly told the JPA engine to generate identifier automatically (with the #GeneratedValue annotation) using a strategy of type SEQUENCE indicating that a database sequence should be used to generate the identifier. In case you wonder, sequences are database specific objects (e.g. Oracle) that can be used to generate unique integers.
I see in my database there a single sequence named 'hibernate_sequence'
You didn't use the sequenceName annotation element in your #SequenceGenerator to specify the name of the database sequence object to use so Hibernate created a default sequence object during schema generation (which defaults to hibernate_sequence). To specify a sequence, do it like this:
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_entity_seq_gen")
#SequenceGenerator(name = "my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ")
private Long id;
In order to name the sequence you have to set the sequenceName in your #SequenceGenerator annotation:
#GeneratedValue(name="gen", strategy = GeneratorType.SEQUENCE)
#SequenceGenerator(name="gen", sequenceName="Sequence_Name", allocationSize = 1)
#Id
public Long getId()
{
// ...
}
Of note, if you are using a pre-existing generator, your allocationSize must match the allocation size of that generator.
In Oracle you don't have the auto_increment type as in MySQL. So, to generate an auto_increment column you need to use a sequence.
This is an example of how you can achieve this.
create table test (id number, testdata varchar2(255));
create sequence test_seq
start with 1
increment by 1
nomaxvalue;
create trigger test_trigger
before insert on test
for each row
begin
select test_seq.nextval into :new.id from dual;
end;
So you create a sequence and use a trigger before each row is inserted to add its id.
So hibernate must be doing something like this, or instead of using the trigger doing
insert into test values(test_seq.nextval, 'no trigger needed!');
Note: Example taken from here

Categories