-1

i know this question was asked before but a can't resolve the problem. I want to generate a table by @Entitiy class.

@Entity
public class Test implements Serializable{
    @Id
    @GeneratedValue long id;

.
.
.
}

in my app properties i got :

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.show_sql=true

I 'm running a mysql 5.7. The problem is that the table is created and id is marked as primary key. But Default is NULL and Extra ( where I expect autoincrement) is empty.

+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | bigint(20)   | NO   | PRI | NULL    |       |
+----------+--------------+------+-----+---------+-------+

Is there something I am missing or that is mittsonfigured?

FishingIsLife
  • 1,972
  • 3
  • 28
  • 51

1 Answers1

1

You mean you want to use auto-increment for the id field ? If yes , you have to specify the strategy of @GeneratedValue to be IDENTITY:

@Entity
public class Test implements Serializable {

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

}
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • Thank you, that worked. I tried .AUTO based on https://www.objectdb.com/java/jpa/entity/generated But that doesn't work whereas, as far as I understand, ids should be autogenerated for new objects. – FishingIsLife Aug 30 '19 at 06:49