├── .gitignore
├── README.md
├── pom.xml
└── src
└── main
└── java
└── com
└── learn
└── sdjpa
├── Application.java
├── DataProvider.java
├── dao
└── CustomerDao.java
├── domain
└── Customer.java
└── util
└── MyClass.java
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | build/
3 | .idea/
4 | .settings/
5 | .classpath
6 | .project
7 | *.iml
8 | *.class
9 | *.jar
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | README
2 | ======
3 |
4 | This README describes the spring-data-jpa-postgresql-json project.
5 |
6 |
7 | What is this?
8 | ---------------------------------------------------------
9 | This repo is copied from this: https://github.com/nzhong/spring-data-jpa-h2
10 |
11 | * A minimal command line java program, simple!
12 | * Java configuration, no XML
13 | * Spring Data / JPA, and PostgreSQL, with a JSONB column
14 | * StringJsonUserType is taken from this thread: http://stackoverflow.com/questions/15974474/mapping-postgresql-json-column-to-hibernate-value-type
15 | * So it's fairly simple to use a JSONB column in JPA.
16 |
17 |
18 | How to run?
19 | ---------------------------------------------------------
20 | * ```mvn clean package```
21 | * ```java -jar target/app.jar```
22 |
23 |
24 | Note: if you need to adapt the code to your own path, make sure that you need to change the line in DataProvider.java,
25 | ```lef.setPackagesToScan("com.learn.sdjpa.domain");```
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 | spring-data-jpa-postgresql-json
5 | spring-data-jpa-postgresql-json
6 | 1.0-SNAPSHOT
7 |
8 |
9 | UTF-8
10 | 1.8
11 | 1.8
12 | 1.8
13 | 1.8
14 | 1.8
15 |
16 |
17 |
18 |
19 | org.springframework.data
20 | spring-data-jpa
21 | 2.2.1.RELEASE
22 |
23 |
24 | org.hibernate
25 | hibernate-entitymanager
26 | 5.4.8.Final
27 |
28 |
29 | com.vladmihalcea
30 | hibernate-types-52
31 | 2.7.1
32 |
33 |
34 | javax.transaction
35 | jta
36 | 1.1
37 |
38 |
39 | org.postgresql
40 | postgresql
41 | 42.2.8
42 |
43 |
44 | com.fasterxml.jackson.core
45 | jackson-core
46 | 2.10.1
47 |
48 |
49 | com.fasterxml.jackson.core
50 | jackson-databind
51 | 2.10.1
52 |
53 |
54 |
55 |
56 |
57 |
58 | org.apache.maven.plugins
59 | maven-shade-plugin
60 | 2.4.2
61 |
62 | false
63 |
64 |
65 | *:*
66 |
67 |
68 |
69 |
70 |
71 |
72 | package
73 |
74 | shade
75 |
76 |
77 |
78 |
80 |
82 |
83 | com.learn.sdjpa.Application
84 |
85 |
86 |
87 | app
88 |
89 |
90 |
91 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/src/main/java/com/learn/sdjpa/Application.java:
--------------------------------------------------------------------------------
1 | package com.learn.sdjpa;
2 |
3 | import com.learn.sdjpa.dao.CustomerDao;
4 | import com.learn.sdjpa.domain.Customer;
5 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6 | import org.springframework.context.support.AbstractApplicationContext;
7 |
8 | import java.sql.Connection;
9 | import java.sql.Statement;
10 | import java.util.List;
11 | import org.springframework.jdbc.datasource.DriverManagerDataSource;
12 |
13 |
14 | public class Application {
15 |
16 | @SuppressWarnings("unused")
17 | private static void prepareTable() throws Exception {
18 | DriverManagerDataSource ds = new DriverManagerDataSource();
19 | ds.setDriverClassName("org.postgresql.Driver");
20 | ds.setUrl("jdbc:postgresql://localhost:5432/postgres");
21 | ds.setUsername("postgres");
22 | ds.setPassword("welcome");
23 | Connection conn = ds.getConnection();
24 | Statement stmt = conn.createStatement();
25 | stmt.executeUpdate( "CREATE TABLE customer ( firstName varchar(50), lastName varchar(50), test JSONB )" );
26 | stmt.close();
27 | conn.close();
28 | }
29 |
30 | public static void main(String[] args) throws Exception {
31 |
32 | //prepareTable();
33 |
34 | AbstractApplicationContext context = new AnnotationConfigApplicationContext(DataProvider.class);
35 |
36 | CustomerDao repository = context.getBean(CustomerDao.class);
37 |
38 | // save a couple of customers
39 | repository.save(new Customer("Jack", "Bauer"));
40 | repository.save(new Customer("Chloe", "O'Brian"));
41 | repository.save(new Customer("Kim", "Bauer"));
42 | repository.save(new Customer("David", "Palmer"));
43 | repository.save(new Customer("Michelle", "Dessler"));
44 |
45 | // fetch all customers
46 | Iterable customers = repository.findAll();
47 | System.out.println("Customers found with findAll():");
48 | System.out.println("-------------------------------");
49 | for (Customer customer : customers) {
50 | System.out.println(customer);
51 | }
52 | System.out.println();
53 |
54 | // fetch an individual customer by ID
55 | Customer customer = repository.findById(61L).orElse(null);
56 | System.out.println("Customer found with findOne(61L):");
57 | System.out.println("--------------------------------");
58 | System.out.println(customer);
59 | System.out.println();
60 |
61 | // fetch customers by last name
62 | List bauers = repository.findByLastName("Bauer");
63 | System.out.println("Customer found with findByLastName('Bauer'):");
64 | System.out.println("--------------------------------------------");
65 | for (Customer bauer : bauers) {
66 | System.out.println(bauer);
67 | }
68 |
69 | // fetch customers by JSONB's last name
70 | List bauers2 = repository.findByJsonbLastName("Bauer");
71 | System.out.println("Customer found with findByJsonbLastName('Bauer'):");
72 | System.out.println("--------------------------------------------");
73 | for (Customer bauer : bauers2) {
74 | System.out.println(bauer);
75 | }
76 |
77 |
78 | context.close();
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/main/java/com/learn/sdjpa/DataProvider.java:
--------------------------------------------------------------------------------
1 | package com.learn.sdjpa;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
6 | import org.springframework.orm.jpa.JpaTransactionManager;
7 | import org.springframework.orm.jpa.JpaVendorAdapter;
8 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
9 | import org.springframework.orm.jpa.vendor.Database;
10 | import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
11 | import org.springframework.transaction.PlatformTransactionManager;
12 |
13 | import javax.sql.DataSource;
14 | import org.springframework.jdbc.datasource.DriverManagerDataSource;
15 |
16 | @Configuration
17 | @EnableJpaRepositories
18 | public class DataProvider {
19 |
20 | @Bean
21 | public DataSource dataSource() throws Exception {
22 | DriverManagerDataSource driver = new DriverManagerDataSource();
23 | driver.setDriverClassName("org.postgresql.Driver");
24 | driver.setUrl("jdbc:postgresql://127.0.0.1:5432/postgres");
25 | driver.setUsername("postgres");
26 | driver.setPassword("welcome");
27 | return driver;
28 | }
29 |
30 | @Bean
31 | public LocalContainerEntityManagerFactoryBean entityManagerFactory(
32 | DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
33 | LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
34 | lef.setDataSource(dataSource);
35 | lef.setJpaVendorAdapter(jpaVendorAdapter);
36 | lef.setPackagesToScan("com.learn.sdjpa.domain");
37 | return lef;
38 | }
39 |
40 | @Bean
41 | public JpaVendorAdapter jpaVendorAdapter() {
42 | HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
43 | hibernateJpaVendorAdapter.setShowSql(true);
44 | hibernateJpaVendorAdapter.setGenerateDdl(true);
45 | hibernateJpaVendorAdapter.setDatabase(Database.POSTGRESQL);
46 | return hibernateJpaVendorAdapter;
47 | }
48 |
49 | @Bean
50 | public PlatformTransactionManager transactionManager() {
51 | return new JpaTransactionManager();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/com/learn/sdjpa/dao/CustomerDao.java:
--------------------------------------------------------------------------------
1 | package com.learn.sdjpa.dao;
2 |
3 | import com.learn.sdjpa.domain.Customer;
4 | import org.springframework.data.jpa.repository.Query;
5 | import org.springframework.data.repository.CrudRepository;
6 | import org.springframework.data.repository.query.Param;
7 |
8 | import java.util.List;
9 |
10 | public interface CustomerDao extends CrudRepository {
11 | List findByLastName(String lastName);
12 |
13 | @Query(value = "select c.* from customer c where c.test->>'myLast'=:LAST", nativeQuery = true)
14 | List findByJsonbLastName(@Param("LAST") String lastName);
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/learn/sdjpa/domain/Customer.java:
--------------------------------------------------------------------------------
1 | package com.learn.sdjpa.domain;
2 |
3 | import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
4 | import org.hibernate.annotations.Type;
5 | import org.hibernate.annotations.TypeDef;
6 |
7 | import com.learn.sdjpa.util.MyClass;
8 |
9 | import javax.persistence.*;
10 |
11 | @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
12 | @Entity
13 | @Table(name = "customer")
14 | public class Customer {
15 |
16 | @Id
17 | @GeneratedValue(strategy = GenerationType.SEQUENCE)
18 | private long id;
19 | private String firstName;
20 | private String lastName;
21 | @Type(type = "jsonb")
22 | @Column(columnDefinition = "jsonb")
23 | private MyClass test;
24 |
25 | public Customer() {
26 | super();
27 | }
28 |
29 | public Customer(String firstName, String lastName) {
30 | super();
31 | this.firstName = firstName;
32 | this.lastName = lastName;
33 | test = new MyClass(firstName,lastName);
34 | }
35 |
36 | public long getId() {
37 | return id;
38 | }
39 |
40 | public void setId(long id) {
41 | this.id = id;
42 | }
43 |
44 | public String getFirstName() {
45 | return firstName;
46 | }
47 |
48 | public void setFirstName(String firstName) {
49 | this.firstName = firstName;
50 | }
51 |
52 | public String getLastName() {
53 | return lastName;
54 | }
55 |
56 | public void setLastName(String lastName) {
57 | this.lastName = lastName;
58 | }
59 |
60 | @Override
61 | public String toString() {
62 | return "Customer{" +
63 | "id=" + id +
64 | ", firstName='" + firstName + '\'' +
65 | ", lastName='" + lastName + '\'' +
66 | ", test=" + test +
67 | '}';
68 | }
69 | }
70 |
71 |
--------------------------------------------------------------------------------
/src/main/java/com/learn/sdjpa/util/MyClass.java:
--------------------------------------------------------------------------------
1 | package com.learn.sdjpa.util;
2 |
3 | public class MyClass {
4 | private String myFirst;
5 | private String myLast;
6 |
7 | public MyClass() {
8 | }
9 | public MyClass(String a, String b) {
10 | myFirst=a;
11 | myLast=b;
12 | }
13 |
14 | public String getMyFirst() {
15 | return myFirst;
16 | }
17 |
18 | public void setMyFirst(String myFirst) {
19 | this.myFirst = myFirst;
20 | }
21 |
22 | public String getMyLast() {
23 | return myLast;
24 | }
25 |
26 | public void setMyLast(String myLast) {
27 | this.myLast = myLast;
28 | }
29 |
30 | @Override
31 | public String toString() {
32 | return "MyClass{" +
33 | "myFirst='" + myFirst + '\'' +
34 | ", myLast='" + myLast + '\'' +
35 | '}';
36 | }
37 | }
38 |
--------------------------------------------------------------------------------