【问题标题】:Java Spring Boot, JPA- I am trying to make connection to database and load tables to it but it doenst workJava Spring Boot,JPA-我正在尝试连接到数据库并向其加载表,但它确实有效
【发布时间】:2023-01-23 04:08:13
【问题描述】:

我正在尝试使用 java spring boot 和 JPA 在 MySQL 中创建表。我的程序加载但没有创建表。

我正在做一个简单的项目,登录后用户可以借书,员工可以添加书。但我无法让实体正确。

package model;

import jakarta.persistence.*;

@Entity
@Table(name="BOOKS")
public class Book {

    //region Attributes

    @Id
    private String ean;

    private String title;
    private String author;

    @Enumerated(EnumType.STRING)
    private BookCategory bookCategory;

    @ManyToOne
    @JoinColumn(name="borrower_id", nullable=false)
    private Borrower borrower;
    //endregion

    //region Constructors

    public Book(String ean, String title, String author, BookCategory bookCategory) {
        this.ean = ean;
        this.title = title;
        this.author = author;
        this.bookCategory = bookCategory;
    }

    public Book() {
    }

    //endregion



    //region Getters & Setters
    public String getEan() {
        return ean;
    }

    public Book setEan(String ean) {
        this.ean = ean;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public Book setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getAuthor() {
        return author;
    }

    public Book setAuthor(String author) {
        this.author = author;
        return this;
    }

    public BookCategory getBookCategory() {
        return bookCategory;
    }

    public Book setBookCategory(BookCategory bookCategory) {
        this.bookCategory = bookCategory;
        return this;
    }

    //endregion
}
package model;

public enum BookCategory {

    WAR, ROMANCE, POETRY, HORROR, SCIENCE_FICTION, BIOGRAPHY, ESSAY

}
package model;

import jakarta.persistence.*;
import java.util.Set;

@Entity
@Table(name="BORROWER")
public class Borrower extends User{

    //region Attributes

    @Column(nullable = false)
    private String addressStreet;

    @Column(nullable = false)
    private String addressHouseNumber;

    @Column(nullable = false)
    private String addressCity;

    @Column(nullable = false)
    private String phoneNumber;

    @Column(nullable = false)
    private String email;

    @OneToMany(mappedBy="borrower")
    private Set<Book> books;

    //endregion

    //region Constructors

    public Borrower(Long id, String userName, String password, String addressStreet, String addressHouseNumber, String addressCity, String phoneNumber, String email) {
        super(id, userName, password);
        this.addressStreet = addressStreet;
        this.addressHouseNumber = addressHouseNumber;
        this.addressCity = addressCity;
        this.phoneNumber = phoneNumber;
        this.email = email;
    }

    public Borrower(String userName, String password, String addressStreet, String addressHouseNumber, String addressCity, String phoneNumber, String email) {
        super(userName, password);
        this.addressStreet = addressStreet;
        this.addressHouseNumber = addressHouseNumber;
        this.addressCity = addressCity;
        this.phoneNumber = phoneNumber;
        this.email = email;
    }

    public Borrower() {
    }

    //endregion

    //region Getters & Setters

    public String getAddressStreet() {
        return addressStreet;
    }

    public Borrower setAddressStreet(String addressStreet) {
        this.addressStreet = addressStreet;
        return this;
    }

    public String getAddressHouseNumber() {
        return addressHouseNumber;
    }

    public Borrower setAddressHouseNumber(String addressHouseNumber) {
        this.addressHouseNumber = addressHouseNumber;
        return this;
    }

    public String getAddressCity() {
        return addressCity;
    }

    public Borrower setAddressCity(String addressCity) {
        this.addressCity = addressCity;
        return this;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public Borrower setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
        return this;
    }

    public String getEmail() {
        return email;
    }

    public Borrower setEmail(String email) {
        this.email = email;
        return this;
    }

    //endregion

}
package model;

import jakarta.persistence.*;

@Entity
@Table(name="EMPLOYEE")
public class Employee extends User {

    //region Attributes

    @Column(nullable = false, length = 20)
    private int employeeId;

    //endregion

    //region Constructors

    public Employee(Long id, String userName, String password, int employeeId) {
        super(id, userName, password);
        this.employeeId = employeeId;
    }

    public Employee(int employeeId) {
        this.employeeId = employeeId;
    }

    public Employee() {

    }

    //endregion

    //region Getters & Setters

    public int getEmployeeId() {
        return employeeId;
    }

    public Employee setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
        return this;
    }


    //endregion

}




package model;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name="USER")
@Inheritance(strategy = InheritanceType.JOINED)

@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@ToString
@Getter@Setter

public class User {

    //region Attributes
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name="username", nullable = false, unique = true, length = 20)

    private String userName;

    @Column(nullable = false)
    private String password;
    //endregion

    //region Constructors

    public User(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    //endregion


}
package projectlibrary.library;

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

@SpringBootApplication
public class LibraryApplication {

    public static void main(String[] args) {
        SpringApplication.run(LibraryApplication.class, args);
    }

}

应用程序.properties

spring.jpa.hibernate.ddl-auto=update

spring.datasource.url=jdbc:mysql://localhost:3306/Library
spring.datasource.username=userlibrary
spring.datasource.password=pwlibrary
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.defer-datasource-initialization=true
spring.web.resources.static-locations = classpath:/static/

数据库详细信息

DROP SCHEMA IF EXISTS `Library`;
CREATE SCHEMA IF NOT EXISTS `Library` DEFAULT CHARACTER SET utf8 ;
USE `Library` ;
-- Create user and grant access
CREATE USER 'userlibrary'@'%' IDENTIFIED BY 'pwlibrary';
GRANT ALL PRIVILEGES ON Library. * TO 'userlibrary'@'%';
FLUSH PRIVILEGES;

我试着看教程,但不知何故我看不出我做错了什么

【问题讨论】:

  • 表名User是保留关键字。
  • 我改了还是没成功
  • 如果存在则删除模式 Library 如果不存在则创建模式 Library 默认字符集 utf8 ;使用Library
  • 这不对吗?
  • 模式是在 MySQL 中创建的

标签: java mysql spring jpa annotations


【解决方案1】:

问题在于您配置的包。

您的主类在包名称projectlibrary.library 中声明

但是您的实体是在名为model 的单独包中声明的。

你有两个选择。

  1. 将实体移动到projectlibrary.library的子包中
  2. 在应用程序的主类之上使用@EntityScan(basePackages={"model"})。如果您的存储库不属于 projectlibrary.library,您可能还需要 @EnableJpaRepositories(basePackages={"..."})

【讨论】:

    猜你喜欢
    • 2020-11-08
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2022-01-13
    • 2018-01-06
    • 1970-01-01
    • 2022-07-17
    • 2016-12-17
    相关资源
    最近更新 更多