2017-07-17 109 views
0

我有測試平臺,其中有2個模型與ManyToOne連接。 Aaaaaand這是行不通的。休眠無法映射2個模型

@Entity 
@Table(name = "Tests") 
public class Test { 
@Id 
@Column(name = "id") 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private long id; 
private String name; 
private int count; 
private String description; 
private boolean free; 
@ManyToOne 
@JoinColumn(name = "authorId") 
private User tAuthor; 
private String section; 
@OneToMany(mappedBy = "rTest", fetch = FetchType.LAZY, cascade = { CascadeType.ALL }) 
private List<Result> results = new ArrayList<Result>(); 
@OneToMany(mappedBy = "qTest", fetch = FetchType.LAZY, cascade = { CascadeType.ALL }) 
private List<Question> questions = new ArrayList<Question>(); 
private String commentToAdmin; 
@Column(length = 32, columnDefinition = "varchar(32) default 'Developing'") 
@Enumerated(EnumType.STRING) 
private TestStatus status = TestStatus.Developing; 
@CreationTimestamp 
private Timestamp createdOn; 
@UpdateTimestamp 
private Timestamp updateDateTime; 

模式2:

@Entity 
@Table(name = "users") 
public class User { 
private Long id; 
private String username; 
private String password; 
private String passwordConfirm; 
@OneToMany(mappedBy = "tAuthor", fetch = FetchType.LAZY, cascade = { CascadeType.ALL }) 
private List<Test> listTests = new ArrayList<Test>(); 
private Set<Role> roles; 
@OneToMany(mappedBy = "rUser", fetch = FetchType.LAZY, cascade = { CascadeType.ALL }) 
private List<Result> result = new ArrayList<Result>(); 
@CreationTimestamp 
private Timestamp dateRegistration; 

異常文本:

SEVERE: Exception sending context initialized event to listener instance of  class [org.springframework.web.context.ContextLoaderListener] 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/appconfig-data.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory 
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.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:750) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446) 
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328) 
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4735) 
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5197) 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) 
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1439) 
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429) 
at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) 
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134) 
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:953) 
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:872) 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) 
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1439) 
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429) 
at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) 
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134) 
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:953) 
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262) 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) 
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422) 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) 
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:793) 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) 
at org.apache.catalina.startup.Catalina.start(Catalina.java:656) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:355) 
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:495) 
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1249) 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120) 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:860) 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) 
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425) 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849) 
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152) 
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:336) 
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) 
... 42 more 
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: users, for columns: [org.hibernate.mapping.Column(listTests)] 
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:349) 
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:322) 
at org.hibernate.mapping.Property.isValid(Property.java:241) 
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:496) 
at org.hibernate.mapping.RootClass.validate(RootClass.java:270) 
at org.hibernate.cfg.Configuration.validate(Configuration.java:1360) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1851) 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857) 
... 50 more 

我檢查過很多次之前發佈在此。並仍然不明白爲什麼我有這個錯誤

回答

0

在提供的模型中沒有用於類User的@Id anotation,但在stacktrace中沒有關於這一點的信息,所以我可以假設您使用了方法級別的anotation,病程exaption像你這樣,把標識anotation附近FILD ID,也許這將幫助你

+0

是的。謝謝!那是 – BruceVVayne

0

的異常的根本原因是這裏是這個: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: users, for columns: [org.hibernate.mapping.Column(listTests)]

我能想到的兩種可能的含義此例外,以及每個可能的原因。

首先,這可能意味着它無法弄清List<Test>Test部分是指什麼。這可能是因爲它沒有配置爲將Test檢測爲映射實體類。不過,我認爲這可能不是問題。其次,這可能意味着它無法弄清楚擁有關係的數據庫中的列應該是什麼類型。這是因爲它所涉及的主鍵的類型沒有被定義 - 在這種映射代碼中,@ManyToOne一側所指的類沒有@Id字段。果然,User沒有標記@Id的字段。要解決此問題,請將@Id添加到User中的Long id字段。