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

Source: Internet
Author: User
Tags generator postgresql


    1. JPA Dependency
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
    </dependency>
    1. Domain class
Package com.hikvison.test.pgtest.entity;

Import java.io.Serializable;

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
  */
@Data
@Entity
@Table(name="test_pg_wushan")
Public class TestEntity implements Serializable {

     Private static final long serialVersionUID = 2672553622864930471L;
     @Id
     @SequenceGenerator(sequenceName="test_sequence", name="abc" )
     @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="abc")
     @Column(name="id")
     Private Integer id;
     @Column(name="test_name")
    
     Private String name;
     @Transient
     Private Integer version;
}
    1. Repository class
package com.hikvison.test.pgtest.repository;

import org.springframework.data.jpa.repository.JpaRepository;

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
  */

@Controller
Public class TeatController {
     @Autowired
     TestRepository r ;
    
     @RequestMapping("/")
     @ResponseBody
     Public String test1(){
         Return "hello";
     }
    
     @RequestMapping("/save")
     @ResponseBody
     Public String test2(){
         TestEntity te = new TestEntity();
         te.setName(System.currentTimeMillis()+"");
         R.save(te);
         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
@Id
    @SequenceGenerator(sequenceName="test_sequence", name="abc" )
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="abc")
    @Column(name="id")
    private Integer id;


which
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: info-contact@alibabacloud.com 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.