euraad
euraad

Reputation: 2836

Using BigInteger as primary key in Spring JPA gives - Incorrect column specifier for column 'id'

I want to have an infinitive primary key and that is BigInteger. https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-type-conversions.html

BIGINT[(M)] [UNSIGNED]  BIGINT [UNSIGNED]   java.lang.Long, if UNSIGNED java.math.BigInteger

So I created my entity

public class Alarm {

    // ID
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger id;

And when I create the tables, I get the following error.

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table alarm (id decimal(19,2) not null auto_increment, frequencyp0p1p2 integer not null, frequencyp3p7p8 integer not null, frequencyp4 integer not null, frequencyp6p5 integer not null, port_description varchar(255), primary key (id)) engine=MyISAM" via JDBC Statement

Caused by: java.sql.SQLSyntaxErrorException: Incorrect column specifier for column 'id'

So what have gone wrong here?

Upvotes: 1

Views: 5823

Answers (1)

Andreas
Andreas

Reputation: 159086

BIGINT is the same as a Java Long, not a BigInteger.

The question even quoted that, so use Long, not BigInteger, which Hibernate seems to want to map to decimal(19,2), and that data type cannot be an auto-generated value.


UPDATE: As mentioned by dunni in a comment, you can force Hibernate to use the BIGINT data type with the @Column annotation:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "BIGINT")
private BigInteger id;

Upvotes: 8

Related Questions