Eugene
Eugene

Reputation: 11075

Why database table is not automatically generated for a Entity

I'm working with Spring Data JPA, the implementation of JPA is hibernate. The database I'm using is Mysql.
The Entity will be mapped to a Mysql table and the table is created automatically when I launched the Spring application. However, when I changed the ID from Integer to String, the table is disappeared and can't be created anymore. I'm a little confused why it happens. Let me post the code comparison as below.

The Entity before:

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity 
public class User {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Integer id;

  private String name;

  private String email;

  public String getId() {
    return id;
  }

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

  public String getName() {
    return name;
  }

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

  public String getEmail() {
    return email;
  }

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

}

The Entity after:

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity 
public class User {
  @Id
  private String id;

  private String name;

  private String email;

  public String getId() {
    return id;
  }

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

  public String getName() {
    return name;
  }

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

  public String getEmail() {
    return email;
  }

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

}

The CrudRepository before:

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

import gearon.model.entity.User;

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

@Component
public interface UserRepository extends CrudRepository<User, Integer> {

}

The CrudRepository after:

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

import gearon.model.entity.User;

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

@Component
public interface UserRepository extends CrudRepository<User, String> {

}

Edit1

The error output in console is:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement

and it's caused by

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too long; max key length is 1000 bytes

Edit2

Found a similar question without answer but votes, it seems that more people met this issues.

Can't use String as @Id with SpringData

Upvotes: 0

Views: 376

Answers (2)

dglozano
dglozano

Reputation: 6607

I think your issue is related to this. Basically, depending on the version of MySQL you are using and the encoding of your strings, the maximum VARCHAR size can vary.

I don't know those parameters of you, but to be safe I would define my String PK with a maximum length of 191. Depending on your version of MySQL and encoding, can be higher.

@Id
@Column(length = 191)
private String id;

Upvotes: 1

Alien
Alien

Reputation: 15878

In case If you don't specify an id generation strategy, Hibernate will by default use GenerationType.AUTO.

If you want a String UUID as an id, you could use

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "PR_KEY")
private String prKey;

Refer how-to-use-id-with-string-type-in-jpa-hibernate

Upvotes: 0

Related Questions