├── .gitignore ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main ├── java │ └── io │ │ └── brant │ │ └── example │ │ └── postgres94 │ │ ├── SpringBootHibernate5Postgres94ExampleApplication.java │ │ ├── domain │ │ ├── UserTest.java │ │ ├── UserTestRepository.java │ │ └── UserTestService.java │ │ ├── json │ │ └── Info.java │ │ └── jsonb │ │ ├── dialect │ │ └── JSONBPostgreSQLDialect.java │ │ └── types │ │ └── JSONBUserType.java └── resources │ └── application.properties └── test └── java └── io └── brant └── example └── postgres94 └── SpringBootHibernate5Postgres94ExampleApplicationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | *.iml 3 | *.idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Spring-Boot based PostgreSQL 9.4 JSON Type Handle Example 2 | ======= 3 | 4 | ``` 5 | Spring Boot + SpringDataJPA + Hibernate + PostgreSQL 9.4 6 | ``` 7 | 8 | ###Description 9 | ``` 10 | SpringBoot based PostgreSQL JSON Type Handle Example (SpringDataJPA) 11 | ``` 12 | 13 | ###How to Open 14 | ``` 15 | IntelliJ -> Open -> Select Project Directory 16 | ``` 17 | 18 | ###Compile Setting 19 | ``` 20 | Open IntelliJ Preference 21 | - Build, Execution, Deployment -> Compiler 22 | -> Check 'Make project automatically' 23 | ``` 24 | 25 | ###How to Run 26 | ``` 27 | - Run Test Code ( SpringBootHibernate5Postgres94ExampleApplicationTests ) 28 | ``` 29 | 30 | ### Environment 31 | - Java 8 32 | - Spring Boot 1.3.2.RELEASE 33 | - Hibernate 5.0.2 34 | - PostgreSQL 9.4 35 | - Maven 2 -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # 58 | # Look for the Apple JDKs first to preserve the existing behaviour, and then look 59 | # for the new JDKs provided by Oracle. 60 | # 61 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then 62 | # 63 | # Apple JDKs 64 | # 65 | export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home 66 | fi 67 | 68 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then 69 | # 70 | # Apple JDKs 71 | # 72 | export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home 73 | fi 74 | 75 | if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then 76 | # 77 | # Oracle JDKs 78 | # 79 | export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home 80 | fi 81 | 82 | if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then 83 | # 84 | # Apple JDKs 85 | # 86 | export JAVA_HOME=`/usr/libexec/java_home` 87 | fi 88 | ;; 89 | esac 90 | 91 | if [ -z "$JAVA_HOME" ] ; then 92 | if [ -r /etc/gentoo-release ] ; then 93 | JAVA_HOME=`java-config --jre-home` 94 | fi 95 | fi 96 | 97 | if [ -z "$M2_HOME" ] ; then 98 | ## resolve links - $0 may be a link to maven's home 99 | PRG="$0" 100 | 101 | # need this for relative symlinks 102 | while [ -h "$PRG" ] ; do 103 | ls=`ls -ld "$PRG"` 104 | link=`expr "$ls" : '.*-> \(.*\)$'` 105 | if expr "$link" : '/.*' > /dev/null; then 106 | PRG="$link" 107 | else 108 | PRG="`dirname "$PRG"`/$link" 109 | fi 110 | done 111 | 112 | saveddir=`pwd` 113 | 114 | M2_HOME=`dirname "$PRG"`/.. 115 | 116 | # make it fully qualified 117 | M2_HOME=`cd "$M2_HOME" && pwd` 118 | 119 | cd "$saveddir" 120 | # echo Using m2 at $M2_HOME 121 | fi 122 | 123 | # For Cygwin, ensure paths are in UNIX format before anything is touched 124 | if $cygwin ; then 125 | [ -n "$M2_HOME" ] && 126 | M2_HOME=`cygpath --unix "$M2_HOME"` 127 | [ -n "$JAVA_HOME" ] && 128 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 129 | [ -n "$CLASSPATH" ] && 130 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 131 | fi 132 | 133 | # For Migwn, ensure paths are in UNIX format before anything is touched 134 | if $mingw ; then 135 | [ -n "$M2_HOME" ] && 136 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 137 | [ -n "$JAVA_HOME" ] && 138 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 139 | # TODO classpath? 140 | fi 141 | 142 | if [ -z "$JAVA_HOME" ]; then 143 | javaExecutable="`which javac`" 144 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 145 | # readlink(1) is not available as standard on Solaris 10. 146 | readLink=`which readlink` 147 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 148 | if $darwin ; then 149 | javaHome="`dirname \"$javaExecutable\"`" 150 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 151 | else 152 | javaExecutable="`readlink -f \"$javaExecutable\"`" 153 | fi 154 | javaHome="`dirname \"$javaExecutable\"`" 155 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 156 | JAVA_HOME="$javaHome" 157 | export JAVA_HOME 158 | fi 159 | fi 160 | fi 161 | 162 | if [ -z "$JAVACMD" ] ; then 163 | if [ -n "$JAVA_HOME" ] ; then 164 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 165 | # IBM's JDK on AIX uses strange locations for the executables 166 | JAVACMD="$JAVA_HOME/jre/sh/java" 167 | else 168 | JAVACMD="$JAVA_HOME/bin/java" 169 | fi 170 | else 171 | JAVACMD="`which java`" 172 | fi 173 | fi 174 | 175 | if [ ! -x "$JAVACMD" ] ; then 176 | echo "Error: JAVA_HOME is not defined correctly." >&2 177 | echo " We cannot execute $JAVACMD" >&2 178 | exit 1 179 | fi 180 | 181 | if [ -z "$JAVA_HOME" ] ; then 182 | echo "Warning: JAVA_HOME environment variable is not set." 183 | fi 184 | 185 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 186 | 187 | # For Cygwin, switch paths to Windows format before running java 188 | if $cygwin; then 189 | [ -n "$M2_HOME" ] && 190 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 191 | [ -n "$JAVA_HOME" ] && 192 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 193 | [ -n "$CLASSPATH" ] && 194 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 195 | fi 196 | 197 | # traverses directory structure from process work directory to filesystem root 198 | # first directory with .mvn subdirectory is considered project base directory 199 | find_maven_basedir() { 200 | local basedir=$(pwd) 201 | local wdir=$(pwd) 202 | while [ "$wdir" != '/' ] ; do 203 | if [ -d "$wdir"/.mvn ] ; then 204 | basedir=$wdir 205 | break 206 | fi 207 | wdir=$(cd "$wdir/.."; pwd) 208 | done 209 | echo "${basedir}" 210 | } 211 | 212 | # concatenates all lines of a file 213 | concat_lines() { 214 | if [ -f "$1" ]; then 215 | echo "$(tr -s '\n' ' ' < "$1")" 216 | fi 217 | } 218 | 219 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)} 220 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 221 | 222 | # Provide a "standardized" way to retrieve the CLI args that will 223 | # work with both Windows and non-Windows executions. 224 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 225 | export MAVEN_CMD_LINE_ARGS 226 | 227 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 228 | 229 | exec "$JAVACMD" \ 230 | $MAVEN_OPTS \ 231 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 232 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 233 | ${WRAPPER_LAUNCHER} "$@" 234 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | set MAVEN_CMD_LINE_ARGS=%* 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | 121 | set WRAPPER_JAR="".\.mvn\wrapper\maven-wrapper.jar"" 122 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 123 | 124 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CMD_LINE_ARGS% 125 | if ERRORLEVEL 1 goto error 126 | goto end 127 | 128 | :error 129 | set ERROR_CODE=1 130 | 131 | :end 132 | @endlocal & set ERROR_CODE=%ERROR_CODE% 133 | 134 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 135 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 136 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 137 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 138 | :skipRcPost 139 | 140 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 141 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 142 | 143 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 144 | 145 | exit /B %ERROR_CODE% -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | io.brant.example.postgres94 7 | postgres 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | springboot-hibernate5-postgres94-example 12 | 13 | 14 | org.springframework.boot 15 | spring-boot-starter-parent 16 | 1.3.1.RELEASE 17 | 18 | 19 | 20 | 21 | UTF-8 22 | 1.8 23 | 5.0.2.Final 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-data-jpa 30 | 31 | 32 | 33 | org.projectlombok 34 | lombok 35 | 1.16.6 36 | 37 | 38 | 39 | org.postgresql 40 | postgresql 41 | 9.4-1201-jdbc41 42 | compile 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-jooq 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-starter-test 53 | test 54 | 55 | 56 | 57 | com.fasterxml.jackson.core 58 | jackson-databind 59 | 2.6.3 60 | 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-devtools 65 | 66 | 67 | 68 | 69 | 70 | 71 | org.springframework.boot 72 | spring-boot-maven-plugin 73 | 74 | 75 | org.apache.maven.plugins 76 | maven-compiler-plugin 77 | 78 | 1.8 79 | 1.8 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | spring-snapshots 88 | Spring Snapshots 89 | https://repo.spring.io/snapshot 90 | 91 | true 92 | 93 | 94 | 95 | 96 | spring-milestones 97 | Spring Milestones 98 | https://repo.spring.io/milestone 99 | 100 | false 101 | 102 | 103 | 104 | 105 | jcenter-snapshots 106 | jcenter 107 | http://oss.jfrog.org/artifactory/oss-snapshot-local/ 108 | 109 | 110 | 111 | sonatype-snapshots 112 | sonatype-snapshots 113 | https://oss.sonatype.org/content/repositories/snapshots/ 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | spring-snapshots 122 | Spring Snapshots 123 | https://repo.spring.io/snapshot 124 | 125 | true 126 | 127 | 128 | 129 | 130 | spring-milestones 131 | Spring Milestones 132 | https://repo.spring.io/milestone 133 | 134 | false 135 | 136 | 137 | 138 | 139 | 140 | 141 | -------------------------------------------------------------------------------- /src/main/java/io/brant/example/postgres94/SpringBootHibernate5Postgres94ExampleApplication.java: -------------------------------------------------------------------------------- 1 | package io.brant.example.postgres94; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.transaction.annotation.EnableTransactionManagement; 6 | 7 | @SpringBootApplication 8 | @EnableTransactionManagement(proxyTargetClass = true) 9 | public class SpringBootHibernate5Postgres94ExampleApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBootHibernate5Postgres94ExampleApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/io/brant/example/postgres94/domain/UserTest.java: -------------------------------------------------------------------------------- 1 | package io.brant.example.postgres94.domain; 2 | 3 | import io.brant.example.postgres94.json.Info; 4 | import io.brant.example.postgres94.jsonb.types.JSONBUserType; 5 | import org.hibernate.annotations.Parameter; 6 | import org.hibernate.annotations.Type; 7 | import org.hibernate.annotations.TypeDef; 8 | 9 | import javax.persistence.Entity; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.Id; 12 | import javax.persistence.Table; 13 | 14 | @Entity 15 | @Table(name = "user_test") 16 | @TypeDef(name = "jsonb", typeClass = JSONBUserType.class, parameters = { 17 | @Parameter(name = JSONBUserType.CLASS, value = "io.brant.example.postgres94.json.Info")}) 18 | public class UserTest { 19 | 20 | @Id 21 | @GeneratedValue 22 | private Long id; 23 | 24 | private String test; 25 | 26 | @Type(type = "jsonb") 27 | private Info info; 28 | 29 | public Long getId() { 30 | return id; 31 | } 32 | 33 | public void setId(Long id) { 34 | this.id = id; 35 | } 36 | 37 | public String getTest() { 38 | return test; 39 | } 40 | 41 | public void setTest(String test) { 42 | this.test = test; 43 | } 44 | 45 | public Info getInfo() { 46 | return info; 47 | } 48 | 49 | public void setInfo(Info info) { 50 | this.info = info; 51 | } 52 | 53 | @Override 54 | public boolean equals(Object o) { 55 | 56 | if (this == o) return true; 57 | if (o == null || getClass() != o.getClass()) return false; 58 | 59 | UserTest userTest = (UserTest) o; 60 | 61 | if (id != null ? !id.equals(userTest.id) : userTest.id != null) return false; 62 | if (test != null ? !test.equals(userTest.test) : userTest.test != null) return false; 63 | if (info != null ? !info.equals(userTest.info) : userTest.info != null) return false; 64 | 65 | return true; 66 | } 67 | 68 | @Override 69 | public int hashCode() { 70 | int result = id != null ? id.hashCode() : 0; 71 | result = 31 * result + (test != null ? test.hashCode() : 0); 72 | result = 31 * result + (info != null ? info.hashCode() : 0); 73 | return result; 74 | } 75 | 76 | @Override 77 | public String toString() { 78 | return "UserTest{" + 79 | "id=" + id + 80 | ", test='" + test + '\'' + 81 | ", info=" + info + 82 | '}'; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/io/brant/example/postgres94/domain/UserTestRepository.java: -------------------------------------------------------------------------------- 1 | package io.brant.example.postgres94.domain; 2 | 3 | import io.brant.example.postgres94.json.Info; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.List; 8 | 9 | @Repository 10 | public interface UserTestRepository extends JpaRepository { 11 | 12 | List findByInfo(Info info); 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/io/brant/example/postgres94/domain/UserTestService.java: -------------------------------------------------------------------------------- 1 | package io.brant.example.postgres94.domain; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import org.springframework.transaction.annotation.Transactional; 6 | 7 | @Service 8 | public class UserTestService { 9 | 10 | @Autowired 11 | private UserTestRepository testRepository; 12 | 13 | @Transactional 14 | public void save(UserTest test) { 15 | testRepository.save(test); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/io/brant/example/postgres94/json/Info.java: -------------------------------------------------------------------------------- 1 | package io.brant.example.postgres94.json; 2 | 3 | public class Info { 4 | 5 | private String address; 6 | 7 | private int age; 8 | 9 | private String educations; 10 | 11 | public String getAddress() { 12 | return address; 13 | } 14 | 15 | public void setAddress(String address) { 16 | this.address = address; 17 | } 18 | 19 | public int getAge() { 20 | return age; 21 | } 22 | 23 | public void setAge(int age) { 24 | this.age = age; 25 | } 26 | 27 | public String getEducations() { 28 | return educations; 29 | } 30 | 31 | public void setEducations(String educations) { 32 | this.educations = educations; 33 | } 34 | 35 | @Override 36 | public boolean equals(Object o) { 37 | if (this == o) return true; 38 | if (o == null || getClass() != o.getClass()) return false; 39 | 40 | Info info = (Info) o; 41 | 42 | if (age != info.age) return false; 43 | if (address != null ? !address.equals(info.address) : info.address != null) return false; 44 | return !(educations != null ? !educations.equals(info.educations) : info.educations != null); 45 | 46 | } 47 | 48 | @Override 49 | public int hashCode() { 50 | int result = address != null ? address.hashCode() : 0; 51 | result = 31 * result + age; 52 | result = 31 * result + (educations != null ? educations.hashCode() : 0); 53 | return result; 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "Info{" + 59 | "address='" + address + '\'' + 60 | ", age=" + age + 61 | ", educations='" + educations + '\'' + 62 | '}'; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/io/brant/example/postgres94/jsonb/dialect/JSONBPostgreSQLDialect.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | *

7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package io.brant.example.postgres94.jsonb.dialect; 14 | 15 | import io.brant.example.postgres94.jsonb.types.JSONBUserType; 16 | import org.hibernate.dialect.PostgreSQL94Dialect; 17 | 18 | import java.sql.Types; 19 | 20 | public class JSONBPostgreSQLDialect extends PostgreSQL94Dialect { 21 | 22 | public JSONBPostgreSQLDialect() { 23 | super(); 24 | registerColumnType(Types.JAVA_OBJECT, JSONBUserType.JSONB_TYPE); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/io/brant/example/postgres94/jsonb/types/JSONBUserType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | *

7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package io.brant.example.postgres94.jsonb.types; 14 | 15 | import com.fasterxml.jackson.core.JsonProcessingException; 16 | import com.fasterxml.jackson.databind.ObjectMapper; 17 | import org.hibernate.HibernateException; 18 | import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl; 19 | import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; 20 | import org.hibernate.engine.spi.SessionImplementor; 21 | import org.hibernate.type.SerializationException; 22 | import org.hibernate.usertype.ParameterizedType; 23 | import org.hibernate.usertype.UserType; 24 | import org.postgresql.util.PGobject; 25 | 26 | import java.io.IOException; 27 | import java.io.Serializable; 28 | import java.sql.PreparedStatement; 29 | import java.sql.ResultSet; 30 | import java.sql.SQLException; 31 | import java.sql.Types; 32 | import java.util.*; 33 | import java.util.stream.Collectors; 34 | 35 | public class JSONBUserType implements ParameterizedType, UserType { 36 | 37 | private static final ObjectMapper objectMapper = new ObjectMapper(); 38 | private static final ClassLoaderService classLoaderService = new ClassLoaderServiceImpl(); 39 | 40 | public static final String JSONB_TYPE = "jsonb"; 41 | public static final String CLASS = "CLASS"; 42 | 43 | private Class jsonClassType; 44 | 45 | @Override 46 | public Class returnedClass() { 47 | return Object.class; 48 | } 49 | 50 | @Override 51 | public int[] sqlTypes() { 52 | return new int[]{Types.JAVA_OBJECT}; 53 | } 54 | 55 | @Override 56 | public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { 57 | try { 58 | final String json = resultSet.getString(names[0]); 59 | return json == null ? null : objectMapper.readValue(json, jsonClassType); 60 | } catch (IOException e) { 61 | throw new HibernateException(e); 62 | } 63 | } 64 | 65 | @Override 66 | public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { 67 | try { 68 | final String json = value == null ? null : objectMapper.writeValueAsString(value); 69 | PGobject pgo = new PGobject(); 70 | pgo.setType(JSONB_TYPE); 71 | pgo.setValue(json); 72 | st.setObject(index, pgo); 73 | } catch (JsonProcessingException e) { 74 | throw new HibernateException(e); 75 | } 76 | } 77 | 78 | @Override 79 | public void setParameterValues(Properties parameters) { 80 | final String clazz = (String) parameters.get(CLASS); 81 | jsonClassType = classLoaderService.classForName(clazz); 82 | } 83 | 84 | @SuppressWarnings("unchecked") 85 | @Override 86 | public Object deepCopy(Object value) throws HibernateException { 87 | 88 | if (!(value instanceof Collection)) { 89 | return value; 90 | } 91 | 92 | Collection collection = (Collection) value; 93 | Collection collectionClone = CollectionFactory.newInstance(collection.getClass()); 94 | 95 | collectionClone.addAll(collection.stream().map(this::deepCopy).collect(Collectors.toList())); 96 | 97 | return collectionClone; 98 | } 99 | 100 | static final class CollectionFactory { 101 | @SuppressWarnings("unchecked") 102 | static > T newInstance(Class collectionClass) { 103 | if (List.class.isAssignableFrom(collectionClass)) { 104 | return (T) new ArrayList(); 105 | } else if (Set.class.isAssignableFrom(collectionClass)) { 106 | return (T) new HashSet(); 107 | } else { 108 | throw new IllegalArgumentException("Unsupported collection type : " + collectionClass); 109 | } 110 | } 111 | } 112 | 113 | @Override 114 | public boolean isMutable() { 115 | return true; 116 | } 117 | 118 | @Override 119 | public boolean equals(Object x, Object y) throws HibernateException { 120 | if (x == y) { 121 | return true; 122 | } 123 | 124 | if ((x == null) || (y == null)) { 125 | return false; 126 | } 127 | 128 | return x.equals(y); 129 | } 130 | 131 | @Override 132 | public int hashCode(Object x) throws HibernateException { 133 | assert (x != null); 134 | return x.hashCode(); 135 | } 136 | 137 | @Override 138 | public Object assemble(Serializable cached, Object owner) throws HibernateException { 139 | return deepCopy(cached); 140 | } 141 | 142 | @Override 143 | public Serializable disassemble(Object value) throws HibernateException { 144 | Object deepCopy = deepCopy(value); 145 | 146 | if (!(deepCopy instanceof Serializable)) { 147 | throw new SerializationException(String.format("%s is not serializable class", value), null); 148 | } 149 | 150 | return (Serializable) deepCopy; 151 | } 152 | 153 | @Override 154 | public Object replace(Object original, Object target, Object owner) throws HibernateException { 155 | return deepCopy(original); 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:postgresql://10.211.55.8:5432/test 2 | spring.datasource.username=test 3 | spring.datasource.password=test 4 | spring.datasource.driver-class-name=org.postgresql.Driver 5 | 6 | spring.jpa.database-platform=io.brant.example.postgres94.jsonb.dialect.JSONBPostgreSQLDialect 7 | spring.jpa.show-sql=true 8 | spring.jpa.hibernate.ddl-auto=update -------------------------------------------------------------------------------- /src/test/java/io/brant/example/postgres94/SpringBootHibernate5Postgres94ExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package io.brant.example.postgres94; 2 | 3 | import io.brant.example.postgres94.domain.UserTest; 4 | import io.brant.example.postgres94.domain.UserTestRepository; 5 | import io.brant.example.postgres94.domain.UserTestService; 6 | import io.brant.example.postgres94.json.Info; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.SpringApplicationConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | import java.util.List; 14 | 15 | @RunWith(SpringJUnit4ClassRunner.class) 16 | @SpringApplicationConfiguration(classes = SpringBootHibernate5Postgres94ExampleApplication.class) 17 | public class SpringBootHibernate5Postgres94ExampleApplicationTests { 18 | 19 | @Autowired 20 | private UserTestService testService; 21 | 22 | @Autowired 23 | private UserTestRepository userTestRepository; 24 | 25 | @Test 26 | public void saveTest() { 27 | UserTest userTest = new UserTest(); 28 | 29 | Info info = new Info(); 30 | info.setAddress("Seoul"); 31 | info.setAge(27); 32 | info.setEducations("CS"); 33 | 34 | userTest.setTest("testValue"); 35 | userTest.setInfo(info); 36 | 37 | testService.save(userTest); 38 | } 39 | 40 | @Test 41 | public void findTest() { 42 | Info info = new Info(); 43 | info.setAddress("Seoul"); 44 | info.setAge(27); 45 | info.setEducations("CS"); 46 | 47 | List userTestList = userTestRepository.findByInfo(info); 48 | 49 | userTestList.stream().forEach(System.out::println); 50 | } 51 | } 52 | --------------------------------------------------------------------------------