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

Source: Internet
Author: User

Tags: ext database link Long persist generated link static boot boot

    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;/** * 测试pg数据库的一些性能 * 1. 主键自增 * 2. 高并发锁机制 *  * @date 2018年8月28日 下午7:23:17 */@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;/** * 测试用contro *  * @date 2018年8月28日 下午7:35:25 */@Controllerpublic 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.

Tags Index: