Spring boot 自动增量预处理

Posted

技术标签:

【中文标题】Spring boot 自动增量预处理【英文标题】:Spring boot Auto increment pre-conditioned 【发布时间】:2021-11-04 09:13:36 【问题描述】:

如何根据特定的给定值将 spring boot jpa 设置为自动递增 @Id ? 例如,如果我希望 @Id 值从(假设为 1000)开始并递增(假设为 10)?

所以第一个id是1000,第二个id是1010?

我的代码为:

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
public class Book 

    @Id
    @GeneratedValue
    private Long isbn;
    private String title;
    @ManyToOne
    @JsonIgnore
    private Reader reader;


@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
public class Reader 
    
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name="reader_id")
    private List<Book> myBooks = new ArrayList<>();
    
    public void addBook(Book book) 
        myBooks.add(book);
    
    
    public void removeBook(Book book) 
        myBooks.remove(book);
    


很多比xxx

.... 稍后添加:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.9.jar:5.3.9]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.9.jar:5.3.9]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.9.jar:5.3.9]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.0-SNAPSHOT.jar:2.6.0-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:713) ~[spring-boot-2.6.0-SNAPSHOT.jar:2.6.0-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) ~[spring-boot-2.6.0-SNAPSHOT.jar:2.6.0-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:299) ~[spring-boot-2.6.0-SNAPSHOT.jar:2.6.0-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1273) ~[spring-boot-2.6.0-SNAPSHOT.jar:2.6.0-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1262) ~[spring-boot-2.6.0-SNAPSHOT.jar:2.6.0-SNAPSHOT]
    at org.study.SpringBootSpringDataJpaManyToManyJoinQueryCourseStudent1Application.main(SpringBootSpringDataJpaManyToManyJoinQueryCourseStudent1Application.java:10) ~[classes/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-5.3.9.jar:5.3.9]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.9.jar:5.3.9]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.3.9.jar:5.3.9]
    ... 16 common frames omitted
Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:123) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:181) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:312) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:471) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1410) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.9.jar:5.3.9]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.9.jar:5.3.9]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.9.jar:5.3.9]
    ... 20 common frames omitted
Caused by: org.hibernate.InstantiationException: could not instantiate test object : org.study.entity.Course
    at org.hibernate.engine.internal.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:43) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    at org.hibernate.engine.internal.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:68) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    at org.hibernate.tuple.PropertyFactory.buildIdentifierAttribute(PropertyFactory.java:65) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:142) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:612) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:126) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:78) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    ... 28 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:78) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
    at org.hibernate.engine.internal.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:40) ~[hibernate-core-5.5.4.Final.jar:5.5.4.Final]
    ... 39 common frames omitted
Caused by: java.lang.Error: Unresolved compilation problems: 
    GenericGenerator cannot be resolved to a type
    Parameter cannot be resolved to a type
    Parameter cannot be resolved to a type
    Parameter cannot be resolved to a type

    at org.study.entity.Course.<init>(Course.java:31) ~[classes/:na]
    ... 45 common frames omitted

我收到此错误消息...

【问题讨论】:

请提供足够的代码,以便其他人更好地理解或重现问题。 【参考方案1】:

您可以按照以下示例自定义序列生成器:

@Id
@GeneratedValue
@GenericGenerator(
        name = "sequence-generator",
        strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
        parameters = 
                @Parameter(name = "sequence_name", value = "reader_sequence"),
                @Parameter(name = "initial_value", value = "1000"),
                @Parameter(name = "increment_size", value = "10")
        
)
private Long id;

【讨论】:

如何在这里发布! @AdelLinuix80s 您可以编辑您的答案并在此处附加错误堆栈跟踪 我编辑了问题并在上面添加了错误消息:) 对于我认为类似的错误是:***.com/a/18032170/3635454。检查您的课程中GenericGenerator 的导入。另外,它看起来需要一个带有 3 个参数的构造函数。 lombok 数据注释放在主代码中。它有所有构造函数,没有构造函数,,,为什么我们需要一个三参数构造函数。它就在那里!

以上是关于Spring boot 自动增量预处理的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Spring Boot 在 DynamoDB 中添加具有自动增量键的新项目

Spring Boot处理JSON数据

Spring boot 错误处理机制

spring boot/batch 通过代码禁用批量自动启动

Spring Boot 整合定时任务和异步任务处理

Spring boot错误处理以及定制错误页面