休眠不创建表,但没有错误消息 [英] hibernate not creating table but no error messages
问题描述
我正在做一个spring-boot项目,并尝试用hibernate创建一个表,当我运行应用程序并且服务器正常启动时,我没有遇到任何错误,但表没有创建。
package model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.PrePersist;
@Entity
@Table(name =status_update)
public class StatusUpdate {
@Id
@Column(name = id)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name =text)
private String text;
@Column(name =added)
@Temporal(TemporalType.TIMESTAMP)
私人添加日期;
@PrePersist
protected void onCreate(){
if(added == null){
added = new Date();
}
}
public StatusUpdate(String text){
this.text = text;
}
public StatusUpdate(String text,Date added){
this.text = text;
this.added = added;
}
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
public String getText(){
return text;
}
public void setText(String text){
this.text = text;
}
public Date getAdded(){
return added;
}
public void setAdded(添加日期){
this.added = added;
}
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result +((added == null)?0:added.hashCode());
result = prime * result +((id == null)?0:id.hashCode());
result = prime * result +((text == null)?0:text.hashCode());
返回结果;
$ b @Override
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass()!= obj.getClass())
return false;
StatusUpdate other =(StatusUpdate)obj;
if(added == null){
if(other.added!= null)
return false;
} else if(!added.equals(other.added))
return false;
if(id == null){
if(other.id!= null)
return false;
} else if(!id.equals(other.id))
return false;
if(text == null){
if(other.text!= null)
return false;
} else if(!text.equals(other.text))
return false;
返回true;
}
}
pom.xml
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns: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>
< parent>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-parent< / artifactId>
< version> 1.4.0.RELEASE< / version>
< / parent>
<属性>
< java.version> 1.8< /java.version>
< tiles.version> 3.0.7< /tiles.version>
< / properties>
< groupId> com.voja< / groupId>
< artifactId> spring-boot-tutorial< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
<依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-web< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.apache.tomcat.embed< / groupId>
< artifactId> tomcat-embed-jasper< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.apache.tiles< / groupId>
< artifactId> tiles-core< / artifactId>
< version> $ {tiles.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.apache.tiles< / groupId>
< artifactId> tiles-jsp< / artifactId>
< version> $ {tiles.version}< / version>
< /依赖关系>
< dependency>
< groupId> javax.servlet< / groupId>
< artifactId> jstl< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-devtools< / artifactId>
< /依赖关系>
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< version> 5.1.38< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-data-jpa< / artifactId>
< version> 1.3.5.RELEASE< / version>
< /依赖关系>
< /依赖关系>
<包装>战争< / packaging>
< build>
< plugins>
< plugin>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-maven-plugin< / artifactId>
<配置>
< executable> true< / executable>
< / configuration>
< / plugin>
< / plugins>
< / build>
< / project>
application.properties
debug = true
spring.datasource.url = jdbc:mysql:// localhost:3306 / springboottutorial
spring.datasource.username = springboot
spring .datasource.password = hello
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect
spring.jpa.generate-ddl = true
spring.jpa.show-sql = true
logging.level.org.hibernate.SQL = DEBUG
在这行中 dialect
下面还有一个黄线 spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect
其中说`spring.jpa.hibernate.dialect'是一个未知属性。你的意思是'spring.jpa.hibernate.ddl-auto',如果这可能是一个问题。
发现问题通过阅读另一篇文章,实际上他们内部的软件包和类存在问题,他们无法找到彼此,所以表未创建。
我创建了一个新项目,并将所有类放入同一个包中,并且它可以工作,所以我将根据它修复现有的项目。 b
I am doing a spring-boot project and trying to create a table with hibernate, I get no errors when I run the app and the server starts normally, but the table does not get created.
StatusUpdate.java
package model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.PrePersist;
@Entity
@Table(name="status_update")
public class StatusUpdate {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(name="text")
private String text;
@Column(name="added")
@Temporal(TemporalType.TIMESTAMP)
private Date added;
@PrePersist
protected void onCreate() {
if (added == null) {
added = new Date();
}
}
public StatusUpdate(String text) {
this.text = text;
}
public StatusUpdate(String text, Date added) {
this.text = text;
this.added = added;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Date getAdded() {
return added;
}
public void setAdded(Date added) {
this.added = added;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((added == null) ? 0 : added.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((text == null) ? 0 : text.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
StatusUpdate other = (StatusUpdate) obj;
if (added == null) {
if (other.added != null)
return false;
} else if (!added.equals(other.added))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (text == null) {
if (other.text != null)
return false;
} else if (!text.equals(other.text))
return false;
return true;
}
}
pom.xml
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<tiles.version>3.0.7</tiles.version>
</properties>
<groupId>com.voja</groupId>
<artifactId>spring-boot-tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
</dependencies>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
debug=true
spring.datasource.url=jdbc:mysql://localhost:3306/springboottutorial
spring.datasource.username=springboot
spring.datasource.password=hello
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
I also get a yellow line under dialect
in this line spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
which says `spring.jpa.hibernate.dialect' is an unknown property. Did you mean 'spring.jpa.hibernate.ddl-auto' in case that might be a problem.
Found the problem by reading another post, actually there was a problem with packages and classes within them, they couldn't find one another and so the table wasn't created.
I made a new project and put all classes inside the same package and it worked, so I will fix my existing project based on that.
这篇关于休眠不创建表,但没有错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!