1

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<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 field above is an enum defined as

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 file

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");

Please help.

Mohammed Shirhaan
  • 555
  • 1
  • 11
  • 27

0 Answers0