I set incrment to be 10, and I set initial value to be 100 but its not working, did it with hibernat
hibernate jpa and spring boot:
111 Replies
ā
This post has been reserved for your question.
Hey @userexit! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
Can you enable SQL logging and check the SQL statements?
Can you also log the values you are getting?
ok
im using this to set the values
Can you run the
SELECT NEXT VALUE
command manually in the DB?
Can you log the value returned by super.generate()
? What happens with that?yeah
this is the generated value returned by super.generate()
100
101
102
ok then it seems the DB doesn't care about the increment
yeah real question now is why
ill try using a normal sequence generator and not using my custom one to see\
Can you try creating a custom sequence with a custom increment with SQL (not from Java) and run the
SELECT NEXT VALUE
from that?
Also you might want to try using NOCACHE
in the CREATE SEQUENCE
, maybe that changes somethingbtw
even when doing this
it does 100,101,102
instead of 100,110,120
im going to do this rn
ffs
not sure why its not working
SELECT NEXT VALUE FOR YOUR_SEQUENCE_NAME
I thinkyeah it does increment by 10s
second time
now try using that sequence from the Java application
it works too when doing it here lol
but it doesnt work when generating from jpa
hibernate
You probably shouldn't generate the tables with Hibernate anyways
that's good for testing but it might mess up some changes
rly ?
heard its good lol
maybe its the way im using configure
hm?
maybe there's something weird with the increment here
maybe I shouldnt override configure at all
maybe compare the SQL statement hibernate uses for generation with the one you used for generating it manually
maybe in constructor i should set it
its same
im going to remove the @Override of configure
and im going to do it in constructor
when I do that it says cant recognize String as an id
oh yea
its because
when the constructor finishes
the super.configure
launches
and it configures the type back to be a Long
so not overriding it
makes it still be a Long
thats why overriding it in constructor is not good enough
yeah the normal sequence generator is probably made for longs
What configure is that?
(btw don't expect me to know Hibernate APIs because I probably don't - I just look them up when needed)
š¤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
the one for sequence style generator
PrefixIdGenerator extends SequenceStyleGenerator
compare the SQL created by Hibernate for creating the sequence with your SQL for creating the sequence
And what is
INCREMENT_PARAM
?the how much to increment by
I meant the constant
its a String
holding "increment_size"
whats weird
maybe that one is wrong?
is that in the db it shows good
š
.
i mean this should be good
What SQL did you use when doing it manually?
Did you try DROPing and recreating the sequence?
CREATE SEQUENCE dan1st
START WITH 1
INCREMENT BY 10
MINVALUE 1
NOCACHE
NOCYCLE;
this is how i made the other one manually @dan1st
maybe the
MINVALUE 1 NOCACHE NOCYCLE
makes a differencewell rip because there is no way to set NOCACHE
and NOCYCLE
and MINVALUE
in hibernate
you can creating it manually without these options
and see what happens
Parameter(name = "optimizer", value = "none")
this is the only caching there is i think
in hiberante
.
nah i created it with no no cache and no no min size and no no cycle
this is 100% a hibernate problem
even when I use the official hibernate jpa @SequenceGenerator
annotation
it doesnt go up 10 by 10
.
i.e. DROPing it manually and letting Hibernate recreate it
hibernate is recreating it every single time
im using h2 db
its in memory db
embedded
ah ok
so you are using the Spring H2 console integration thing?
yeahhh
I think the issue is hibernate but like 100%
even when using their own generator
not my custom oen
it fails to do it
Well Hibernate just sends SQL to H2
@GeneratedValue(generator = "my_gen", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "my_gen", sequenceName = "my_seq",
allocationSize = 10, initialValue = 100)
when using this
Did you try updating Hibernate and H2?
The initial value works, right?
100%
yeah
i can set any initial value
maybe its teh INCREMENT ?
ig letting Hibernate generate stuff isn't that bad for in-memory DBs where you don't need to preserve data between runs
probably
like maybe the INCREMENT enum
Is it a
BIGINT
in H2?
the sequence?
And you said if you manually create a sequence without specifying MINVALUE
, INCREMENT BY
, NOCACHE
and NOCYCLE
, it works?yeah
u know whats funny
even when doing it with hibernate
and jpa
it works
if i query from the db
instead of my application
like
check
:
Can you try specifying all your DDL in a schema.sql and use that (and disable Hiberate's auto schema updating thing)?
you see ?
it does go up 10 by 10 when doing that from the db
.
what is ddl
?
data definition language - especially all SQL used for setting up the DB
The
CREATE TABLE
, CREATE SEQUENCE
, etc
So writing all the SQL by yourself instead of letting Hibernate generate itsure
If you use a schema.sql, it should pick that up automatically
same thing
very weird thing
Try using the exact SQL statement you entered (with the name you entered before and also change the code to use that name)
so including NOCACHE and all that stuff
just to see what happens
tried too
same issue
i think it has to do with hibernate
or maybe the way im doing configure method
so not incrementing correctly?
it only increments correctly when i type in db SELECT NEXT VALUE FOR
Do you even need the configure method with the schema.sql?
You did disable schema generation with Hibernate, right?
yeah I did
oh yea
so the issue is not the configure method
okay
the issue i think is i have to restart completetly my app @dan1st
i think there is some weird caching going on
Did you not do that before?
no I just used hibernate auto update
but that doesnt recompile my code
:kekw:
...
nvm still doesnt work
What version of Spring and H2 are you using?
Can you show your pom.xml?
bro, as far as I can judge, ur id consist of string + generated seq nubmer. Why not keeping it as two separate columns and use it as combined key? In that way you can have that generation without custom selfmade stuff in more out-of-box manner
I know, it differs from what you wanted to achieve originally, however
yeah I think I also suggested something similar before
even out of box manner dont work lol
thats the problem
even when using @GeneratedSequence
it dont go up 10 by 10
Why are you explicitly specifying the version of hibernate-core?
yeah
I asked why
What do you mean with
@GeneratedSequence
?
I don't see that annotation existing
Do you meanYou mean @SequenceGenerator
, right?yeah that one
it doesnt matter
i can delete it
everything functions
i only tried using it to have access to LongType
but can't do that apparently
Can you show your entity class again?
With
@GeneratedSequence
ah yes I can reproduce it
that's all that's neededdoes it work for you
that ?
ignoring the allocation size, just as it is the case with your code
it ignores it for you too then
?
yes
yeah
idk
hibernate problem
well idk whether it's actually Hibernate
could also be something else inside Spring
it also happens with hsqldb
and also with MySQL
ohhhhhhhh
actually it seems like
allocationSize
means "change it by that number in the sequence" but the id is only incremented by 1
So an allocation size of 10 means "use 10 elements in the sequence for incrementing the ID by 1"
well or maybe not
ohhh yeah I know why
so it means "Spring takes 10 elements at once"
so it reserves space for 10 elements
and then you can insert 10 elements without accessing the sequence
and when these are inserted, Spring will generate the next one from the sequence
you could get arond this by calculating the values yourself
or alternatively you could try something else: You use an allocation size of 1 but you create the sequence via SQL with INCREMENT BY
set to something higher but idk whether that worksmm
ive read that somewhere
š¤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
Stack Overflow
Should javax.persistence.SequenceGenerator.allocationSize() be cons...
I have an issue using Hibernate javax.persistence.SequenceGenerator.allocationSize() property. When it is set to 1 I face performance issues when inserting a lot of records to database. Otherwise w...
š¤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.