Thia吃了我的周末,我搜索了它,但可能会有几个可能导致它的问题,希望有人会帮助我。
我正在尝试使用第三个表通过一对多和多对一关系创建多对多关系,但我收到此错误:
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: PoojaFirstCategory, for columns: [org.hibernate.mapping.Column(poojaPanditRelationShip)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:314) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:292) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.Property.isValid(Property.java:239) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:483) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:270) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.cfg.Configuration.validate(Configuration.java:1327) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1789) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
... 26 more
2016/04/03 09:12:14,134 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-7) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956) [spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747) [spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) [spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_95]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_95]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_95]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:924) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:76) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:286) [spring-orm-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310) [spring-orm-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
... 20 more
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: PoojaFirstCategory, for columns: [org.hibernate.mapping.Column(poojaPanditRelationShip)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:314) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:292) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.Property.isValid(Property.java:239) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:483) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:270) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.cfg.Configuration.validate(Configuration.java:1327) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1789) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
... 26 more
2016/04/03 09:12:14,135 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/]] (MSC service thread 1-7) 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/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956) [spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747) [spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) [spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_95]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_95]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_95]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:924) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:76) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:286) [spring-orm-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310) [spring-orm-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
... 20 more
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: PoojaFirstCategory, for columns: [org.hibernate.mapping.Column(poojaPanditRelationShip)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:314) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:292) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.Property.isValid(Property.java:239) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:483) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:270) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.cfg.Configuration.validate(Configuration.java:1327) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1789) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
... 26 more
2016/04/03 09:12:14,540 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-7) Error listenerStart
2016/04/03 09:12:14,541 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-7) Context [] startup failed due to previous errors
我的实体是:
@Entity
public class PoojaFirstCategory {
private Long poojaFirstCategoryId;
private String name;
private String brief;
private Set<PoojaPanditRelationShip> poojaPanditRelationShip;
public PoojaFirstCategory() {
}
public PoojaFirstCategory(String name,String brief) {
this.name = name;
this.brief = brief;
poojaPanditRelationShip = new HashSet<PoojaPanditRelationShip>();
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getPoojaFirstCategoryId() {
return poojaFirstCategoryId;
}
public void setPoojaFirstCategoryId(Long poojaFirstCategoryId) {
this.poojaFirstCategoryId = poojaFirstCategoryId;
}
public Set<PoojaPanditRelationShip> getPoojaPanditRelationShip() {
return poojaPanditRelationShip;
}
@OneToMany(mappedBy = "poojaFirstCategory", cascade = CascadeType.ALL)
public void setPoojaPanditRelationShip(Set<PoojaPanditRelationShip> poojaPanditRelationShip) {
this.poojaPanditRelationShip = poojaPanditRelationShip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrief() {
return brief;
}
public void setBrief(String brief) {
this.brief = brief;
}
}
@Entity
public class PoojaSecondCategory {
private Long poojaSecondCategoryId;
private Long TId;
private String poojaName;
private String name;
private Integer experince;
private byte[] photo;
private Integer rate;
public PoojaSecondCategory() {
}
public PoojaSecondCategory(String poojaName,String name, Integer experince, Integer rate) {
this.poojaName = poojaName;
this.name = name;
this.experince = experince;
this.rate = rate;
}
private Set<PoojaPanditRelationShip> poojaPanditRelationShip;
public Set<PoojaPanditRelationShip> getPoojaPanditRelationShip() {
return poojaPanditRelationShip;
}
@OneToMany(mappedBy = "poojaSecondCategory" ,cascade = CascadeType.ALL)
public void setPoojaPanditRelationShip(Set<PoojaPanditRelationShip> poojaPanditRelationShip) {
this.poojaPanditRelationShip = poojaPanditRelationShip;
}
public String getPoojaName() {
return poojaName;
}
public void setPoojaName(String poojaName) {
this.poojaName = poojaName;
}
public Long getTId() {
return TId;
}
public void setTId(Long tId) {
TId = tId;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getPoojaSecondCategoryId() {
return poojaSecondCategoryId;
}
public void setPoojaSecondCategoryId(Long poojaSecondCategoryId) {
this.poojaSecondCategoryId = poojaSecondCategoryId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getExperince() {
return experince;
}
public void setExperince(Integer experince) {
this.experince = experince;
}
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
public Integer getRate() {
return rate;
}
public void setRate(Integer rate) {
this.rate = rate;
}
}
我的关系实体是:
@Entity
public class PoojaPanditRelationShip implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private PoojaFirstCategory poojaFirstCategory;
private PoojaSecondCategory poojaSecondCategory;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Id
@ManyToOne
@JoinColumn(name = "poojaFirstCategoryId")
public PoojaFirstCategory getPoojaFirstCategory() {
return poojaFirstCategory;
}
public void setPoojaFirstCategory(PoojaFirstCategory poojaFirstCategory) {
this.poojaFirstCategory = poojaFirstCategory;
}
@Id
@ManyToOne
@JoinColumn(name = "poojaSecondCategoryId")
public PoojaSecondCategory getPoojaSecondCategory() {
return poojaSecondCategory;
}
public void setPoojaSecondCategory(PoojaSecondCategory poojaSecondCategory) {
this.poojaSecondCategory = poojaSecondCategory;
}
}
在这里我试图填充一些虚拟数据:
@Component
public class StartOnStartUp implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private TouristFirstCategoryRepository touristFirstCategoryRepository;
@Autowired
private TouristSecondCategoryRepository touristSecondCategoryRepository;
@Autowired
private PoojaFirstCategoryRepository poojaFirstCategoryRepository;
@Autowired
private PoojaSecondCategoryRepository poojaSecondCategoryRepository;
@Override
@Transactional
public void onApplicationEvent(ContextRefreshedEvent arg0) {
PoojaFirstCategory poojaFirstCategory = new PoojaFirstCategory("StayaNarayan","SatayNarayan Ki pooja");
PoojaSecondCategory poojaSecondCategory = new PoojaSecondCategory("StayaNarayan","Pandit Brij Bhushan",23,1000);
PoojaPanditRelationShip poojaPanditRelationShip = new PoojaPanditRelationShip();
poojaPanditRelationShip.setPoojaFirstCategory(poojaFirstCategory);
poojaPanditRelationShip.setPoojaSecondCategory(poojaSecondCategory);
poojaFirstCategory.getPoojaPanditRelationShip().add(poojaPanditRelationShip);
poojaSecondCategoryRepository.save(poojaSecondCategory);
poojaFirstCategoryRepository.save(poojaFirstCategory);
}
我也在使用hibernate属性来创建基于实体类的表
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<!-- spring based scanning for entity classes -->
<property name="packagesToScan" value="first.varanasi.erp" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
答案 0 :(得分:1)
您应该在getter(或字段)上放置注释映射,而不是setter。所以不要这样:
@OneToMany(mappedBy = "poojaFirstCategory", cascade = CascadeType.ALL)
public void setPoojaPanditRelationShip(Set<PoojaPanditRelationShip> poojaPanditRelationShip) {
this.poojaPanditRelationShip = poojaPanditRelationShip;
}
你应该用这个:
@OneToMany(mappedBy = "poojaFirstCategory", cascade = CascadeType.ALL)
public Set<PoojaPanditRelationShip> getPoojaPanditRelationShip() {
return poojaPanditRelationShip;
}
HTH。