I am getting org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL for Create table in h2 database

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

I am getting org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL for Create table in h2 database

问题

我正在使用 h2 数据库进行 junit 测试。我在这里找到了一个类似的问题链接。但我没有使用任何保留关键字。我得到了以下错误:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table user (id integer not null auto_increment, address varchar(255), create_date_time datetime(6), email varchar(255), first_name varchar(255), last_name varchar(255), password varchar(255), phone varchar(255), place varchar(255), user_name varchar(255), primary key (id)) engine=InnoDB" via JDBC Statement
	at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]

我的实体类 "user" 如下:

import java.io.Serializable;
import java.time.LocalDateTime;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.CreationTimestamp;

import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor
@Data
public class User implements Serializable, Comparable<User> {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String firstName;
    private String lastName;
    private String userName;
    private String password;
    private String phone;
    private String email;
    private String address;

    @Enumerated(EnumType.STRING)
    private Place place;

    @CreationTimestamp
    private LocalDateTime createDateTime;

    public User(Integer id, String firstName, String lastName, String userName, String password, String phone, String email, String address, String place) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.userName = userName;
        this.password = password;
        this.phone = phone;
        this.email = email;
        this.address = address;
        this.place = Place.valueOf(place);
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(this.getId()).append(this.getFirstName()).append(this.getLastName())
                .append(this.getEmail()).toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof User))
            return false;
        User other = (User) obj;
        return new EqualsBuilder().append(this.getId(), other.getId()).append(this.getFirstName(), other.getFirstName())
                .append(this.getLastName(), other.getLastName()).append(this.getEmail(), other.getEmail()).isEquals();
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", userName=" + userName
                + ", password=" + password + ", phone=" + phone + ", email=" + email + ", address=" + address
                + ", createDateTime=" + createDateTime + "]";
    }

    @Override
    public int compareTo(User other) {
        return this.getId() > other.getId() ? 1 : this.getId() < other.getId() ? -1 : 0;
    }

}

上面的 "Place" 字段是一个枚举,定义如下:

public enum Place {

    SOUTH("SOUTH"),
    NORTH("NORTH"),
    EAST("EAST"),
    WEST("WEST"),
    CENTRAL("CENTRAL");

    private String description;

    private Place(String desc) {
        this.setDescription(desc);
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

"Data.sql" 文件内容如下:

INSERT INTO user(id,firstName,lastName) VALUES(1,"firstName","lastName");
INSERT INTO user(id,firstName,lastName) VALUES(2,"firstName1","lastName1");
INSERT INTO user(id,firstName,lastName) VALUES(3,"firstName2","lastName2");
INSERT INTO user(id,firstName,lastName) VALUES(4,"firstName3","lastName3");

请帮助。

英文:

I am using h2 database for junit testing. I found a similar question here. But I am not using any reserved key word.
I am getting the following error

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table user (id integer not null auto_increment, address varchar(255), create_date_time datetime(6), email varchar(255), first_name varchar(255), last_name varchar(255), password varchar(255), phone varchar(255), place varchar(255), user_name varchar(255), primary key (id)) engine=InnoDB" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]

My Entity class user is as below

import java.io.Serializable;
import java.time.LocalDateTime;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.CreationTimestamp;

import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor
@Data
public class User implements Serializable, Comparable&lt;User&gt; {

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;

  private String firstName;
  private String lastName;
  private String userName;
  private String password;
  private String phone;
  private String email;
  private String address;

  @Enumerated(EnumType.STRING)
  private Place place;

  @CreationTimestamp
  private LocalDateTime createDateTime;

  public User(Integer id, String firstName, String lastName, String userName, String password, String phone, String email, String address, String place) {
	  this.id = id;
	  this.firstName = firstName;
	  this.lastName = lastName;
	  this.userName = userName;
	  this.password = password;
	  this.phone = phone;
	  this.email = email;
	  this.address = address;
	  this.place = Place.valueOf(place);
}

  @Override
  public int hashCode() {
    	return new HashCodeBuilder().append(this.getId()).append(this.getFirstName()).append(this.getLastName())
			.append(this.getEmail()).toHashCode();
}

  @Override
  public boolean equals(Object obj) {
   	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (!(obj instanceof User))
		return false;
	User other = (User) obj;
	return new EqualsBuilder().append(this.getId(), other.getId()).append(this.getFirstName(), other.getFirstName())
			.append(this.getLastName(), other.getLastName()).append(this.getEmail(), other.getEmail()).isEquals();
}

  @Override
  public String toString() {
   	return &quot;User [id=&quot; + id + &quot;, firstName=&quot; + firstName + &quot;, lastName=&quot; + lastName + &quot;, userName=&quot; + userName
			+ &quot;, password=&quot; + password + &quot;, phone=&quot; + phone + &quot;, email=&quot; + email + &quot;, address=&quot; + address
			+ &quot;, createDateTime=&quot; + createDateTime + &quot;]&quot;;
}

  @Override
  public int compareTo(User other) {
	  return this.getId() &gt; other.getId() ? 1 : this.getId() &lt; other.getId() ? -1 : 0;
}

}

Place field above is an enum defined as

public enum Place {

  SOUTH(&quot;SOUTH&quot;),
  NORTH(&quot;NORTH&quot;),
  EAST(&quot;EAST&quot;),
  WEST(&quot;WEST&quot;),
  CENTRAL(&quot;CENTRAL&quot;);

  private String description;

  private Place(String desc) {
      this.setDescription(desc);
  }

  public String getDescription() {
      return description;
  }

  public void setDescription(String description) {
      this.description = description;
  }
}

Data.sql file

INSERT INTO user(id,firstName,lastName) VALUES(1,&quot;firstName&quot;,&quot;lastName&quot;);
INSERT INTO user(id,firstName,lastName) VALUES(2,&quot;firstName1&quot;,&quot;lastName1&quot;);
INSERT INTO user(id,firstName,lastName) VALUES(3,&quot;firstName2&quot;,&quot;lastName2&quot;);
INSERT INTO user(id,firstName,lastName) VALUES(4,&quot;firstName3&quot;,&quot;lastName3&quot;);

Please help.

huangapple
  • 本文由 发表于 2020年4月8日 15:12:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/61095146.html
匿名

发表评论

匿名网友

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

确定