Java Spring数据jpa:创建名为';实体管理工厂&x27;
大家好,我是stackoverflow的新手,也是spring数据jpa的新手 我目前正在开发个人网站,我有一个问题 我正在使用: 爪哇8 弹簧靴1.5.2 Spring boot starter数据jpa 1.5.2和 MySQL连接器 当我只有一个@Entity时,它工作正常,Hibernate成功地创建了MySQL模式,但当添加一些具有关系的@Entity时,我遇到以下错误:Java Spring数据jpa:创建名为';实体管理工厂&x27;,java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,大家好,我是stackoverflow的新手,也是spring数据jpa的新手 我目前正在开发个人网站,我有一个问题 我正在使用: 爪哇8 弹簧靴1.5.2 Spring boot starter数据jpa 1.5.2和 MySQL连接器 当我只有一个@Entity时,它工作正常,Hibernate成功地创建了MySQL模式,但当添加一些具有关系的@Entity时,我遇到以下错误: org.springframework.beans.factory.BeanCreationExcepti
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: 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:1628) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at fr.geekads.Application.main(Application.java:10) [classes/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 16 common frames omitted
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user, for columns: [org.hibernate.mapping.Column(ads)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:431) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:398) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.Property.isValid(Property.java:225) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:595) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:265) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
... 22 common frames omitted
这是我的代码结构:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.geekads</groupId>
<artifactId>geekads</artifactId>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
和Ad.java:
@Entity
@Table(name = "Ad")
public class Ad {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String logical_id;
private String title;
private String logoUrl;
private String shortDescription;
private String description;
@OneToOne
private AdCategory category;
private String postDate;
private String lastUp;
private Boolean putMoneyIntoPlay;
private int geekCoins;
private int views;
@OneToOne
private User user;
@OneToOne
private Project project;
private Boolean allowComments;
@OneToMany
private List<Comment> comments;
@OneToOne
private AdType type;
private String bannerUrl;
private String startDate;
private String endDate;
}
@实体
@表(name=“Ad”)
公共类广告{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
私有字符串逻辑标识;
私有字符串标题;
私有字符串logoUrl;
私有字符串短描述;
私有字符串描述;
@奥内托内
私人广告类别;
私有字符串postDate;
私人串;
私家布尔游戏;
私人国际geekCoins;
私人意见;
@奥内托内
私人用户;
@奥内托内
私人项目;
私有布尔allowComments;
@独身癖
私人名单评论;
@奥内托内
私有AdType类型;
私人字符串横幅;
私有字符串起始日期;
私有字符串结束日期;
}
我不明白为什么我会犯这个错误。。。我正在寻找一个为期2天的解决方案
如果您有想法,请提前感谢您的帮助我终于找到了解决方案。是我的错 解释: 我考虑的是对象,而不是关系数据库方面 例如,在我的User.java中,我与我的Ad.java有一个关系,在我的Ad.java中,我与我的User.java有一个关系,这导致了笛卡尔积。因此Hibernate无法创建SQL架构 因此,我根据概念数据模型重构了代码,效果很好 现在,User.java不再引用Ad.java,Ad.java用@OneToOne引用User.java的@id
问题解决了!:) 您在
Ad
上有@OneToOne
,但在用户
上有列出广告
。我不确定这是否有用,但您是否尝试将此@OneToOne
更改为@ManyToOne
?不起作用,但我遇到了一个新错误:org.springframework.beans.factory.BeanCreationException:创建名为“entityManagerFactory”的bean时出错,该bean在类路径资源中定义[org/springframework/boot/autoconfigure/orm/jpa/hibernatejpaaautoconfiguration.class]:调用init方法失败;嵌套异常为org.hibernate.AnnotationException:fr.geekads.model.User.ads上的OneToOne或ManyToOne引用未知实体:java.util.list这些关系中有没有应该是双向的?在这种情况下,将mappedBy
放在所有相关的关系上。发布persistence.xml
so people可以看到定义了哪些类there@NeilStockton事实上,这是由于我的双向关系。
@Entity
@Table(name = "Project")
public class Project {
@Id
private String name;
private String logoUrl;
private String shortDescription;
private String description;
private String projectUrl;
private Boolean showContact;
private String skype;
private String facebook;
private String twitter;
private String linkedin;
private String mail;
@OneToOne
private ProjectCategory category;
@OneToOne
private User owner;
@OneToMany
private List<User> members;
@OneToMany
private List<Ad> ads;
private int geekCoins;
private int views;
}
@Entity
@Table(name = "Comment")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne//(cascade = CascadeType.ALL, targetEntity = User.class)
private User user;
private String message;
private String postDate;
@OneToOne(targetEntity = Ad.class)
private Long ad_id;
}
@Entity
@Table(name = "Ad")
public class Ad {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String logical_id;
private String title;
private String logoUrl;
private String shortDescription;
private String description;
@OneToOne
private AdCategory category;
private String postDate;
private String lastUp;
private Boolean putMoneyIntoPlay;
private int geekCoins;
private int views;
@OneToOne
private User user;
@OneToOne
private Project project;
private Boolean allowComments;
@OneToMany
private List<Comment> comments;
@OneToOne
private AdType type;
private String bannerUrl;
private String startDate;
private String endDate;
}