早上好,
这是我的第一个问题,所以请保持温和! ;-)如果您需要更多信息,请告诉我!
我正在使用Hibernate 3.2.1并创建一个读取文本文件的Java Batch-Application,将记录转换为Java-Objects并将其保存到数据库中。文本文件由许多记录组组成,我应该只在完成一组记录后“提交”。 我使用序列生成器创建了对象ID。
我的问题是,在某些情况下,ID的顺序很重要 - 我不知道如何影响它。
[编辑]
我试着举个例子:
Retourmeldung r1 = new Retourmeldung();
Retourmeldung r2 = new Retourmeldung();
r1.process();
r2.process();
getEntityManager().merge(r1);
getEntityManager().merge(r2);
getEntityManager().flush();
r1必须具有比r2更低的ID(例如: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
答案 0 :(得分:1)
您需要为每个实体考虑单独的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;
答案 1 :(得分:0)
您可以在合并后刷新会话,以便在第二次合并之前插入它。