实体类未能使用JPA在我的MySQL数据库中创建表格。

huangapple go评论68阅读模式
英文:

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 = &quot;Coaches_TBL&quot;)

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&lt;Coaches, Integer&gt; {

	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.

huangapple
  • 本文由 发表于 2020年10月22日 13:15:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64475721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定