hibernate没有创建表但没有错误消息
我正在做一个spring-boot项目并尝试使用hibernate创建一个表,当我运行应用程序并且服务器正常启动时,我没有错误,但是表没有被创建。
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
4.0.0 org.springframework.boot spring-boot-starter-parent 1.4.0.RELEASE 1.8 3.0.7 com.voja spring-boot-tutorial 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-web org.apache.tomcat.embed tomcat-embed-jasper org.apache.tiles tiles-core ${tiles.version} org.apache.tiles tiles-jsp ${tiles.version} javax.servlet jstl org.springframework.boot spring-boot-devtools mysql mysql-connector-java 5.1.38 org.springframework.boot spring-boot-starter-data-jpa 1.3.5.RELEASE war org.springframework.boot spring-boot-maven-plugin true
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
我也在这行的spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
dialect
下得到一条黄线,其中的“spring.jpa.hibernate.dialect”是一个未知属性。 你的意思是’spring.jpa.hibernate.ddl-auto’以防可能出现问题。
如果您使用的是jpa和hibernate,请在application.properties文件中添加以下属性
spring.jpa.hibernate.ddl-auto=update
尝试将此添加到您的application.properties:
spring.jpa.hibernate.ddl-auto=create
它不适合您,因为您没有使用它,我的意思是,JPA在需要使用它时创建数据库。 您可以尝试编写使用它的测试,或添加一个rest库来尝试它,只需将其添加到您的application.properties;
spring.jpa.hibernate.ddl-auto=create
然后创建此接口:
@RepositoryRestResource public interface IStatusRepository extends CrudRepository { }
你还需要这种依赖;
org.springframework.boot spring-boot-starter-data-rest
创建一个测试更容易,但我想你正在尝试构建一个rest的Web服务,所以试试看它的工作原理=)
对于您的application.properties文件,请按照以下链接进行操作
它就像: spring.jpa.generate-ddl=true
然后添加spring.jpa.hibernate.ddl-auto=create-drop
通过阅读另一篇文章找到问题,实际上它们中的包和类存在问题,它们找不到彼此,因此没有创建表。
我创建了一个新项目并将所有类放在同一个包中并且它工作正常,因此我将基于此修复现有项目。
有时,您选择的属性名称中会出现问题。 像文本,值等,它们是数据库的保留关键字。 因此,您可以看到创建表的日志,但不会创建表。 要解决此问题,只需更改您bean中的属性名称或提供显式列名称。
示例:mysql中不支持属性或列名“value”。 因此我的表格没有创建。 我必须将其重命名为’valueString’。
希望这可以帮助。