我正在尝试使用MySQL数据库启动spring boot项目,但是数据库存在一些问题。我尝试启动我的应用程序,并且服务器正在运行,但是休眠时不创建Tables等。
这是我的代码:
用户实体
@Entity
public class User {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
private String firstName;
private String lastName;
private String email;
private String password;
private String description;
private String profile_photo;
private LocalDate create;
private LocalDate update;
@OneToMany(mappedBy = "eventOwner")
private List<Event> ownedEvents;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getProfile_photo() {
return profile_photo;
}
public void setProfile_photo(String profile_photo) {
this.profile_photo = profile_photo;
}
public LocalDate getCreate() {
return create;
}
public void setCreate(LocalDate create) {
this.create = create;
}
public LocalDate getUpdate() {
return update;
}
public void setUpdate(LocalDate update) {
this.update = update;
}
public List<Event> getOwnedEvents() {
return ownedEvents;
}
public void setOwnedEvents(List<Event> ownedEvents) {
this.ownedEvents = ownedEvents;
}}
事件实体
@Entity
@Table(name = "events")
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Double longitude;
private Double latitude;
private String description;
private String header;
private LocalDate startData;
private LocalDate endData;
private LocalDate creat;
private LocalDate update;
private Filters filters;
@ManyToOne
@JoinColumn(name = "owner_id")
private User eventOwner;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public LocalDate getStartData() {
return startData;
}
public void setStartData(LocalDate startData) {
this.startData = startData;
}
public LocalDate getEndData() {
return endData;
}
public void setEndData(LocalDate endData) {
this.endData = endData;
}
public LocalDate getCreat() {
return creat;
}
public void setCreat(LocalDate creat) {
this.creat = creat;
}
public LocalDate getUpdate() {
return update;
}
public void setUpdate(LocalDate update) {
this.update = update;
}
public Filters getFilters() {
return filters;
}
public void setFilters(Filters filters) {
this.filters = filters;
}
public User getEventOwner() {
return eventOwner;
}
public void setEventOwner(User eventOwner) {
this.eventOwner = eventOwner;
}
}
这是我的属性:
spring.datasource.url= jdbc:mysql://localhost:3306/some_database?
requireSSL=false&useSSL=false
spring.datasource.username= user
spring.datasource.password= passw
logging.level.org.hibernate.SQL= DEBUG
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
这是我得到的错误
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing
DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via
JDBC Statement
和
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing
DDL "create table events (id bigint not null auto_increment, creat date,
description varchar(255), end_data date, event_type integer, max_age
integer not null, min_age integer not null, open_to_changes bit not null,
pets_allowed bit not null, price_range integer, smoking_allowed bit not
null, header varchar(255), latitude double precision, longitude double
precision, start_data date, update date, owner_id bigint, primary key (id))
engine=InnoDB" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlString(SchemaCreatorImpl.java:440) [hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlStrings(SchemaCreatorImpl.java:424) [hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.createFromMetadata(SchemaCreatorImpl.java:315) [hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.performCreation(SchemaCreatorImpl.java:166) [hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:135) [hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:121) [hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:155) [hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72) [hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:310) [hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467) [hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:939) [hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) [spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) [spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) [spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) [spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) [spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) [spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) [spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) [spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) [spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) [spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) [spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) [spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:853) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
有人知道如何解决这个问题吗?
答案 0 :(得分:2)
将 spring.jpa.hibernate.ddl-auto =创建-拖放更改为更新。它在开始时就删除了数据库,因此找不到所需的事件表来更改任何内容。
答案 1 :(得分:1)
在我的情况下,出现此问题的原因是,某些表具有保留给postgreSQL的名称。例如。 “喜欢”或“用户”。更改名称:
@Table(name="likes")
,效果很好。 也许有人有同样的问题。
答案 2 :(得分:1)
更改hibernate.cfg.xml语言的方言:-org.hibernate.dialect.MySQL5InnoDBDialect 要么 org.hibernate.dialect.MySQL55Dialect 要么 org.hibernate.dialect.MySQL55InnoDBDialect 要么 org.hibernate.dialect.MySQL57InnoDBDialect
答案 3 :(得分:1)
只需在您的application.properties
中指定方言即可。例如,对于MySQL 8:
hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
答案 4 :(得分:0)
嗯,我也遇到了这个问题,但是这些解决方案都不能解决我的问题。我正在将MySQL数据库打包与XAMPP一起使用。我尝试解决的问题但失败了
那么我为解决此问题所做的工作。可能是你的
实际上是我的朋友,也许对您这样的问题有所帮助。如果您有任何疑问,请发表评论。谢谢
答案 5 :(得分:0)
遇到类似问题,并在这里找到SupaMario的答案。
在我的情况下,我有一个名为orders
的表,但是MySql抛出了错误,因为它似乎是保留表/关键字。
我将其更改为customer_order
,并且一切正常。
答案 6 :(得分:0)
似乎是与保留的 PostgreSQL单词有关的错误。与最高答案(SupaMario的答案)类似,将我的一个列名从更改为
后,错误消失了@Column(name = "name", nullable = false)
到
@Column(name = "employee_name", nullable = false)
答案 7 :(得分:0)
如果您遇到此问题,请仔细检查您的类属性,以获取诸如key(在我的情况下)之类的保留关键字。
我将密钥更改为mkey,一切正常。
答案 8 :(得分:0)
对于使用 PostgreSQL 并且即使您的表名不是关键字也遇到相同问题的任何人,请将以下行放在您的 application.properties 中。它对我有用。
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
答案 9 :(得分:0)
就我而言,当我在特定实体属性中执行此操作时出现错误
@Column(name ="condition", nullable = false)
我把这个改成
@Column(name ="condition_of_house", nullable = false)
而且效果很好
答案 10 :(得分:0)
就我而言,发生错误是因为 mysql 用户没有 REFERENCES 权限。
解决这个问题;
或者您只需要授予 'your_user'@'localhost' REFERENCES 权限
查看文档:{{3}}
答案 11 :(得分:-1)
好的,有同样的问题。添加前缀为我解决了这个问题:
来自:
`
@Entity
@Table(name = "result_man")
@Proxy(lazy = false)
public class ResultManEntity {
@Id
@GeneratedValue
@Column(name = "id", unique = true, length = 128)
private long id;
private String key;
private int score;
private int index;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "result_id")
private ResultEntity result;
}`
收件人:
`@Entity
@Table(name = "result_man")
@Proxy(lazy = false)
public class ResultManEntity {
@Id
@GeneratedValue
@Column(name = "id", unique = true, length = 128)
private long id;
private String _key;
private int _score;
private int _index;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "result_id")
private ResultEntity result;`
答案 12 :(得分:-1)
就我而言,从数据库中删除表有帮助。 但是,请注意,这是一个培训项目:)