表在 Spring Boot 中的 Hibernate JPA 中不存在。

huangapple 未分类评论46阅读模式
英文:

Table does not exist in Spring boot hibernate JPA

问题

我正在尝试在Spring Boot中开发一个小的API概念验证(POC),但持续收到“表不存在”错误。我相信Hibernate会自动在数据库中创建表。

这是我的代码:

Application.java

package FirstPackage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }

}

MainController.java

package FirstPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/main")
public class MainController {

    @Autowired
    private StudentRepository studentRepository;

    @PostMapping(path = "/add")
    public @ResponseBody String addNewStudent(@RequestParam String name) {
        Student s = new Student();
        s.setName(name);
        s.setRoll_Number(101);
        s.setStanderd("I");
        studentRepository.save(s);
        return "Data Entered";
    }

    @GetMapping(path = "/all")
    public Iterable<Student> getAllStudent() {
        return studentRepository.findAll();
    }
}

Student.java

package FirstPackage;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "students_table")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String name;
    private Integer roll_Number;
    private String standerd;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getRoll_Number() {
        return roll_Number;
    }

    public void setRoll_Number(Integer roll_Number) {
        this.roll_Number = roll_Number;
    }

    public String getStanderd() {
        return standerd;
    }

    public void setStanderd(String standerd) {
        this.standerd = standerd;
    }
}

StudentRepository.java

package FirstPackage;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface StudentRepository extends CrudRepository<Student, Integer> {

}

application.properties

spring.jpa.hibernate.dll-auto = create
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3303/db_student
spring.datasource.username=springuser
spring.datasource.password=Root@1234
英文:

iam trying to develop small API POC in spring boot but consistently getting Table does not exist error I believe hibernate automatically creates a table in database.

here is my code
..............................................................

Application.java

package FirstPackage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(Application.class, args);

	}

}

........................................................................................

MainController.java

package FirstPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller   //This refers as this is main controller class
@RequestMapping(&quot;/main&quot;)   //This means request will be mapping on localhost:8081/main 
public class MainController {
	
	@Autowired   //This means Spring will take care of this object creation
	private StudentRepository studentRepository;
	
	@PostMapping(path = &quot;/add&quot;)
	public @ResponseBody String addNewStudent(@RequestParam String name)/*, @RequestParam Integer roll_Number, @RequestParam String standerd)*/
	{
		Student s = new Student();
		s.setName(name);
		s.setRoll_Number(101);
		s.setStanderd(&quot;I&quot;);
		studentRepository.save(s);
		return &quot;Data Entered&quot;;
	}
	
	@GetMapping(path = &quot;/all&quot;)
	public Iterable&lt;Student&gt; getAllStudent()
	{
		return studentRepository.findAll();
	}
}

...........................................................................................

Student.java

package FirstPackage;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/*this is going to be entity Model this tell Hibernate to make a table out of it */
@Entity
@Table(name = &quot;students_table&quot;)
public class Student {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;
	private String name;
	private Integer roll_Number;
	private String standerd;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public Integer getRoll_Number() {
		return roll_Number;
	}

	public void setRoll_Number(Integer roll_Number) {
		this.roll_Number = roll_Number;
	}
	public String getStanderd() {
		return standerd;
	}

	public void setStanderd(String standerd) {
		this.standerd = standerd;
	}
}

.....................................................................................

package FirstPackage;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import FirstPackage.Student;

//This will be AUTO IMPLEMENTED by Spring into a Bean called StudentRepository
//CRUD refers Create, Read, Update, Delete
@Repository
public interface StudentRepository extends CrudRepository&lt;Student, Integer&gt; 
{

}

......................................................................................
application.properties

spring.jpa.hibernate.dll-auto = create
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3303/db_student
spring.datasource.username=springuser
spring.datasource.password=Root@1234

huangapple
  • 本文由 发表于 2020年4月9日 17:18:34
  • 转载请务必保留本文链接:https://java.coder-hub.com/61117801.html
匿名

发表评论

匿名网友

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

确定