使用JPA和spring不存在表xyz

时间:2017-04-25 14:45:38

标签: java spring jpa spring-boot spring-data-jpa

我有桌子

@Entity
@Table(name = "User", schema = "swprojekt", catalog = "")
public class UserEntity {
  private String password;
  private String email;
  private int userId;
  private String firstName;
  private String lastName;
  private String addressHouseNumber;
  private String addressStreet;
  private String addressCity;
  private String addressState;
  private String phoneNumber;
  private int isAdmin;

  @Basic
  @Column(name = "password")
  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  @Basic
  @Column(name = "email")
  public String getEmail() {
    return email;
  }

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

  @Id
  @Column(name = "user_id")
  public int getUserId() {
    return userId;
  }

  public void setUserId(int userId) {
    this.userId = userId;
  }

  @Basic
  @Column(name = "first_name")
  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  @Basic
  @Column(name = "last_name")
  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  @Basic
  @Column(name = "address_house_number")
  public String getAddressHouseNumber() {
    return addressHouseNumber;
  }

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

  @Basic
  @Column(name = "address_street")
  public String getAddressStreet() {
    return addressStreet;
  }

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

  @Basic
  @Column(name = "address_city")
  public String getAddressCity() {
    return addressCity;
  }

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

  @Basic
  @Column(name = "address_state")
  public String getAddressState() {
    return addressState;
  }

  public void setAddressState(String addressState) {
    this.addressState = addressState;
  }

  @Basic
  @Column(name = "phone_number")
  public String getPhoneNumber() {
    return phoneNumber;
  }

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

  @Basic
  @Column(name = "is_admin")
  public int getIsAdmin() {
    return isAdmin;
  }

  public void setIsAdmin(int isAdmin) {
    this.isAdmin = isAdmin;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    UserEntity that = (UserEntity) o;

    if (userId != that.userId) return false;
    if (isAdmin != that.isAdmin) return false;
    if (password != null ? !password.equals(that.password) : that.password != null) return false;
    if (email != null ? !email.equals(that.email) : that.email != null) return false;
    if (firstName != null ? !firstName.equals(that.firstName) : that.firstName != null) return false;
    if (lastName != null ? !lastName.equals(that.lastName) : that.lastName != null) return false;
    if (addressHouseNumber != null ? !addressHouseNumber.equals(that.addressHouseNumber) : that.addressHouseNumber != null)
      return false;
    if (addressStreet != null ? !addressStreet.equals(that.addressStreet) : that.addressStreet != null) return false;
    if (addressCity != null ? !addressCity.equals(that.addressCity) : that.addressCity != null) return false;
    if (addressState != null ? !addressState.equals(that.addressState) : that.addressState != null) return false;
    if (phoneNumber != null ? !phoneNumber.equals(that.phoneNumber) : that.phoneNumber != null) return false;

    return true;
  }

  @Override
  public int hashCode() {
    int result = password != null ? password.hashCode() : 0;
    result = 31 * result + (email != null ? email.hashCode() : 0);
    result = 31 * result + userId;
    result = 31 * result + (firstName != null ? firstName.hashCode() : 0);
    result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
    result = 31 * result + (addressHouseNumber != null ? addressHouseNumber.hashCode() : 0);
    result = 31 * result + (addressStreet != null ? addressStreet.hashCode() : 0);
    result = 31 * result + (addressCity != null ? addressCity.hashCode() : 0);
    result = 31 * result + (addressState != null ? addressState.hashCode() : 0);
    result = 31 * result + (phoneNumber != null ? phoneNumber.hashCode() : 0);
    result = 31 * result + isAdmin;
    return result;
  }
}

它叫做用户。

我想将数据插入其中:

@Transactional
public boolean register( UserEntity u){
    Query q =  em.createNativeQuery("Select * from User where email = ?1", UserEntity.class);
    q.setParameter(1, u.getEmail());
    List<UserEntity> registeredUser = q.getResultList();

    if( registeredUser.isEmpty()) {
        try{
            u.setPassword(BCrypt.hashpw(u.getPassword(), BCrypt.gensalt(16)));;
            em.persist(u);
            return true;
        }catch( DataAccessException e ){
            return false;
        }
    }
    return false;
}

然而,这会引发异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'swprojekt.user' doesn't exist

我已经读过它区分大小写,所以我试图重命名

@Table(name =“User”...)到@Table(name =“user”...)

我定义了

spring.datasource.username = name spring.datasource.password =密码

在application.properties文件中。

哪个没有帮助。此外,当我尝试访问数据库中的另一个表时,它可以工作。

什么可能导致此异常?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

@EntityScan仅标识特定持久化上下文应使用哪些类。

示例:

@EntityScan(basePackages = {"com.project.model"})  // scan JPA entities

spring.jpa.hibernate.ddl-auto = create

更新:

如果有其他表格,请更改表格名称。

@Table(name = "User_Tbl")

答案 1 :(得分:0)

你在那边有你的房产吗?因为很可能数据库的名称没有设置为例如:

dbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sys        //name of the database
jdbc.username=root
jdbc.password=asdadsadsadsa
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=false
hibernate.format_sql=false

mysql的例子