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:
No description
111 Replies
JavaBot
JavaBotā€¢3w ago
āŒ› This post has been reserved for your question.
Hey @userexit! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1stā€¢3w ago
Can you enable SQL logging and check the SQL statements? Can you also log the values you are getting?
userexit
userexitOPā€¢3w ago
ok
userexit
userexitOPā€¢3w ago
No description
userexit
userexitOPā€¢3w ago
public class PrefixIdGenerator extends SequenceStyleGenerator {

private String valuePrefix;
private String sequenceName;
private int initialValue;
private int increaseBy;

public PrefixIdGenerator(PrefixGenerator config, Member annotatedMember, CustomIdGeneratorCreationContext context) {
this.valuePrefix = config.prefix();
this.sequenceName = config.sequenceName();
this.initialValue = config.initialValue();
this.increaseBy = config.increaseBy();
}

@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
String key = valuePrefix + super.generate(session, object);
return key;
}

@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
params.setProperty(INITIAL_PARAM, String.valueOf(initialValue));
params.setProperty(INCREMENT_PARAM, String.valueOf(increaseBy));
params.setProperty(SEQUENCE_PARAM, this.sequenceName);
super.configure(new NamedBasicTypeImpl<>( new JavaTypeBasicAdaptor<>( Long.class ), NumericJdbcType.INSTANCE, "long"), params, serviceRegistry);
}
}
public class PrefixIdGenerator extends SequenceStyleGenerator {

private String valuePrefix;
private String sequenceName;
private int initialValue;
private int increaseBy;

public PrefixIdGenerator(PrefixGenerator config, Member annotatedMember, CustomIdGeneratorCreationContext context) {
this.valuePrefix = config.prefix();
this.sequenceName = config.sequenceName();
this.initialValue = config.initialValue();
this.increaseBy = config.increaseBy();
}

@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
String key = valuePrefix + super.generate(session, object);
return key;
}

@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
params.setProperty(INITIAL_PARAM, String.valueOf(initialValue));
params.setProperty(INCREMENT_PARAM, String.valueOf(increaseBy));
params.setProperty(SEQUENCE_PARAM, this.sequenceName);
super.configure(new NamedBasicTypeImpl<>( new JavaTypeBasicAdaptor<>( Long.class ), NumericJdbcType.INSTANCE, "long"), params, serviceRegistry);
}
}
im using this to set the values
@MappedSuperclass
@IdGeneratorType(PrefixIdGenerator.class)
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface PrefixGenerator {
String prefix();
String sequenceName();
int initialValue();
int increaseBy();
}
@MappedSuperclass
@IdGeneratorType(PrefixIdGenerator.class)
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface PrefixGenerator {
String prefix();
String sequenceName();
int initialValue();
int increaseBy();
}
@PrefixGenerator(sequenceName = "matricule_seq", prefix = "MATRICULE", initialValue = 100, increaseBy = 10)
@PrefixGenerator(sequenceName = "matricule_seq", prefix = "MATRICULE", initialValue = 100, increaseBy = 10)
dan1st
dan1stā€¢3w ago
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?
userexit
userexitOPā€¢3w ago
yeah
userexit
userexitOPā€¢3w ago
No description
userexit
userexitOPā€¢3w ago
this is the generated value returned by super.generate() 100 101 102
dan1st
dan1stā€¢3w ago
ok then it seems the DB doesn't care about the increment
userexit
userexitOPā€¢3w ago
yeah real question now is why ill try using a normal sequence generator and not using my custom one to see\
dan1st
dan1stā€¢3w ago
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 something
userexit
userexitOPā€¢3w ago
btw
No description
userexit
userexitOPā€¢3w ago
even when doing this
userexit
userexitOPā€¢3w ago
No description
userexit
userexitOPā€¢3w ago
it does 100,101,102 instead of 100,110,120 im going to do this rn
userexit
userexitOPā€¢3w ago
ffs
No description
userexit
userexitOPā€¢3w ago
not sure why its not working
userexit
userexitOPā€¢3w ago
No description
dan1st
dan1stā€¢3w ago
SELECT NEXT VALUE FOR YOUR_SEQUENCE_NAME I think
userexit
userexitOPā€¢3w ago
No description
userexit
userexitOPā€¢3w ago
yeah it does increment by 10s
userexit
userexitOPā€¢3w ago
No description
userexit
userexitOPā€¢3w ago
second time
dan1st
dan1stā€¢3w ago
now try using that sequence from the Java application
userexit
userexitOPā€¢3w ago
it works too when doing it here lol
No description
userexit
userexitOPā€¢3w ago
but it doesnt work when generating from jpa hibernate
dan1st
dan1stā€¢3w ago
You probably shouldn't generate the tables with Hibernate anyways that's good for testing but it might mess up some changes
userexit
userexitOPā€¢3w ago
rly ? heard its good lol maybe its the way im using configure
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
params.setProperty(INITIAL_PARAM, String.valueOf(initialValue));
params.setProperty(INCREMENT_PARAM, String.valueOf(increaseBy));
params.setProperty(SEQUENCE_PARAM, this.sequenceName);
super.configure(new NamedBasicTypeImpl<>( new JavaTypeBasicAdaptor<>( Long.class ), NumericJdbcType.INSTANCE, "long"), params, serviceRegistry);
}
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
params.setProperty(INITIAL_PARAM, String.valueOf(initialValue));
params.setProperty(INCREMENT_PARAM, String.valueOf(increaseBy));
params.setProperty(SEQUENCE_PARAM, this.sequenceName);
super.configure(new NamedBasicTypeImpl<>( new JavaTypeBasicAdaptor<>( Long.class ), NumericJdbcType.INSTANCE, "long"), params, serviceRegistry);
}
dan1st
dan1stā€¢3w ago
hm? maybe there's something weird with the increment here
userexit
userexitOPā€¢3w ago
maybe I shouldnt override configure at all
dan1st
dan1stā€¢3w ago
maybe compare the SQL statement hibernate uses for generation with the one you used for generating it manually
userexit
userexitOPā€¢3w ago
maybe in constructor i should set it its same
userexit
userexitOPā€¢3w ago
No description
userexit
userexitOPā€¢3w ago
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
dan1st
dan1stā€¢3w ago
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)
JavaBot
JavaBotā€¢3w ago
šŸ’¤ 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.
userexit
userexitOPā€¢3w ago
the one for sequence style generator
public class PrefixIdGenerator extends SequenceStyleGenerator {

private String valuePrefix;
private String sequenceName;
private int initialValue;
private int increaseBy;

public PrefixIdGenerator(PrefixGenerator config, Member annotatedMember, CustomIdGeneratorCreationContext context) {
this.valuePrefix = config.prefix();
this.sequenceName = config.sequenceName();
this.initialValue = config.initialValue();
this.increaseBy = config.increaseBy();
}

@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
Long generated = (Long) super.generate(session, object);
String key = valuePrefix + generated;
System.out.println(generated);
return key;
}

@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
params.setProperty(INITIAL_PARAM, String.valueOf(initialValue));
params.setProperty(INCREMENT_PARAM, String.valueOf(increaseBy));
params.setProperty(SEQUENCE_PARAM, this.sequenceName);
super.configure(new NamedBasicTypeImpl<>( new JavaTypeBasicAdaptor<>( Long.class ), NumericJdbcType.INSTANCE, "long"), params, serviceRegistry);
}
}
public class PrefixIdGenerator extends SequenceStyleGenerator {

private String valuePrefix;
private String sequenceName;
private int initialValue;
private int increaseBy;

public PrefixIdGenerator(PrefixGenerator config, Member annotatedMember, CustomIdGeneratorCreationContext context) {
this.valuePrefix = config.prefix();
this.sequenceName = config.sequenceName();
this.initialValue = config.initialValue();
this.increaseBy = config.increaseBy();
}

@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
Long generated = (Long) super.generate(session, object);
String key = valuePrefix + generated;
System.out.println(generated);
return key;
}

@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
params.setProperty(INITIAL_PARAM, String.valueOf(initialValue));
params.setProperty(INCREMENT_PARAM, String.valueOf(increaseBy));
params.setProperty(SEQUENCE_PARAM, this.sequenceName);
super.configure(new NamedBasicTypeImpl<>( new JavaTypeBasicAdaptor<>( Long.class ), NumericJdbcType.INSTANCE, "long"), params, serviceRegistry);
}
}
PrefixIdGenerator extends SequenceStyleGenerator
dan1st
dan1stā€¢3w ago
compare the SQL created by Hibernate for creating the sequence with your SQL for creating the sequence And what is INCREMENT_PARAM?
userexit
userexitOPā€¢3w ago
the how much to increment by
dan1st
dan1stā€¢3w ago
I meant the constant
userexit
userexitOPā€¢3w ago
its a String holding "increment_size"
userexit
userexitOPā€¢3w ago
No description
userexit
userexitOPā€¢3w ago
whats weird
dan1st
dan1stā€¢3w ago
maybe that one is wrong?
userexit
userexitOPā€¢3w ago
is that in the db it shows good šŸ˜­
dan1st
dan1stā€¢3w ago
.
userexit
userexitOPā€¢3w ago
No description
userexit
userexitOPā€¢3w ago
i mean this should be good
dan1st
dan1stā€¢3w ago
What SQL did you use when doing it manually? Did you try DROPing and recreating the sequence?
userexit
userexitOPā€¢3w ago
CREATE SEQUENCE dan1st START WITH 1 INCREMENT BY 10 MINVALUE 1 NOCACHE NOCYCLE; this is how i made the other one manually @dan1st
dan1st
dan1stā€¢3w ago
maybe the MINVALUE 1 NOCACHE NOCYCLE makes a difference
userexit
userexitOPā€¢3w ago
well rip because there is no way to set NOCACHE and NOCYCLE and MINVALUE in hibernate
dan1st
dan1stā€¢3w ago
you can creating it manually without these options and see what happens
userexit
userexitOPā€¢3w ago
Parameter(name = "optimizer", value = "none") this is the only caching there is i think in hiberante
dan1st
dan1stā€¢3w ago
.
userexit
userexitOPā€¢3w ago
nah i created it with no no cache and no no min size and no no cycle
No description
userexit
userexitOPā€¢3w ago
this is 100% a hibernate problem even when I use the official hibernate jpa @SequenceGenerator annotation it doesnt go up 10 by 10
dan1st
dan1stā€¢3w ago
. i.e. DROPing it manually and letting Hibernate recreate it
userexit
userexitOPā€¢3w ago
hibernate is recreating it every single time im using h2 db its in memory db embedded
dan1st
dan1stā€¢3w ago
ah ok so you are using the Spring H2 console integration thing?
userexit
userexitOPā€¢3w ago
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
dan1st
dan1stā€¢3w ago
Well Hibernate just sends SQL to H2
userexit
userexitOPā€¢3w ago
@GeneratedValue(generator = "my_gen", strategy = GenerationType.SEQUENCE) @SequenceGenerator(name = "my_gen", sequenceName = "my_seq", allocationSize = 10, initialValue = 100) when using this
dan1st
dan1stā€¢3w ago
Did you try updating Hibernate and H2? The initial value works, right?
userexit
userexitOPā€¢3w ago
100% yeah i can set any initial value maybe its teh INCREMENT ?
dan1st
dan1stā€¢3w ago
ig letting Hibernate generate stuff isn't that bad for in-memory DBs where you don't need to preserve data between runs probably
userexit
userexitOPā€¢3w ago
like maybe the INCREMENT enum
dan1st
dan1stā€¢3w ago
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?
userexit
userexitOPā€¢3w ago
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 :
dan1st
dan1stā€¢3w ago
Can you try specifying all your DDL in a schema.sql and use that (and disable Hiberate's auto schema updating thing)?
userexit
userexitOPā€¢3w ago
No description
userexit
userexitOPā€¢3w ago
No description
userexit
userexitOPā€¢3w ago
you see ? it does go up 10 by 10 when doing that from the db
dan1st
dan1stā€¢3w ago
.
userexit
userexitOPā€¢3w ago
what is ddl ?
dan1st
dan1stā€¢3w ago
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 it
userexit
userexitOPā€¢3w ago
sure
dan1st
dan1stā€¢3w ago
If you use a schema.sql, it should pick that up automatically
userexit
userexitOPā€¢3w ago
No description
userexit
userexitOPā€¢3w ago
same thing very weird thing
dan1st
dan1stā€¢3w ago
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
userexit
userexitOPā€¢3w ago
tried too same issue i think it has to do with hibernate or maybe the way im doing configure method
dan1st
dan1stā€¢3w ago
so not incrementing correctly?
userexit
userexitOPā€¢3w ago
it only increments correctly when i type in db SELECT NEXT VALUE FOR
dan1st
dan1stā€¢3w ago
Do you even need the configure method with the schema.sql? You did disable schema generation with Hibernate, right?
userexit
userexitOPā€¢3w ago
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
dan1st
dan1stā€¢3w ago
Did you not do that before?
userexit
userexitOPā€¢3w ago
no I just used hibernate auto update but that doesnt recompile my code :kekw:
dan1st
dan1stā€¢3w ago
...
userexit
userexitOPā€¢3w ago
nvm still doesnt work
dan1st
dan1stā€¢3w ago
What version of Spring and H2 are you using? Can you show your pom.xml?
marshall
marshallā€¢3w ago
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
dan1st
dan1stā€¢3w ago
yeah I think I also suggested something similar before
userexit
userexitOPā€¢3w ago
userexit
userexitOPā€¢3w ago
even out of box manner dont work lol thats the problem even when using @GeneratedSequence it dont go up 10 by 10
dan1st
dan1stā€¢3w ago
Why are you explicitly specifying the version of hibernate-core?
userexit
userexitOPā€¢3w ago
yeah
dan1st
dan1stā€¢3w ago
I asked why What do you mean with @GeneratedSequence? I don't see that annotation existing Do you meanYou mean @SequenceGenerator, right?
userexit
userexitOPā€¢3w ago
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
dan1st
dan1stā€¢3w ago
Can you show your entity class again? With @GeneratedSequence ah yes I can reproduce it
@SpringBootApplication
public class SpringJpaTest1Application implements ApplicationListener<ApplicationReadyEvent> {

private final MyEntityRepo repo;

public SpringJpaTest1Application(MyEntityRepo repo) {
this.repo = repo;
}

public static void main(String[] args) {
SpringApplication.run(SpringJpaTest1Application.class, args);
}

@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
repo.save(new MyEntity());
repo.save(new MyEntity());
System.out.println(repo.findAll());
}



}

@Entity
class MyEntity {
@Id
@GeneratedValue(generator = "test_seq")
@SequenceGenerator(name = "test_seq", initialValue = 100, allocationSize = 10)
private Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Override
public String toString() {
return "MyEntity [id=" + id + "]";
}
}

@Repository
interface MyEntityRepo extends CrudRepository<MyEntity, Long> {
}
@SpringBootApplication
public class SpringJpaTest1Application implements ApplicationListener<ApplicationReadyEvent> {

private final MyEntityRepo repo;

public SpringJpaTest1Application(MyEntityRepo repo) {
this.repo = repo;
}

public static void main(String[] args) {
SpringApplication.run(SpringJpaTest1Application.class, args);
}

@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
repo.save(new MyEntity());
repo.save(new MyEntity());
System.out.println(repo.findAll());
}



}

@Entity
class MyEntity {
@Id
@GeneratedValue(generator = "test_seq")
@SequenceGenerator(name = "test_seq", initialValue = 100, allocationSize = 10)
private Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Override
public String toString() {
return "MyEntity [id=" + id + "]";
}
}

@Repository
interface MyEntityRepo extends CrudRepository<MyEntity, Long> {
}
that's all that's needed
userexit
userexitOPā€¢3w ago
does it work for you that ?
dan1st
dan1stā€¢3w ago
ignoring the allocation size, just as it is the case with your code
userexit
userexitOPā€¢3w ago
it ignores it for you too then ?
dan1st
dan1stā€¢3w ago
yes
userexit
userexitOPā€¢3w ago
yeah idk hibernate problem
dan1st
dan1stā€¢3w ago
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 works
userexit
userexitOPā€¢3w ago
mm ive read that somewhere
JavaBot
JavaBotā€¢3w ago
šŸ’¤ 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.
dan1st
dan1stā€¢3w ago
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...
JavaBot
JavaBotā€¢3w ago
šŸ’¤ 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.
Want results from more Discord servers?
Add your server