问题描述
我有一个用Spring MVC编写的简单的两表应用程序,它使用Hibernate。一切正常,但如果我尝试单元测试其中一个控制器,我会收到以下消息:
调用init方法失败;嵌套异常是java.lang.NoSuchMethodError:javax.persistence.JoinColumn.foreignKey()Ljavax / persistence / ForeignKey;
单元测试方法如下:
@RunWith(value = SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(file:C:/ NetBeansProjects / Library / build / web / WEB-INF / library-servlet.xml)
public class TestPersonController {
$ b $ @Autowired
private PersonService personService;
@Autowired
私有WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup(){
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
@Test
public void testGetProfile(){
Person mockPerson = new Person();
mockPerson.setPersonId(1);
mockPerson.setName(Mr Brown);
mockPerson.setAddress(火星上的Utopia Planitia);
mockPerson.setTelephone(1234567890);
mockPerson.setEmail([email protected]);
when(personService.get(1))。thenReturn(mockPerson);
尝试{
mockMvc.perform(get(/ person / profile?personId = 1))
.andExpect(status()。isOk())
.andExpect view()。name(view / profile))
.andExpect(forwardedUrl(/ WEB-INF / jsp / view / profile.jsp))
.andExpect(model()。attribute (person,hasSize(1)))
.andExpect(model()。attribute(person,hasItem(
allOf(
hasProperty(personId,is(1) ),
hasProperty(name,是(Mr Brown)),
hasProperty(地址,是(53 Onslow Gardens)),
hasProperty(telephone ,是(1234567890)),
hasProperty(email,是([email protected]))
)
)));
catch(例外e){
Misc.printStackTrace(e);
}
verify(personService,times(1))。get(1);
verifyNoMoreInteractions(personService);
}
}
构成应用程序模型的两个类通过1:M关系连接。第一个表Person有:
@Id
@Column(name =PERSON_ID,unique = true,可为空= false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer personId;
@Column(name =NAME,nullable = false,length = 50)
私有字符串名称;
@Column(name =ADDRESS,nullable = false,length = 100)
私有字符串地址;
@Column(name =TELEPHONE,nullable = false,length = 10)
private String telephone;
@Column(name =EMAIL,nullable = false,length = 50)
private String email;
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy =person)
@JsonManagedReference
private List< Book>图书;
第二个表格包含:
<$ p
$ @ $
@Column(name =BOOK_ID,unique = true,nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private整数bookId;
@Column(name =AUTHOR,nullable = false,length = 50)
private String author;
@Column(name =TITLE,nullable = false,length = 50)
私有字符串标题;
@Column(name =DESCRIPTION,nullable = false,length = 500)
私有字符串描述;
@Column(name =ONLOAN,nullable = false)
private boolean onLoan;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =person_id)
@JsonBackReference
private Person person;
这些类在所有其他情况下都能正常工作,除了Person控制器中的方法的新单元测试外。
我没有在我的类中使用@ForeignKey注释,因为似乎没有必要,如果我尝试,Java表示它是不必要的。 b
$ b
我有一个依赖项问题还是什么?
单元测试stacktrace如下所示:
Testsuite:library.person.TestPersonController
测试运行:1,失败:0,错误:1,跳过:0,耗时:2.085秒
-------------标准错误-----------------
log4j:WARN无法找到appender记录器(org.springframework.test.context.junit4.SpringJUnit4ClassRunner)。
log4j:WARN请正确初始化log4j系统。
------------- ---------------- ---------------
Testcase:testGetProfile(library.person.TestPersonController):导致错误
无法加载ApplicationContext
java.lang.IllegalStateException:未能在org.springframework.test.context中加载ApplicationContext
.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
在org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
在org.springframework.test.context.web.ServletTestExecutionListener .setUpRequestContextIfNecessary(ServletTestExecutionListener.java:155)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:100)
at org.springframework.test.context.TestContextManager.prepareTestInstance (TestContextManager.java:319)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunne r.java:212)
在org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock (SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild (SpringJUnit4ClassRunner.java:89)
在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)在org.springframework.test.context.junit4.statements
.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
引起的:org.springframework.beans.factory .BeanCreationException:创建名称为'adminCo的bean时出错ntroller':注入自动连接依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private library.service.PersonService library.controller.admin.AdminController.personService;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'personService'的bean时出错:注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private library.dao.PersonDAOImpl library.service.PersonService.personDAO;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'personDAOImpl'的bean时出错:注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private org.hibernate.SessionFactory library.dao.PersonDAOImpl.sessionFactory;嵌套异常是org.springframework.beans.factory.BeanCreationException:在URL [文件:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]中定义名称为'sessionFactory'的bean时创建错误:Invocation初始化方法失败;嵌套异常是java.lang.NoSuchMethodError:javax.persistence.JoinColumn.foreignKey()Ljavax / persistence / ForeignKey;
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
,位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)处,
)
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
在org.springframew在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons ork.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
(DefaultListableBeanFactory.java:700)
。在组织。 springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
处org.springframework.test org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
。 context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:129)
处org.springframework.test org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:60)
。 context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoad er.loadContext(AbstractDelegatingSmartContextLoader.java:250)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext( CacheAwareContextLoaderDelegate.java:91)
导致:org.springframework.beans.factory.BeanCreationException:无法自动装载字段:private library.service.PersonService library.controller.admin.AdminController.personService;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'personService'的bean时出错:注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private library.dao.PersonDAOImpl library.service.PersonService.personDAO;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'personDAOImpl'的bean时出错:注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private org.hibernate.SessionFactory library.dao.PersonDAOImpl.sessionFactory;嵌套异常是org.springframework.beans.factory.BeanCreationException:在URL [文件:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]中定义名称为'sessionFactory'的bean时创建错误:Invocation初始化方法失败;嵌套异常是java.lang.NoSuchMethodError:javax.persistence.JoinColumn.foreignKey()Ljavax / persistence / ForeignKey;
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java: 87)在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues
(AutowiredAnnotationBeanPostProcessor.java:289)
引起的:org.springframework.beans.factory.BeanCreationException:错误名称创建豆 personService':注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private library.dao.PersonDAOImpl library.service.PersonService.personDAO;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'personDAOImpl'的bean时出错:注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private org.hibernate.SessionFactory library.dao.PersonDAOImpl.sessionFactory;嵌套异常是org.springframework.beans.factory.BeanCreationException:在URL [文件:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]中定义名称为'sessionFactory'的bean时创建错误:Invocation初始化方法失败;嵌套异常是java.lang.NoSuchMethodError:javax.persistence.JoinColumn.foreignKey()Ljavax / persistence / ForeignKey;
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
,位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)处,
)
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
在org.springframew ork.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
在org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1014)
。在组织。 springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org。 springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
引起:org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private library.dao.PersonDAOImpl库。 service.PersonService.personDAO;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'personDAOImpl'的bean时出错:注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private org.hibernate.SessionFactory library.dao.PersonDAOImpl.sessionFactory;嵌套异常是org.springframework.beans.factory.BeanCreationException:错误创建具有名称的sessionFactory在URL定义豆[文件:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]:调用初始化方法失败;嵌套异常是java.lang.NoSuchMethodError:javax.persistence.JoinColumn.foreignKey()Ljavax / persistence / ForeignKey;
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:
at org.springframework.beans.factory.AnowowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
导致:org.springframework.beans.factory.BeanCreationException:创建名为' personDAOImpl':注入自动装载的依赖关系失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private org.hibernate.SessionFactory library.dao.PersonDAOImpl.sessionFactory;嵌套异常是org.springframework.beans.factory.BeanCreationException:错误创建具有名称的sessionFactory在URL定义豆[文件:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]:调用初始化方法失败;嵌套异常是java.lang.NoSuchMethodError:javax.persistence.JoinColumn.foreignKey()Ljavax / persistence / ForeignKey;
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
,位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)处,
)
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
在org.springframew ork.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
在org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1014)
。在组织。 springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org。 springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
导致:org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private org.hibernate.SessionFactory库。 dao.PersonDAOImpl.sessionFactory;嵌套异常是org.springframework.beans.factory.BeanCreationException:在URL [文件:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]中定义名称为'sessionFactory'的bean时创建错误:Invocation初始化方法失败;嵌套异常是java.lang.NoSuchMethodError:javax.persistence.JoinColumn.foreignKey()Ljavax / persistence / ForeignKey;
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:
at org.springframework.beans.factory.AnowowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
导致:org.springframework.beans.factory.BeanCreationException:创建名为'在URL [file:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]中定义的sessionFactory:调用init方法失败;嵌套异常是java.lang.NoSuchMethodError:javax.persistence.JoinColumn.foreignKey()Ljavax / persistence / ForeignKey;在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject
(AbstractBeanFactory.java:304 )美元,org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228 b $ b)
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300 )
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory。 findAutowireCandidates(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957)
at org.springframework.beans.factory.support.DefaultListableBeanFactory。 resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
由java.lang.NoSuchMethodError引发:javax。 persistence.JoinColumn.foreignKey()Ljavax /持久/ ForeignKey的;
在org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:2881)在org.hibernate.cfg.AnnotationBinder.processElementAnnotations
(AnnotationBinder.java:1795)
在org.hibernate作为.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796)
at org.hibernate.cfg.Configuration $ MetadataSourceQueue.processAnnotatedClassesQueue (Configuration.java:3788)
在org.hibernate.cfg.Configuration $ MetadataSourceQueue.processMetadata(Configuration.java:3742)
在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410 )
在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
在org。 springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:343)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:431)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:416)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
测试library.person.TestPersonController失败
Props文件配置:
<! - Library的Spring属性文件。 - >
< bean id =propertiesFactoryclass =org.springframework.beans.factory.config.PropertiesFactoryBean>
< property name =location>
<值> /WEB-INF/library.properties< /值>
< / property>
< / bean>
JoinColumn.foreignKey 是在JPA 2.1中引入的,它直到4.3版才由Hibernate 4实现。如果您使用的是旧版本的Hibernate 4,那么请尝试升级到4.3.x。
如果您已经使用Hibernate 4.3,那么请确保您也使用JPA 2.1确保API和实现匹配。
I have a simple two table application written in Spring MVC and which uses Hibernate. Everything works perfectly well but if I try to unit test one of the controllers, I get message:
Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
The unit test method is below:
@RunWith(value=SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("file:C:/NetBeansProjects/Library/build/web/WEB-INF/library- servlet.xml") public class TestPersonController { @Autowired private PersonService personService; @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void testGetProfile() { Person mockPerson = new Person(); mockPerson.setPersonId(1); mockPerson.setName("Mr Brown"); mockPerson.setAddress("Utopia Planitia on Mars"); mockPerson.setTelephone("1234567890"); mockPerson.setEmail("[email protected]"); when(personService.get(1)).thenReturn(mockPerson); try { mockMvc.perform(get("/person/profile?personId=1")) .andExpect(status().isOk()) .andExpect(view().name("view/profile")) .andExpect(forwardedUrl("/WEB-INF/jsp/view/profile.jsp")) .andExpect(model().attribute("person", hasSize(1))) .andExpect(model().attribute("person", hasItem( allOf( hasProperty("personId", is(1)), hasProperty("name", is("Mr Brown")), hasProperty("address", is("53 Onslow Gardens")), hasProperty("telephone", is("1234567890")), hasProperty("email", is("[email protected]")) ) ))); } catch(Exception e) { Misc.printStackTrace(e); } verify(personService, times(1)).get(1); verifyNoMoreInteractions(personService); }
}
The two classes forming the model of the application are joined via a 1:M relationship. The first table Person has:
@Id @Column(name="PERSON_ID", unique=true, nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) private Integer personId; @Column(name="NAME", nullable=false, length=50) private String name; @Column(name="ADDRESS", nullable=false, length=100) private String address; @Column(name="TELEPHONE", nullable=false, length=10) private String telephone; @Column(name="EMAIL", nullable=false, length=50) private String email; @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="person") @JsonManagedReference private List<Book> books;
And the second table has:
@Id @Column(name="BOOK_ID", unique=true, nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) private Integer bookId; @Column(name="AUTHOR", nullable=false, length=50) private String author; @Column(name="TITLE", nullable=false, length=50) private String title; @Column(name="DESCRIPTION", nullable=false, length=500) private String description; @Column(name="ONLOAN", nullable=false) private boolean onLoan; @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="person_id") @JsonBackReference private Person person;
These classes work correctly under all other circumstances except this new unit test of a method in the Person controller.
I do not use the @ForeignKey annotation in my classes because there seems to be no need to and if I try, Java says it is unnecessary.
Do I have a dependency issue or what?
The unit test stacktrace follows:
Testsuite: library.person.TestPersonController Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.085 sec ------------- Standard Error ----------------- log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner). log4j:WARN Please initialize the log4j system properly. ------------- ---------------- --------------- Testcase: testGetProfile(library.person.TestPersonController): Caused an ERROR Failed to load ApplicationContext java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101) at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:155) at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:100) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:319) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:212) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private library.service.PersonService library.controller.admin.AdminController.personService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private library.dao.PersonDAOImpl library.service.PersonService.personDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory library.dao.PersonDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey; at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:129) at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:60) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:250) at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64) at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private library.service.PersonService library.controller.admin.AdminController.personService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private library.dao.PersonDAOImpl library.service.PersonService.personDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory library.dao.PersonDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey; at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private library.dao.PersonDAOImpl library.service.PersonService.personDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory library.dao.PersonDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey; at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1014) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private library.dao.PersonDAOImpl library.service.PersonService.personDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory library.dao.PersonDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey; at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory library.dao.PersonDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey; at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1014) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory library.dao.PersonDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey; at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:C:/NetBeansProjects/Library/build/web/WEB-INF/library-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey; at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1014) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) Caused by: java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey; at org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:2881) at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1795) at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963) at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796) at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788) at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928) at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:343) at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:431) at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:416) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) Test library.person.TestPersonController FAILED
Props file config:
<!-- Spring Properties file for Library. --> <bean id="propertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location"> <value>/WEB-INF/library.properties</value> </property> </bean>
JoinColumn.foreignKey() was introduced with JPA 2.1, which was not implemented by Hibernate 4 until version 4.3. If you're using an older version of Hibernate 4 then try upgrading to 4.3.x.
If you're already using Hibernate 4.3 then make sure you're also using JPA 2.1 to make sure the API and implementation match up.
这篇关于错误:javax.persistence.JoinColumn.foreignKey()使用Spring控制器的Ljavax / persistence / ForeignKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!