-
-
Save rgordeev/1267864011e6d1383c8c48e0070a97e2 to your computer and use it in GitHub Desktop.
Spring Boot JPA example with custom Table based Identifier generation with PostgreSQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
import org.hibernate.annotations.GenericGenerator; | |
import org.hibernate.annotations.Parameter; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
@SpringBootApplication | |
public class App { | |
public static void main(String[] args) { | |
CustomerRepository customerRepository = SpringApplication.run(App.class, args) | |
.getBean(CustomerRepository.class); | |
Customer customer = new Customer(); | |
customer.setName("customer" + System.currentTimeMillis()); | |
customerRepository.save(customer); | |
} | |
} | |
interface CustomerRepository extends JpaRepository<Customer, Long> { | |
} | |
@Entity | |
@Table(name = "customer") | |
class Customer { | |
@Id | |
@GeneratedValue(generator = "pooled") | |
@GenericGenerator(name = "pooled", strategy = "enhanced-table", parameters = { | |
@Parameter(name = "value_column_name", value = "sequence_next_hi_value"), | |
@Parameter(name = "prefer_entity_table_as_segment_value", value = "true"), | |
@Parameter(name = "optimizer", value = "pooled-lo"), @Parameter(name = "increment_size", value = "100") }) | |
Long id; | |
String name; | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring: | |
datasource: | |
url: jdbc:postgresql://localhost/training | |
username: training | |
password: training | |
platform: postgresql | |
jpa: | |
generate-ddl: true | |
hibernate: | |
use-new-id-generator-mappings: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>de.tdlabs.training</groupId> | |
<artifactId>spring-boot-jpa-hibernate-postgres</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>spring-boot-jpa-hibernate-postgres</name> | |
<description>Demo project for Spring Boot</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.4.1.RELEASE</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-jpa</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.postgresql</groupId> | |
<artifactId>postgresql</artifactId> | |
<scope>runtime</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment