Home > AI > Backend > SpringBoot > mysql-connector-java >

@GeneratedValue

AUTO

Will have table named hibernate_sequence to record current id number which will be used by all entities in this database.

Increase by 1

Example 1

@Entity
@Table(name = "STUDENTS")
class Student {

    @Id
    @GeneratedValue
    private long studentId;

    private String name;
    Student(String name){
        this.name = name;
    }

}



interface StudentRepository extends JpaRepository<Student, Long> { }

@RestController
class StudentController {
    @Autowired
    StudentRepository studentRepository;

    @GetMapping("/saveStudent")
    public Student save() {
        Random r = new Random();
        Student s = new Student(String.valueOf(r.nextInt()));
        return studentRepository.save(s);
    }
}

Example 2: UUID

@Entity
@Table(name = "STUDENTS")
class Student {

    @Id
    @GeneratedValue
    private UUID studentId;

    private String name;
    Student(String name){
        this.name = name;
    }

}



interface StudentRepository extends JpaRepository<Student, Long> { }

@RestController
class StudentController {
    @Autowired
    StudentRepository studentRepository;

    @GetMapping("/saveStudent")
    public Student save() {
        Random r = new Random();
        Student s = new Student(String.valueOf(r.nextInt()));
        return studentRepository.save(s);
    }
}

Disadvantage, not good, the id is too long.

If you change from original long field to UUID, you need to delete the table otherwise it will have error

IDENTITY

@Entity
@Table(name = "STUDENTS")
class Student {

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

    private String name;
    Student(String name){
        this.name = name;
    }

}



interface StudentRepository extends JpaRepository<Student, Long> { }

@RestController
class StudentController {
    @Autowired
    StudentRepository studentRepository;

    @GetMapping("/saveStudent")
    public Student save() {
        Random r = new Random();
        Student s = new Student(String.valueOf(r.nextInt()));
        return studentRepository.save(s);
    }
}

Result, will always start from 1 if no record. Otherwise, it will continue the current largest number. Good. I like this.

SEQUENCE

@Entity
@Table(name = "STUDENTS")
class Student {

    @Id
    @GeneratedValue(generator = "sequence-generator")
    @GenericGenerator(
            name = "sequence-generator",
            strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
            parameters = {
                    @Parameter(name = "sequence_name", value = "STUDENTS_SEQ"),
                    @Parameter(name = "initial_value", value = "4"),
                    @Parameter(name = "increment_size", value = "1")
            }
    )
    private long studentId;

    private String name;
    Student(String name){
        this.name = name;
    }

}



interface StudentRepository extends JpaRepository<Student, Long> { }

@RestController
class StudentController {
    @Autowired
    StudentRepository studentRepository;

    @GetMapping("/saveStudent")
    public Student save() {
        Random r = new Random();
        Student s = new Student(String.valueOf(r.nextInt()));
        return studentRepository.save(s);
    }
}

TABLE

@Entity
@Table(name = "STUDENTS")
class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE,
            generator = "table-generator")
    @TableGenerator(name = "table-generator",
            table = "dep_ids",
            pkColumnName = "seq_id",
            valueColumnName = "seq_value")
    private long studentId;

    private String name;
    Student(String name){
        this.name = name;
    }

}



interface StudentRepository extends JpaRepository<Student, Long> { }

@RestController
class StudentController {
    @Autowired
    StudentRepository studentRepository;

    @GetMapping("/saveStudent")
    public Student save() {
        Random r = new Random();
        Student s = new Student(String.valueOf(r.nextInt()));
        return studentRepository.save(s);
    }
}

Inclusin, the IDENTITY is recommended for conveniencet

Leave a Reply