英文:
Entity class not creating a table in mySql database using jpa
问题
我可以在其他项目中创建表格,但是在这个项目中,从实体类创建表格失败。我在Spring Boot JPA中使用Eclipse有以下实体类:
package com.abc.allcoaches.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Coaches_TBL")
public class Coaches {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String t;
private String c;
public Coaches() {
}
public Coaches(int id, String team, String coach ) {
super();
this.id = id;
this.t = team;
this.c = coach;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getT() {
return t;
}
public void setT(String t) {
this.t = t;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
}
仓库类如下:
package com.abc.allcoaches.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.abc.allcoaches.entity.Coaches;
public interface CoachesRepository extends JpaRepository<Coaches, Integer> {
Coaches findByTeam(String team);
}
这是属性文件:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/football2020db
spring.datasource.username = root
spring.datasource.password = password
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
但是在没有任何错误的情况下运行项目后,我在MySQL Workbench中看不到创建的表格。我尝试了多次但没有成功。如果您找到解决方案,请告诉我。谢谢!
英文:
While I can create tables in other projects but this project is not creating table from the entity class.I have following entity class in spring boot jpa using eclipse:
package com.abc.allcoaches.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Coaches_TBL")
public class Coaches {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String t;
private String c;
public Coaches() {
}
public Coaches(int id, String team, String coach ) {
super();
this.id = id;
this.t = team;
this.c = coach;
}
public int getId() {
return id;
}
public void setID(int id) {
this.id = id;
}
public String getT() {
return t;
}
public void setT(String t) {
this.t = t;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
}
The repository class is as below:
package com.abc.allcoaches.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.abc.allcoaches.entity.Coaches;
public interface CoachesRepository extends JpaRepository<Coaches, Integer> {
Coaches findByTeam(String team);
}
Here is the property file:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/football2020db
spring.datasource.username = root
spring.datasource.password = password
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
But after running the project without any error, I dont see table created in mySql work bench. I tried multiple times without any success. Please let me know if you find a solution. Cheers!
答案1
得分: 2
我发现了导致表未被创建的问题。我的应用程序类(具有main(String [] args)方法)位于不同的包中。我重新整理了包结构,问题得以解决。
英文:
I found the problem which was causing the table not being created. My application class (with main(String [] args) method) was in a different package. I refactored the packages and the problem was resolved.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论