1

When I convert my database from Sql Server to MySql, the JPA @GeneratedValue does not work, return null, the code like this:

@Id
@GeneratedValue
@Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
    return id;
}

Should I change some properties?

1 Answer 1

1

You need to explicitly specify the generation strategy

@GeneratedValue(strategy = GenerationType.IDENTITY)

Basically there are 4 types of generation strategy:

  • Auto
  • Identity
  • Sequence
  • Table
Sign up to request clarification or add additional context in comments.

3 Comments

@GeneratedValue have default value(strategy = GenerationType.AUTO) ,when the database is MySql ,the value is AUTO_INCREMENT,isn't that?
AUTO actually means "the JPA provider chooses" ... not AUTO_INCREMENT. So you cannot bank on any particular one being used unless you choose IDENTITY, SEQUENCE or TABLE.
As already mentioned by @NeilStockton. GenerationType.AUTO makes JPA Provider to decide on the generation strategy, it may choose Table based or sequence based strategy so it is good that the data source you have has high privilege (It has to create a table or sequence and these are high privilege operations often restricted)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.