Using JPA to create a primary key increment table in the PostgreSQL database

Source: Internet
Author: User
Tags generator postgresql

    1. JPA Dependency
<!-- -->
    <!-- -->
    1. Domain class
Package com.hikvison.test.pgtest.entity;


Import javax.persistence.*;

Import lombok.Data;

  * Test some performance of the pg database
  * 1. Primary key increment
  * 2. High concurrent lock mechanism
  * @dateAugust 28, 2018 7:23:17 PM
Public class TestEntity implements Serializable {

     Private static final long serialVersionUID = 2672553622864930471L;
     @SequenceGenerator(sequenceName="test_sequence", name="abc" )
     Private Integer id;
     Private String name;
     Private Integer version;
    1. Repository class
package com.hikvison.test.pgtest.repository;


import com.hikvison.test.pgtest.entity.TestEntity;

public interface TestRepository extends JpaRepository<TestEntity, Long> {

    1. Controller class
Package com.hikvison.test.pgtest.controller;

Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import org.springframework.web.bind.annotation.ResponseBody;

Import com.hikvison.test.pgtest.entity.TestEntity;
Import com.hikvison.test.pgtest.repository.TestRepository;

  * Test contro
  * @date August 28, 2018 7:35:25 PM

Public class TeatController {
     TestRepository r ;
     Public String test1(){
         Return "hello";
     Public String test2(){
         TestEntity te = new TestEntity();
         Return "success";
Add database link information, database driver, Spring boot dependency, start running. Implement primary key self-increment parsing:
    1. In the domain class, you use the
    @SequenceGenerator(sequenceName="test_sequence", name="abc" )
    private Integer id;

1) test_sequence: The sequence name in the database, if it does not exist, will be created with an initial value of 1 and a step of 1 (PostgreSQL and Oracle, the dependent sequence implements the self-increment of the primary key)
2) @SequenceGenerator, note the use of this annotation to declare a sequence.

There is a problem:
I tested the table primary key number starting with 50, not figuring out why.

Using JPA to create a primary key increment table in the PostgreSQL database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.