gpt4 book ai didi

java - 如何使用 Hibernate 以特殊顺序保存对象?

转载 作者:太空宇宙 更新时间:2023-11-04 07:11:10 26 4
gpt4 key购买 nike

早上好,

这是我的第一个问题,所以请温柔一点! ;-) 如果您需要更多信息,请告诉我!

我正在使用 Hibernate 3.2.1 并创建一个 Java 批处理应用程序,该应用程序读取文本文件,将记录转换为 Java 对象并将其保存到数据库中。文本文件由许多记录组组成,我应该只在一组记录完全完成后“提交”。我使用序列生成器创建对象 ID。

我的问题是,在某些情况下,ID 的顺序很重要 - 我不知道如何影响这一点。

[编辑]

我会尝试举一个例子:

Retourmeldung r1 = new Retourmeldung();
Retourmeldung r2 = new Retourmeldung();

r1.process();
r2.process();

getEntityManager().merge(r1);
getEntityManager().merge(r2);
getEntityManager().flush();

r1 的 ID 必须低于 r2(例如:r1.getID() = 213 和 r2.getID() = 214),因为另一个程序将再次读取数据库,并且不同的记录应处于特殊设置中。

[/编辑]

有什么想法吗?

谢谢,海伦

编辑:添加时出现异常堆栈跟踪

@Id 
@Column(name="ID",unique=true,nullable=false,precision=16)
@GeneratedValue(generator="RETOURMELDUNG")
@SequenceGenerator(name="RETOURMELDUNG",sequenceName="SEQ_RETOURMELDUNG")
@Generated(GenerationTime.INSERT)



> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'functionServiceDAO': Autowiring of fields
> failed; nested exception is
> org.springframework.beans.factory.BeanCreationException: Could not
> autowire field: private
> at.sozvers.svb.lkfEinarb.batch.dao.jpa.Speicherservice
> at.sozvers.svb.lkfEinarb.batch.dao.impl.FunctionServiceDAOImpl.speicherservice;
> nested exception is
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'speicherservice': Autowiring of fields
> failed; nested exception is
> org.springframework.beans.factory.BeanCreationException: Could not
> autowire field: private javax.persistence.EntityManagerFactory
> at.sozvers.svb.lkfEinarb.batch.dao.jpa.Speicherservice.factory; nested
> exception is org.springframework.beans.factory.BeanCreationException:
> Error creating bean with name 'entityManagerFactory' defined in class
> path resource [META-INF/lkfEinarbApplicationContext.xml]: Invocation
> of init method failed; nested exception is
> java.lang.NullPointerException
> at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
> at java.security.AccessController.doPrivileged(Native Method)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
> at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
> at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
> at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
> at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
> at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
> at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
> at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
> at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
> at at.sozvers.svb.lkfEinarb.batch.main.LkfEinarbVerarbeitung.<init>(LkfEinarbVerarbeitung.java:26)
> at at.sozvers.svb.lkfEinarb.batch.main.LkfEinarbMain.main(LkfEinarbMain.java:20)
> Caused by: org.springframework.beans.factory.BeanCreationException: Could not
> autowire field: private
> at.sozvers.svb.lkfEinarb.batch.dao.jpa.Speicherservice
> at.sozvers.svb.lkfEinarb.batch.dao.impl.FunctionServiceDAOImpl.speicherservice;
> nested exception is
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'speicherservice': Autowiring of fields
> failed; nested exception is
> org.springframework.beans.factory.BeanCreationException: Could not
> autowire field: private javax.persistence.EntityManagerFactory
> at.sozvers.svb.lkfEinarb.batch.dao.jpa.Speicherservice.factory; nested
> exception is org.springframework.beans.factory.BeanCreationException:
> Error creating bean with name 'entityManagerFactory' defined in class
> path resource [META-INF/lkfEinarbApplicationContext.xml]: Invocation
> of init method failed; nested exception is
> java.lang.NullPointerException
> at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
> at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
> at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
> ... 15 more
> Caused by: org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'speicherservice': Autowiring of fields
> failed; nested exception is
> org.springframework.beans.factory.BeanCreationException: Could not
> autowire field: private javax.persistence.EntityManagerFactory
> at.sozvers.svb.lkfEinarb.batch.dao.jpa.Speicherservice.factory; nested
> exception is org.springframework.beans.factory.BeanCreationException:
> Error creating bean with name 'entityManagerFactory' defined in class
> path resource [META-INF/lkfEinarbApplicationContext.xml]: Invocation
> of init method failed; nested exception is
> java.lang.NullPointerException
> at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
> at java.security.AccessController.doPrivileged(Native Method)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
> at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
> at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
> at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
> at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
> at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
> at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:671)
> at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:610)
> at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
> ... 17 more
> Caused by: org.springframework.beans.factory.BeanCreationException: Could not
> autowire field: private javax.persistence.EntityManagerFactory
> at.sozvers.svb.lkfEinarb.batch.dao.jpa.Speicherservice.factory; nested
> exception is org.springframework.beans.factory.BeanCreationException:
> Error creating bean with name 'entityManagerFactory' defined in class
> path resource [META-INF/lkfEinarbApplicationContext.xml]: Invocation
> of init method failed; nested exception is
> java.lang.NullPointerException
> at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
> at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
> at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
> ... 30 more
> Caused by: org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'entityManagerFactory' defined in class path
> resource [META-INF/lkfEinarbApplicationContext.xml]: Invocation of
> init method failed; nested exception is java.lang.NullPointerException
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
> at java.security.AccessController.doPrivileged(Native Method)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
> at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
> at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
> at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
> at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
> at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
> at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:671)
> at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:610)
> at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
> ... 32 more
> Caused by: java.lang.NullPointerException
> at org.hibernate.cfg.annotations.PropertyBinder.make(PropertyBinder.java:145)
> at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1738)
> at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1180)
> at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:710)
> at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:452)
> at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:268)
> at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1112)
> at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1233)
> at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
> at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:869)
> at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:407)
> at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:126)
> at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:224)
> at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:291)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
> ... 44 more
> Programm LkfEinarb-Batch fehlerhaftes Programmende 17-12-2013 10:11:17

最佳答案

您需要为每个实体考虑单独的 id 生成器,如下所示。

@Id
@Column(name = "id", unique = true, nullable = false)
@SequenceGenerator(name = "classname_seq_generator", sequenceName = "classname_id_seq")
@GeneratedValue(generator = "classname_seq_generator")
@Generated(GenerationTime.INSERT)
Long id;

关于java - 如何使用 Hibernate 以特殊顺序保存对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20628169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com