├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── pom.xml └── src ├── main └── java │ └── com │ └── github │ └── thealchemist │ └── pg_hibernate │ ├── BoxType.java │ ├── CircleType.java │ ├── HstoreType.java │ ├── InetAddressType.java │ ├── IntegerArrayType.java │ ├── LineSegmentType.java │ ├── PointType.java │ ├── PolygonType.java │ ├── StringArrayType.java │ ├── XMLType.java │ └── types │ ├── Box.java │ ├── Circle.java │ ├── LineSegment.java │ ├── Point.java │ └── Polygon.java └── test ├── java └── com │ └── github │ └── thealchemist │ └── pg_hibernate │ ├── BoxTypeTest.java │ ├── CircleTypeTest.java │ ├── HibernateTest.java │ ├── HstoreTypeTest.java │ ├── InetAddressTypeTest.java │ ├── IntegerArrayTypeTest.java │ ├── LineSegmentTypeTest.java │ ├── PointTypeTest.java │ ├── PolygonTypeTest.java │ ├── StringArrayTypeTest.java │ └── spring │ ├── BoxTypeEntity.java │ ├── CircleTypeEntity.java │ ├── HstoreTypeEntity.java │ ├── InetAddressEntity.java │ ├── IntegerArrayEntity.java │ ├── LineSegmentTypeEntity.java │ ├── Marker.java │ ├── PersistenceJPAConfig.java │ ├── PointTypeEntity.java │ ├── PolygonTypeEntity.java │ ├── StringArrayEntity.java │ └── package-info.java └── resources ├── com └── github │ └── thealchemist │ └── pg_hibernate │ ├── BoxTypeTest.testGet.sql │ ├── CircleTypeTest.testGet.sql │ ├── HstoreTypeTest.testGet.sql │ ├── HstoreTypeTest.testGetWithNullInMap.sql │ ├── HstoreTypeTest.testSetWithNewMap.sql │ ├── InetAddressTypeTest.testGet.sql │ ├── InetAddressTypeTest.testGetIpv6.sql │ ├── IntArrayTypeTest.sql │ ├── IntArrayTypeTest.testGet.sql │ ├── IntArrayTypeTest.testSetViaSql.sql │ ├── IntegerArrayTypeTest.sql │ ├── IntegerArrayTypeTest.testGet.sql │ ├── IntegerArrayTypeTest.testSetViaSql.sql │ ├── LineSegmentTypeTest.testGet.sql │ ├── LineSegmentTypeTest.testGetAlternateSyntax.sql │ ├── PointTypeTest.testGet.sql │ ├── PolygonTypeTest.testGet.sql │ └── StringArrayTypeTest.testGet.sql ├── database.properties ├── logback.xml └── schema.sql /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | pom.xml.tag 3 | pom.xml.releaseBackup 4 | pom.xml.versionsBackup 5 | pom.xml.next 6 | release.properties 7 | /.classpath 8 | /.project 9 | /.settings/ 10 | /.factorypath 11 | /.idea 12 | *.iml 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - openjdk8 4 | addons: 5 | postgresql: "9.4" 6 | before_install: 7 | - psql -c 'create database pg_hibernate;' -U postgres 8 | - psql -U postgres -c 'create extension hstore' pg_hibernate 9 | - psql -U postgres -c 'GRANT ALL PRIVILEGES ON DATABASE pg_hibernate to postgres' pg_hibernate 10 | install: 11 | true 12 | script: 13 | mvn verify 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | hibernate-postgresql 2 | ==================== 3 | 4 | Extra type mapping for PostgreSQL-specific types such as hstore and inet for Hibernate. 5 | 6 | I haven't written most of the code: I've just scoured the web for code that's already been written and tried to put it in one place. 7 | 8 | Sources include: 9 | * https://github.com/jamesward/spring_hibernate_hstore_demo 10 | * https://hibernate.atlassian.net/browse/HB-450 11 | * https://github.com/Canadensys/canadensys-data-access 12 | 13 | ## Deprecation Notice 14 | 15 | This project has been superceded by other projects such as https://github.com/vladmihalcea/hypersistence-utils. 16 | 17 | ## How To Use 18 | #### Java Source 19 | ```java 20 | @TypeDefs(value={ 21 | @TypeDef(name = "hstore", typeClass = com.github.thealchemist.pg_hibernate.HstoreType.class), 22 | @TypeDef(name = "inet", typeClass = com.github.thealchemist.pg_hibernate.InetAddressType.class) 23 | }) 24 | public class LoggedAction implements Serializable { 25 | @Type(type = "hstore") 26 | @Column(name="row_data", columnDefinition="hstore") 27 | private Map rowData; 28 | 29 | @Column(name="client_addr", columnDefinition="inet") 30 | @Type(type="inet") 31 | private InetAddress clientAddr; 32 | ``` 33 | 34 | #### Maven Configuration (pom.xml) 35 | 36 | ```xml 37 | 38 | 39 | com.github.the-alchemist 40 | hibernate-postgresql 41 | 1.0.18 42 | 43 | ``` 44 | #### Hibernate 5 Support 45 | Hibernate 5 is supported starting with version 1.0.16 46 | 47 | ## Full List of Supported Types 48 | * box 49 | * circle 50 | * hstore 51 | * inet 52 | * point 53 | * lineseg 54 | * polygon 55 | * string[] 56 | * intarray 57 | * xml 58 | 59 | ## Contributions Welcome! 60 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | com.github.the-alchemist 6 | hibernate-postgresql 7 | jar 8 | 1.0.22-SNAPSHOT 9 | 10 | Hibernate type mapping for PostgreSQL types 11 | 12 | A collection of Hibernate type mappings for PostgreSQL-specific types such as hstore, circle, polygon, and more! 13 | 14 | https://github.com/The-Alchemist/hibernate-postgresql 15 | 2014 16 | 17 | 18 | 3.3.0 19 | 20 | 21 | 22 | 23 | Apache License, Version 2.0 24 | http://opensource.org/licenses/Apache-2.0 25 | 26 | 27 | 28 | 29 | 30 | The Alchemist 31 | kap4020@gmail.com 32 | http://frightanic.com 33 | America/New_York 34 | 35 | developer 36 | 37 | 38 | 39 | 40 | 41 | 42 | ossrh 43 | Maven Central Snapshot Repository 44 | https://oss.sonatype.org/content/repositories/snapshots 45 | 46 | 47 | ossrh 48 | Maven Central Staging Repository 49 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 50 | 51 | 52 | 53 | 54 | Travis CI 55 | https://travis-ci.org/The-Alchemist/hibernate-postgresql 56 | 57 | 58 | github 59 | https://github.com/The-Alchemist/hibernate-postgresql/issues 60 | 61 | 62 | https://github.com/The-Alchemist/hibernate-postgresql 63 | scm:git:git@github.com:The-Alchemist/hibernate-postgresql.git 64 | scm:git:git@github.com:The-Alchemist/hibernate-postgresql.git 65 | HEAD 66 | 67 | 68 | 69 | 73 | 74 | 75 | -Xdoclint:none 76 | UTF-8 77 | UTF-8 78 | 79 | 1.7 80 | 1.7 81 | 82 | 83 | 3.2 84 | 1.5 85 | 2.10.1 86 | 2.7 87 | 2.5.1 88 | 2.4 89 | 90 | 91 | 1.1.0.Final 92 | 3.3.2 93 | 1.2 94 | 2.4 95 | 19.0 96 | 1.3 97 | 5.1.1.Final 98 | 2.4.4 99 | 4.12 100 | 1.14.8 101 | 1.9.5 102 | 0.9.9 103 | 3.1.0 104 | 1.7.7 105 | 5.2.7.Final 106 | 1.1.2 107 | 1 108 | 9.3-1102-jdbc41 109 | 110 | 111 | 112 | 113 | 114 | 115 | org.apache.maven.plugins 116 | maven-compiler-plugin 117 | ${maven-compiler-plugin.version} 118 | 119 | 120 | org.apache.maven.plugins 121 | maven-javadoc-plugin 122 | ${maven-javadoc-plugin.version} 123 | 124 | 125 | org.apache.maven.plugins 126 | maven-resources-plugin 127 | ${maven-resources-plugin.version} 128 | 129 | 130 | org.apache.maven.plugins 131 | maven-source-plugin 132 | ${maven-source-plugin.version} 133 | 134 | 135 | org.apache.maven.plugins 136 | maven-gpg-plugin 137 | ${maven-gpg-plugin.version} 138 | 139 | 140 | org.apache.maven.plugins 141 | maven-release-plugin 142 | ${maven-release-plugin.version} 143 | 144 | 145 | 146 | 147 | 148 | 149 | maven-compiler-plugin 150 | 151 | ${source.version} 152 | ${target.version} 153 | 154 | 155 | 156 | org.apache.maven.plugins 157 | maven-release-plugin 158 | 159 | true 160 | false 161 | release 162 | deploy 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | release 171 | 172 | 173 | 174 | org.apache.maven.plugins 175 | maven-source-plugin 176 | 177 | 178 | attach-sources 179 | 180 | jar-no-fork 181 | 182 | 183 | 184 | 185 | 186 | org.apache.maven.plugins 187 | maven-javadoc-plugin 188 | 189 | 190 | attach-javadocs 191 | 192 | jar 193 | 194 | 195 | 196 | 197 | 198 | org.apache.maven.plugins 199 | maven-gpg-plugin 200 | 201 | 202 | sign-artifacts 203 | verify 204 | 205 | sign 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | org.springframework 220 | spring-framework-bom 221 | 4.3.29.RELEASE 222 | pom 223 | import 224 | 225 | 226 | org.hibernate 227 | hibernate-core 228 | ${hibernate-core.version} 229 | provided 230 | 231 | 232 | org.springframework.data 233 | spring-data-jpa 234 | 1.11.0.RELEASE 235 | test 236 | 237 | 238 | org.postgresql 239 | postgresql 240 | ${postgresql.version} 241 | true 242 | 243 | 244 | junit 245 | junit 246 | ${junit.version} 247 | 248 | 249 | com.google.guava 250 | guava 251 | ${guava.version} 252 | 253 | 254 | javax.inject 255 | javax.inject 256 | ${javax.inject.version} 257 | 258 | 259 | ch.qos.logback 260 | logback-classic 261 | ${logback.version} 262 | test 263 | 264 | 265 | org.hibernate 266 | hibernate-entitymanager 267 | ${hibernate-core.version} 268 | provided 269 | 270 | 271 | org.hibernate 272 | hibernate-jpamodelgen 273 | ${hibernate-core.version} 274 | provided 275 | 276 | 277 | org.hibernate 278 | hibernate-validator-annotation-processor 279 | 5.1.3.Final 280 | 281 | 282 | org.hamcrest 283 | hamcrest-library 284 | ${hamcrest.version} 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | org.hibernate 293 | hibernate-core 294 | 295 | 296 | org.springframework.data 297 | spring-data-jpa 298 | test 299 | 300 | 301 | org.springframework 302 | spring-orm 303 | test 304 | 305 | 306 | org.postgresql 307 | postgresql 308 | 309 | 310 | junit 311 | junit 312 | test 313 | 314 | 315 | com.google.guava 316 | guava 317 | 318 | 319 | javax.inject 320 | javax.inject 321 | 322 | 323 | org.springframework 324 | spring-test 325 | test 326 | 327 | 328 | ch.qos.logback 329 | logback-classic 330 | test 331 | 332 | 333 | org.hibernate 334 | hibernate-entitymanager 335 | 336 | 337 | org.hibernate 338 | hibernate-jpamodelgen 339 | provided 340 | 341 | 342 | org.hibernate 343 | hibernate-validator-annotation-processor 344 | provided 345 | 346 | 347 | org.hamcrest 348 | hamcrest-library 349 | test 350 | 351 | 352 | 353 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/BoxType.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import java.io.Serializable; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | import org.hibernate.HibernateException; 9 | import org.hibernate.engine.spi.SharedSessionContractImplementor; 10 | import org.hibernate.usertype.UserType; 11 | import org.postgresql.geometric.PGbox; 12 | 13 | import com.github.thealchemist.pg_hibernate.types.Box; 14 | import com.github.thealchemist.pg_hibernate.types.Point; 15 | 16 | /** 17 | * A Hibernate UserType for PostgreSQL's box type. 18 | * 19 | * @author Jesse Costello-Good 20 | * @version $Id$ 21 | */ 22 | public class BoxType implements UserType { 23 | 24 | @Override 25 | public int[] sqlTypes() { 26 | return new int[] { java.sql.Types.OTHER }; 27 | } 28 | 29 | @Override 30 | public Class returnedClass() { 31 | return Box.class; 32 | } 33 | 34 | @Override 35 | public boolean equals(Object o, Object o1) throws HibernateException { 36 | if (o == null && o1 == null) 37 | return true; 38 | else if (o == null || o1 == null) 39 | return false; 40 | return o.equals(o1); 41 | } 42 | 43 | @Override 44 | public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sessionImplementor, Object owner) 45 | throws HibernateException, SQLException { 46 | if (names.length != 1) 47 | throw new IllegalArgumentException("names.length != 1, names = " + names); 48 | 49 | PGbox value = (PGbox) resultSet.getObject(names[0]); 50 | 51 | if (value == null) { 52 | return null; 53 | } else { 54 | Point p1 = new Point(value.point[0].x, value.point[0].y); 55 | Point p2 = new Point(value.point[1].x, value.point[1].y); 56 | return new Box(p1, p2); 57 | } 58 | } 59 | 60 | @Override 61 | public void nullSafeSet(PreparedStatement preparedStatement, Object value, int i, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException { 62 | Box rect = (Box) value; 63 | 64 | if (value == null) { 65 | preparedStatement.setNull(i, java.sql.Types.OTHER); 66 | } else { 67 | preparedStatement.setObject(i, new PGbox(rect.getP1().getX(), rect.getP1().getY(), 68 | rect.getP2().getX(), rect.getP2().getY())); 69 | } 70 | } 71 | 72 | @Override 73 | public Object deepCopy(Object o) throws HibernateException { 74 | if (o == null) 75 | return null; 76 | 77 | try { 78 | return ((Box) o).clone(); 79 | } catch (CloneNotSupportedException e) { 80 | throw new IllegalArgumentException(e.toString()); 81 | } 82 | } 83 | 84 | @Override 85 | public boolean isMutable() { 86 | return false; 87 | } 88 | 89 | @Override 90 | public int hashCode(Object o) throws HibernateException { 91 | return o.hashCode(); 92 | } 93 | 94 | @Override 95 | public Serializable disassemble(Object o) throws HibernateException { 96 | return (Serializable) o; 97 | } 98 | 99 | @Override 100 | public Object assemble(Serializable cached, Object owner) throws HibernateException { 101 | return cached; 102 | } 103 | 104 | @Override 105 | public Object replace(Object original, Object target, Object owner) throws HibernateException { 106 | return original; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/CircleType.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import java.io.Serializable; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | import org.hibernate.HibernateException; 9 | import org.hibernate.engine.spi.SharedSessionContractImplementor; 10 | import org.hibernate.usertype.UserType; 11 | import org.postgresql.geometric.PGcircle; 12 | 13 | import com.github.thealchemist.pg_hibernate.types.Circle; 14 | import com.github.thealchemist.pg_hibernate.types.Point; 15 | 16 | /** 17 | * A Hibernate UserType for PostgreSQL's circle type.
18 | *
19 | * Note: this implementation unfortunately uses string parsing of the PostgreSQL data because of a problem (oversite?) 20 | * of the PostgreSQL JDBC driver. 21 | * 22 | * @author Jesse Costello-Good 23 | * @version $Id$ 24 | */ 25 | public class CircleType implements UserType { 26 | 27 | @Override 28 | public int[] sqlTypes() { 29 | return new int[] { java.sql.Types.OTHER }; 30 | } 31 | 32 | @Override 33 | public Class returnedClass() { 34 | return Circle.class; 35 | } 36 | 37 | @Override 38 | public boolean equals(Object o, Object o1) throws HibernateException { 39 | if (o == null && o1 == null) 40 | return true; 41 | else if (o == null || o1 == null) 42 | return false; 43 | return o.equals(o1); 44 | } 45 | 46 | @Override 47 | public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sessionImplementor, Object owner) 48 | throws HibernateException, SQLException { 49 | if (names.length != 1) 50 | throw new IllegalArgumentException("names.length != 1, names = " + names); 51 | 52 | PGcircle value = (PGcircle) resultSet.getObject(names[0]); 53 | 54 | if (value == null) { 55 | return null; 56 | } else { 57 | /* 58 | * Because of an oversite in the Postgres JDBC library, we have to parse a circle from its string 59 | * representation: <(5.23,9.71234),4.10293> 60 | */ 61 | String asString = value.getValue(); 62 | double cx = 0, cy = 0, r = 0; 63 | try { 64 | cx = Double.parseDouble(asString.substring(asString.indexOf("(") + 1, asString.indexOf(","))); 65 | cy = Double.parseDouble(asString.substring(asString.indexOf(",") + 1, asString.indexOf(")"))); 66 | r = Double.parseDouble(asString.substring(asString.lastIndexOf(",") + 1, asString.indexOf(">"))); 67 | } catch (RuntimeException e) { 68 | throw new HibernateException("error parsing circle: " + asString, e); 69 | } 70 | return new Circle(new Point(cx, cy), r); 71 | } 72 | } 73 | 74 | @Override 75 | public void nullSafeSet(PreparedStatement preparedStatement, Object value, int i, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException { 76 | Circle c = (Circle) value; 77 | 78 | if (value == null) { 79 | preparedStatement.setNull(i, java.sql.Types.OTHER); 80 | } else { 81 | preparedStatement.setObject(i, new PGcircle(c.getCenter().getX(), c.getCenter().getY(), c.getRadius())); 82 | } 83 | } 84 | 85 | @Override 86 | public Object deepCopy(Object o) throws HibernateException { 87 | if (o == null) 88 | return null; 89 | 90 | try { 91 | return ((Circle) o).clone(); 92 | } catch (CloneNotSupportedException e) { 93 | throw new IllegalArgumentException(e.toString()); 94 | } 95 | } 96 | 97 | @Override 98 | public boolean isMutable() { 99 | return false; 100 | } 101 | 102 | @Override 103 | public int hashCode(Object o) throws HibernateException { 104 | return o.hashCode(); 105 | } 106 | 107 | @Override 108 | public Serializable disassemble(Object o) throws HibernateException { 109 | return (Serializable) o; 110 | } 111 | 112 | @Override 113 | public Object assemble(Serializable cached, Object owner) throws HibernateException { 114 | return cached; 115 | } 116 | 117 | @Override 118 | public Object replace(Object original, Object target, Object owner) throws HibernateException { 119 | return original; 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/HstoreType.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import java.io.Serializable; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | import java.sql.Types; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | import org.hibernate.HibernateException; 12 | import org.hibernate.engine.spi.SharedSessionContractImplementor; 13 | import org.hibernate.usertype.UserType; 14 | 15 | import org.postgresql.util.HStoreConverter; 16 | 17 | // courtesy of: http://backtothefront.net/2011/storing-sets-keyvalue-pairs-single-db-column-hibernate-postgresql-hstore-type/ 18 | public class HstoreType implements UserType { 19 | 20 | public HstoreType() { 21 | } 22 | 23 | @Override 24 | public Object assemble(Serializable cached, Object owner) 25 | throws HibernateException { 26 | return cached; 27 | } 28 | 29 | @Override 30 | public Object deepCopy(Object o) throws HibernateException { 31 | if(o == null) { 32 | return null; 33 | } 34 | else { 35 | // It's not a true deep copy, but we store only String instances, and they 36 | // are immutable, so it should be OK 37 | Map m = (Map) o; 38 | return new HashMap(m); 39 | } 40 | } 41 | 42 | @Override 43 | public Serializable disassemble(Object o) throws HibernateException { 44 | return (Serializable) o; 45 | } 46 | 47 | @Override 48 | public boolean equals(Object o1, Object o2) throws HibernateException { 49 | Map m1 = (Map) o1; 50 | Map m2 = (Map) o2; 51 | return m1.equals(m2); 52 | } 53 | 54 | @Override 55 | public int hashCode(Object o) throws HibernateException { 56 | return o.hashCode(); 57 | } 58 | 59 | @Override 60 | public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sessionImplementor, Object owner) throws HibernateException, SQLException { 61 | String col = names[0]; 62 | String val = resultSet.getString(col); 63 | return val == null ? HStoreConverter.fromString("") : HStoreConverter.fromString(val); 64 | } 65 | 66 | @Override 67 | public void nullSafeSet(PreparedStatement preparedStatement, Object value, int i, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException { 68 | String s = HStoreConverter.toString((Map) value); 69 | preparedStatement.setObject(i, s, Types.OTHER); 70 | } 71 | 72 | @Override 73 | public boolean isMutable() { 74 | return true; 75 | } 76 | 77 | @Override 78 | public Object replace(Object original, Object target, Object owner) 79 | throws HibernateException { 80 | return original; 81 | } 82 | 83 | @Override 84 | public Class returnedClass() { 85 | return Map.class; 86 | } 87 | 88 | @Override 89 | public int[] sqlTypes() { 90 | /* 91 | * i'm not sure what value should be used here, but it works, AFAIK only 92 | * length of this array matters, as it is a column span (1 in our case) 93 | */ 94 | return new int[] { Types.INTEGER }; 95 | } 96 | } -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/InetAddressType.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import java.io.Serializable; 4 | import java.net.InetAddress; 5 | import java.net.UnknownHostException; 6 | import java.sql.PreparedStatement; 7 | import java.sql.ResultSet; 8 | import java.sql.SQLException; 9 | 10 | import org.hibernate.HibernateException; 11 | import org.hibernate.engine.spi.SharedSessionContractImplementor; 12 | import org.hibernate.usertype.UserType; 13 | import org.postgresql.util.PGobject; 14 | 15 | import com.google.common.net.InetAddresses; 16 | 17 | /** 18 | * A Hibernate UserType for PostgreSQL's inet type. 19 | * 20 | * @author Jesse Costello-Good, The Alchemist 21 | */ 22 | public class InetAddressType implements UserType { 23 | 24 | @Override 25 | public int[] sqlTypes() { 26 | return new int[]{java.sql.Types.OTHER}; 27 | } 28 | 29 | @Override 30 | public Class returnedClass() { 31 | return InetAddress.class; 32 | } 33 | 34 | @Override 35 | public boolean equals( Object o, Object o1 ) throws HibernateException { 36 | if (o == null && o1 == null) 37 | return true; 38 | else if (o == null || o1 == null) 39 | return false; 40 | return o.equals(o1); 41 | } 42 | 43 | @Override 44 | public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sessionImplementor, Object owner) throws HibernateException, SQLException { 45 | if (names.length != 1) 46 | throw new IllegalArgumentException("names.length != 1, names = " + names); 47 | 48 | String value = resultSet.getString(names[0]); 49 | 50 | if (value == null) { 51 | return null; 52 | } else { 53 | return InetAddresses.forString(value); 54 | } 55 | } 56 | 57 | @Override 58 | public void nullSafeSet(PreparedStatement preparedStatement, Object value, int i, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException { 59 | if (value == null) { 60 | preparedStatement.setNull(i, java.sql.Types.OTHER); 61 | } else { 62 | PGobject object = new PGobject(); 63 | object.setValue(((InetAddress) value).getHostAddress()); 64 | object.setType("inet"); 65 | preparedStatement.setObject(i, object); 66 | } 67 | } 68 | 69 | @Override 70 | public Object deepCopy( Object o ) throws HibernateException { 71 | if (o == null) 72 | return null; 73 | 74 | try { 75 | return InetAddress.getByAddress(((InetAddress) o).getAddress()); 76 | } catch (UnknownHostException e) { 77 | throw new AssertionError("this can't happen!"); 78 | } 79 | } 80 | 81 | @Override 82 | public boolean isMutable() { 83 | return false; 84 | } 85 | 86 | @Override 87 | public int hashCode(Object o) throws HibernateException { 88 | return o.hashCode(); 89 | } 90 | 91 | @Override 92 | public Serializable disassemble(Object o) throws HibernateException { 93 | return (Serializable) o; 94 | } 95 | 96 | @Override 97 | public Object assemble(Serializable cached, Object owner) throws HibernateException { 98 | return cached; 99 | } 100 | 101 | @Override 102 | public Object replace(Object original, Object target, Object owner) throws HibernateException { 103 | return original; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/IntegerArrayType.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import java.io.Serializable; 4 | import java.sql.Array; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.util.Arrays; 9 | 10 | import org.hibernate.HibernateException; 11 | import org.hibernate.engine.spi.SharedSessionContractImplementor; 12 | import org.hibernate.usertype.UserType; 13 | 14 | /** 15 | * A Hibernate UserType for PostgreSQL's native intarray type. 16 | * 17 | * @see http://stackoverflow.com/a/26231403/423943 18 | */ 19 | public class IntegerArrayType implements UserType { 20 | 21 | private static final int[] JDBC_TYPES = new int[]{java.sql.Types.ARRAY}; 22 | 23 | @Override 24 | public int[] sqlTypes() { 25 | return JDBC_TYPES; 26 | } 27 | 28 | @Override 29 | public Class returnedClass() { 30 | return Integer[].class; 31 | } 32 | 33 | @Override 34 | public boolean equals( Object o1, Object o2 ) throws HibernateException { 35 | if (o1 == null && o2 == null) 36 | return true; 37 | else if (o1 == null || o2 == null) 38 | return false; 39 | if(o1 instanceof int[] && o2 instanceof int[]) { 40 | return Arrays.equals(((int[]) o1), ((int[])o2)); 41 | } else { 42 | return Arrays.equals((Integer[]) o1, (Integer[]) o2); 43 | } 44 | } 45 | 46 | @Override 47 | public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sessionImplementor, Object owner) throws HibernateException, SQLException { 48 | 49 | if (names.length != 1) 50 | throw new IllegalArgumentException("names.length != 1, names = " + names); 51 | 52 | Array value = resultSet.getArray(names[0]); 53 | 54 | if (value == null) { 55 | return null; 56 | } else { 57 | return value.getArray(); 58 | } 59 | } 60 | 61 | /** 62 | * Supports both int[] and Integer[], yay! 63 | */ 64 | @Override 65 | public void nullSafeSet(PreparedStatement preparedStatement, Object value, int i, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException { 66 | // use JDBC array type, which is fairly new, so tread carefully 67 | if (value == null) { 68 | preparedStatement.setNull(i, java.sql.Types.ARRAY); 69 | } else if(value instanceof Integer[]){ 70 | Integer[] myArray = (Integer[]) value; 71 | Array inArray = preparedStatement.getConnection().createArrayOf("integer", myArray); 72 | preparedStatement.setArray(i, inArray); 73 | } else if(value instanceof int[]) { 74 | int[] myArray = (int[]) value; 75 | Array inArray = preparedStatement.getConnection().createArrayOf("integer", wrap(myArray)); 76 | preparedStatement.setArray(i, inArray); 77 | } else { 78 | throw new IllegalArgumentException("Invalid type of input: " + value.getClass().getName()); 79 | } 80 | } 81 | 82 | @Override 83 | public Object deepCopy( Object o ) throws HibernateException { 84 | if (o == null) { 85 | return null; 86 | } else if(o instanceof Integer[]) { 87 | Integer[] array = (Integer[]) o; 88 | return array.clone(); 89 | } else if(o instanceof int[]) { 90 | int[] array = (int[]) o; 91 | return array.clone(); 92 | } else { 93 | throw new IllegalArgumentException("Invalid type to copy: " + o.getClass().getName()); 94 | } 95 | } 96 | 97 | @Override 98 | public boolean isMutable() { 99 | return true; 100 | } 101 | 102 | @Override 103 | public int hashCode(Object o) throws HibernateException { 104 | return o.hashCode(); 105 | } 106 | 107 | @Override 108 | public Serializable disassemble(Object o) throws HibernateException { 109 | return (Serializable) o; 110 | } 111 | 112 | @Override 113 | public Object assemble(Serializable cached, Object owner) throws HibernateException { 114 | return cached; 115 | } 116 | 117 | @Override 118 | public Object replace(Object original, Object target, Object owner) throws HibernateException { 119 | return original; 120 | } 121 | 122 | private static Object[] wrap(int[] intArray) { 123 | Integer[] result = new Integer[intArray.length]; 124 | for (int i = 0; i < intArray.length; i++) { 125 | result[i] = Integer.valueOf(intArray[i]); 126 | } 127 | return result; 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/LineSegmentType.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import java.io.Serializable; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | import org.hibernate.HibernateException; 9 | import org.hibernate.engine.spi.SharedSessionContractImplementor; 10 | import org.hibernate.usertype.UserType; 11 | import org.postgresql.geometric.PGlseg; 12 | 13 | import com.github.thealchemist.pg_hibernate.types.LineSegment; 14 | import com.github.thealchemist.pg_hibernate.types.Point; 15 | 16 | /** 17 | * A Hibernate UserType for PostgreSQL's lseg type. 18 | * 19 | * @author Jesse Costello-Good 20 | * @version $Id$ 21 | */ 22 | public class LineSegmentType implements UserType { 23 | 24 | @Override 25 | public int[] sqlTypes() { 26 | return new int[]{java.sql.Types.OTHER}; 27 | } 28 | 29 | @Override 30 | public Class returnedClass() { 31 | return LineSegment.class; 32 | } 33 | 34 | @Override 35 | public boolean equals( Object o, Object o1 ) throws HibernateException { 36 | if (o == null && o1 == null) 37 | return true; 38 | else if (o == null || o1 == null) 39 | return false; 40 | return o.equals(o1); 41 | } 42 | 43 | @Override 44 | public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sessionImplementor, Object owner) throws HibernateException, SQLException { 45 | if (names.length != 1) 46 | throw new IllegalArgumentException("names.length != 1, names = " + names); 47 | 48 | PGlseg value = (PGlseg) resultSet.getObject(names[0]); 49 | 50 | if (value == null) { 51 | return null; 52 | } else { 53 | Point p1 = new Point(value.point[0].x, value.point[0].y); 54 | Point p2 = new Point(value.point[1].x, value.point[1].y); 55 | return new LineSegment(p1, p2); 56 | } 57 | } 58 | 59 | @Override 60 | public void nullSafeSet(PreparedStatement preparedStatement, Object value, int i, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException { 61 | LineSegment line = (LineSegment) value; 62 | 63 | if (value == null) { 64 | preparedStatement.setNull(i, java.sql.Types.OTHER); 65 | } else { 66 | preparedStatement.setObject(i, new PGlseg(line.getP1().getX(), line.getP1().getY(), 67 | line.getP2().getX(), line.getP2().getY())); 68 | } 69 | } 70 | 71 | @Override 72 | public Object deepCopy( Object o ) throws HibernateException { 73 | if (o == null) 74 | return null; 75 | 76 | try { 77 | return ((LineSegment) o).clone(); 78 | } catch (CloneNotSupportedException e) { 79 | throw new IllegalArgumentException(e.toString()); 80 | } 81 | } 82 | 83 | @Override 84 | public boolean isMutable() { 85 | return false; 86 | } 87 | 88 | @Override 89 | public int hashCode(Object o) throws HibernateException { 90 | return o.hashCode(); 91 | } 92 | 93 | @Override 94 | public Serializable disassemble(Object o) throws HibernateException { 95 | return (Serializable) o; 96 | } 97 | 98 | @Override 99 | public Object assemble(Serializable cached, Object owner) throws HibernateException { 100 | return cached; 101 | } 102 | 103 | @Override 104 | public Object replace(Object original, Object target, Object owner) throws HibernateException { 105 | return original; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/PointType.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import java.io.Serializable; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | import org.hibernate.HibernateException; 9 | import org.hibernate.engine.spi.SharedSessionContractImplementor; 10 | import org.hibernate.usertype.UserType; 11 | import org.postgresql.geometric.PGpoint; 12 | 13 | import com.github.thealchemist.pg_hibernate.types.Point; 14 | 15 | /** 16 | * A Hibernate UserType for PostgreSQL's point type. 17 | * 18 | * @author Jesse Costello-Good 19 | * @version $Id$ 20 | */ 21 | public class PointType implements UserType { 22 | 23 | @Override 24 | public int[] sqlTypes() { 25 | return new int[]{java.sql.Types.OTHER}; 26 | } 27 | 28 | @Override 29 | public Class returnedClass() { 30 | return Point.class; 31 | } 32 | 33 | @Override 34 | public boolean equals( Object o, Object o1 ) throws HibernateException { 35 | if (o == null && o1 == null) 36 | return true; 37 | else if (o == null || o1 == null) 38 | return false; 39 | return o.equals(o1); 40 | } 41 | 42 | @Override 43 | public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sessionImplementor, Object owner) throws HibernateException, SQLException { 44 | if (names.length != 1) 45 | throw new IllegalArgumentException("names.length != 1, names = " + names); 46 | 47 | PGpoint value = (PGpoint) resultSet.getObject(names[0]); 48 | 49 | if (value == null) { 50 | return null; 51 | } else { 52 | return new Point(value.x, value.y); 53 | } 54 | } 55 | 56 | @Override 57 | public void nullSafeSet(PreparedStatement preparedStatement, Object value, int i, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException { 58 | if (value == null) { 59 | preparedStatement.setNull(i, java.sql.Types.OTHER); 60 | } else { 61 | preparedStatement.setObject(i, new PGpoint(((Point) value).getX(), ((Point) value).getY())); 62 | } 63 | } 64 | 65 | @Override 66 | public Object deepCopy( Object o ) throws HibernateException { 67 | if (o == null) 68 | return null; 69 | 70 | try { 71 | return ((Point) o).clone(); 72 | } catch (CloneNotSupportedException e) { 73 | throw new IllegalArgumentException(e); 74 | } 75 | } 76 | 77 | @Override 78 | public boolean isMutable() { 79 | return false; 80 | } 81 | 82 | @Override 83 | public int hashCode(Object o) throws HibernateException { 84 | return o.hashCode(); 85 | } 86 | 87 | @Override 88 | public Serializable disassemble(Object o) throws HibernateException { 89 | return (Serializable) o; 90 | } 91 | 92 | @Override 93 | public Object assemble(Serializable cached, Object owner) throws HibernateException { 94 | return cached; 95 | } 96 | 97 | @Override 98 | public Object replace(Object original, Object target, Object owner) throws HibernateException { 99 | return original; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/PolygonType.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import org.hibernate.HibernateException; 4 | import org.hibernate.engine.spi.SessionImplementor; 5 | import org.hibernate.engine.spi.SharedSessionContractImplementor; 6 | import org.hibernate.usertype.UserType; 7 | import org.postgresql.geometric.PGpoint; 8 | import org.postgresql.geometric.PGpolygon; 9 | 10 | import com.github.thealchemist.pg_hibernate.types.Point; 11 | import com.github.thealchemist.pg_hibernate.types.Polygon; 12 | 13 | import java.io.Serializable; 14 | import java.sql.PreparedStatement; 15 | import java.sql.ResultSet; 16 | import java.sql.SQLException; 17 | 18 | /** 19 | * A Hibernate UserType for PostgreSQL's polygon type. 20 | * 21 | * @author Jesse Costello-Good 22 | * @version $Id$ 23 | */ 24 | public class PolygonType implements UserType { 25 | 26 | @Override 27 | public int[] sqlTypes() { 28 | return new int[]{java.sql.Types.OTHER}; 29 | } 30 | 31 | @Override 32 | public Class returnedClass() { 33 | return Polygon.class; 34 | } 35 | 36 | @Override 37 | public boolean equals( Object o, Object o1 ) throws HibernateException { 38 | if (o == null && o1 == null) 39 | return true; 40 | else if (o == null || o1 == null) 41 | return false; 42 | return o.equals(o1); 43 | } 44 | 45 | @Override 46 | public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sessionImplementor, Object owner) throws HibernateException, SQLException { 47 | 48 | if (names.length != 1) 49 | throw new IllegalArgumentException("names.length != 1, names = " + names); 50 | 51 | PGpolygon value = (PGpolygon) resultSet.getObject(names[0]); 52 | 53 | if (value == null) { 54 | return null; 55 | } else { 56 | return new Polygon(convert(value.points)); 57 | } 58 | } 59 | 60 | private Point[] convert( PGpoint[] points ) { 61 | Point[] newpoints = new Point[points.length]; 62 | for (int i = 0; i < points.length; i++) { 63 | newpoints[i] = new Point(points[i].x, points[i].y); 64 | } 65 | return newpoints; 66 | } 67 | 68 | private PGpoint[] convert( Point[] points ) { 69 | PGpoint[] newpoints = new PGpoint[points.length]; 70 | for (int i = 0; i < points.length; i++) { 71 | newpoints[i] = new PGpoint(points[i].getX(), points[i].getY()); 72 | } 73 | return newpoints; 74 | } 75 | 76 | @Override 77 | public void nullSafeSet(PreparedStatement preparedStatement, Object value, int i, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException { 78 | Polygon polygon = (Polygon) value; 79 | 80 | if (value == null) { 81 | preparedStatement.setNull(i, java.sql.Types.OTHER); 82 | } else { 83 | preparedStatement.setObject(i, new PGpolygon(convert(polygon.getPoints()))); 84 | } 85 | } 86 | 87 | @Override 88 | public Object deepCopy( Object o ) throws HibernateException { 89 | if (o == null) 90 | return null; 91 | 92 | try { 93 | return ((Polygon) o).clone(); 94 | } catch (CloneNotSupportedException e) { 95 | throw new IllegalArgumentException(e.toString()); 96 | } 97 | } 98 | 99 | @Override 100 | public boolean isMutable() { 101 | return false; 102 | } 103 | 104 | @Override 105 | public int hashCode(Object o) throws HibernateException { 106 | return o.hashCode(); 107 | } 108 | 109 | @Override 110 | public Serializable disassemble(Object o) throws HibernateException { 111 | return (Serializable) o; 112 | } 113 | 114 | @Override 115 | public Object assemble(Serializable cached, Object owner) throws HibernateException { 116 | return cached; 117 | } 118 | 119 | @Override 120 | public Object replace(Object original, Object target, Object owner) throws HibernateException { 121 | return original; 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/StringArrayType.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import java.io.Serializable; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | import java.util.Arrays; 8 | import java.util.StringTokenizer; 9 | 10 | import org.hibernate.HibernateException; 11 | import org.hibernate.engine.spi.SharedSessionContractImplementor; 12 | import org.hibernate.usertype.UserType; 13 | import org.postgresql.util.PGobject; 14 | 15 | /** 16 | * A Hibernate UserType for PostgreSQL's native varchar[] type. 17 | * 18 | * @author Jesse Costello-Good 19 | * @version $Id$ 20 | */ 21 | public class StringArrayType implements UserType { 22 | 23 | @Override 24 | public int[] sqlTypes() { 25 | return new int[]{java.sql.Types.OTHER}; 26 | } 27 | 28 | @Override 29 | public Class returnedClass() { 30 | return String[].class; 31 | } 32 | 33 | @Override 34 | public boolean equals( Object o, Object o1 ) throws HibernateException { 35 | if (o == null && o1 == null) 36 | return true; 37 | else if (o == null || o1 == null) 38 | return false; 39 | return Arrays.equals((String[]) o, (String[]) o1); 40 | } 41 | 42 | @Override 43 | public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sessionImplementor, Object owner) throws HibernateException, SQLException { 44 | if (names.length != 1) 45 | throw new IllegalArgumentException("names.length != 1, names = " + names); 46 | 47 | String value = resultSet.getString(names[0]); 48 | 49 | if (value == null) { 50 | return null; 51 | } else if (value.length() < 2) { 52 | return new String[]{}; 53 | } else { 54 | StringTokenizer tokenizer = new StringTokenizer(value.substring(1, value.length() - 1), ","); 55 | String[] values = new String[tokenizer.countTokens()]; 56 | int i = 0; 57 | while (tokenizer.hasMoreTokens()) { 58 | values[i++] = tokenizer.nextToken(); 59 | } 60 | return values; 61 | } 62 | } 63 | 64 | @Override 65 | public void nullSafeSet(PreparedStatement preparedStatement, Object value, int i, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException { 66 | String[] strings = (String[]) value; 67 | 68 | if (value == null) { 69 | preparedStatement.setNull(i, java.sql.Types.OTHER); 70 | } else { 71 | StringBuffer buffer = new StringBuffer(); 72 | for (int j = 0; j < strings.length; j++) { 73 | buffer.append("\"" + strings[j] + "\""); 74 | if (j < strings.length - 1) 75 | buffer.append(","); 76 | } 77 | 78 | PGobject object = new PGobject(); 79 | object.setValue("{" + buffer + "}"); 80 | preparedStatement.setObject(i, object); 81 | } 82 | } 83 | 84 | @Override 85 | public Object deepCopy( Object o ) throws HibernateException { 86 | if (o == null) 87 | return null; 88 | 89 | return ((String[]) o).clone(); 90 | } 91 | 92 | @Override 93 | public boolean isMutable() { 94 | return true; 95 | } 96 | 97 | @Override 98 | public int hashCode(Object o) throws HibernateException { 99 | return o.hashCode(); 100 | } 101 | 102 | @Override 103 | public Serializable disassemble(Object o) throws HibernateException { 104 | return (Serializable) o; 105 | } 106 | 107 | @Override 108 | public Object assemble(Serializable cached, Object owner) throws HibernateException { 109 | return cached; 110 | } 111 | 112 | @Override 113 | public Object replace(Object original, Object target, Object owner) throws HibernateException { 114 | return original; 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/XMLType.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import java.io.Serializable; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | import java.sql.Types; 8 | 9 | import org.hibernate.HibernateException; 10 | import org.hibernate.engine.spi.SharedSessionContractImplementor; 11 | import org.hibernate.usertype.UserType; 12 | 13 | /** 14 | * A Hibernate UserType for PostgreSQL's xml type. 15 | * 16 | * @author kpietrzak 17 | * @source https://wiki.postgresql.org/wiki/Hibernate_XML_Type 18 | */ 19 | public class XMLType implements UserType { 20 | 21 | private final int[] sqlTypesSupported = new int[] { Types.VARCHAR }; 22 | 23 | @Override 24 | public int[] sqlTypes() { 25 | return sqlTypesSupported; 26 | } 27 | 28 | @Override 29 | public Class returnedClass() { 30 | return String.class; 31 | } 32 | 33 | @Override 34 | public boolean equals(Object x, Object y) throws HibernateException { 35 | if (x == null) { 36 | return y == null; 37 | } else { 38 | return x.equals(y); 39 | } 40 | } 41 | 42 | @Override 43 | public int hashCode(Object x) throws HibernateException { 44 | return x == null ? null : x.hashCode(); 45 | } 46 | 47 | @Override 48 | public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { 49 | assert(names.length == 1); 50 | String xmldoc = rs.getString( names[0] ); 51 | return rs.wasNull() ? null : xmldoc; 52 | } 53 | 54 | @Override 55 | public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { 56 | if (value == null) { 57 | st.setNull(index, Types.OTHER); 58 | } else { 59 | st.setObject(index, value, Types.OTHER); 60 | } 61 | } 62 | 63 | @Override 64 | public Object deepCopy(Object value) throws HibernateException { 65 | if (value == null) 66 | return null; 67 | 68 | return new String( (String)value ); 69 | } 70 | 71 | @Override 72 | public boolean isMutable() { 73 | return false; 74 | } 75 | 76 | @Override 77 | public Serializable disassemble(Object value) throws HibernateException { 78 | return (String) value; 79 | } 80 | 81 | @Override 82 | public Object assemble(Serializable cached, Object owner) throws HibernateException { 83 | return cached; 84 | } 85 | 86 | @Override 87 | public Object replace(Object original, Object target, Object owner) throws HibernateException { 88 | return original; 89 | } 90 | } -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/types/Box.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.types; 2 | 3 | import java.awt.geom.Rectangle2D; 4 | import java.io.Serializable; 5 | 6 | /** 7 | * A simple immutable rectangle object. 8 | * 9 | * @author Jesse Costello-Good 10 | * @version $Id$ 11 | */ 12 | public class Box implements Serializable, Cloneable { 13 | 14 | private final Point p1; 15 | private final Point p2; 16 | 17 | public Box( Point p1, Point p2 ) { 18 | this.p1 = p1; 19 | this.p2 = p2; 20 | } 21 | 22 | public Point getP1() { 23 | return p1; 24 | } 25 | 26 | public Point getP2() { 27 | return p2; 28 | } 29 | 30 | public int hashCode() { 31 | return p1.hashCode() + p2.hashCode(); 32 | } 33 | 34 | public boolean equals( Object obj ) { 35 | if (obj instanceof Box) { 36 | return (p1.equals(((Box) obj).p1) && p2.equals(((Box) obj).p2)) || 37 | (p1.equals(((Box) obj).p2) && p2.equals(((Box) obj).p1)); 38 | } 39 | return super.equals(obj); 40 | } 41 | 42 | public Rectangle2D asRectangle2D() { 43 | return new Rectangle2D.Double(p1.getX(), p1.getY(), p2.getX() - p1.getX(), p2.getY() - p1.getY()); 44 | } 45 | 46 | public Object clone() throws CloneNotSupportedException { 47 | return super.clone(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/types/Circle.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.types; 2 | 3 | import java.awt.geom.Ellipse2D; 4 | import java.io.Serializable; 5 | 6 | /** 7 | * A simple immutable circle object. 8 | * 9 | * @author Jesse Costello-Good 10 | * @version $Id$ 11 | */ 12 | public class Circle implements Serializable, Cloneable { 13 | 14 | private final Point center; 15 | private final double radius; 16 | 17 | public Circle( Point center, double radius ) { 18 | this.center = center; 19 | this.radius = radius; 20 | } 21 | 22 | public Point getCenter() { 23 | return center; 24 | } 25 | 26 | public double getRadius() { 27 | return radius; 28 | } 29 | 30 | public Ellipse2D asEllipse2D() { 31 | return new Ellipse2D.Double(center.getX() - radius, center.getY() - radius, radius * 2, radius * 2); 32 | } 33 | 34 | public int hashCode() { 35 | return (int) radius * 31 + center.hashCode(); 36 | } 37 | 38 | public boolean equals( Object obj ) { 39 | if (obj instanceof Circle) { 40 | return radius == ((Circle) obj).radius && center.equals(((Circle) obj).getCenter()); 41 | } 42 | return super.equals(obj); 43 | } 44 | 45 | public Object clone() throws CloneNotSupportedException { 46 | return super.clone(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/types/LineSegment.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.types; 2 | 3 | import java.awt.geom.Line2D; 4 | import java.io.Serializable; 5 | 6 | /** 7 | * A simple immutable (directed) line segment object. 8 | * 9 | * @author Jesse Costello-Good 10 | */ 11 | public class LineSegment implements Serializable, Cloneable { 12 | 13 | private final Point p1; 14 | private final Point p2; 15 | 16 | public LineSegment( Point p1, Point p2 ) { 17 | this.p1 = p1; 18 | this.p2 = p2; 19 | } 20 | 21 | public Point getP1() { 22 | return p1; 23 | } 24 | 25 | public Point getP2() { 26 | return p2; 27 | } 28 | 29 | @Override 30 | public int hashCode() { 31 | return p1.hashCode() * 29 + p2.hashCode(); 32 | } 33 | 34 | @Override 35 | public boolean equals( Object obj ) { 36 | if (obj instanceof LineSegment) { 37 | return p1.equals(((LineSegment) obj).p1) && p2.equals(((LineSegment) obj).p2); 38 | } 39 | return super.equals(obj); 40 | } 41 | 42 | public Line2D asLine2D() { 43 | return new Line2D.Double(p1.asPoint2D(), p2.asPoint2D()); 44 | } 45 | 46 | @Override 47 | public Object clone() throws CloneNotSupportedException { 48 | return super.clone(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/types/Point.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.types; 2 | 3 | import java.awt.geom.Point2D; 4 | import java.io.Serializable; 5 | 6 | /** 7 | * A simple immutable point object. 8 | * 9 | * @author Jesse Costello-Good 10 | * @version $Id$ 11 | */ 12 | public class Point implements Serializable, Cloneable { 13 | 14 | private final double x; 15 | private final double y; 16 | 17 | public Point( double x, double y ) { 18 | this.x = x; 19 | this.y = y; 20 | } 21 | 22 | public double getX() { 23 | return x; 24 | } 25 | 26 | public double getY() { 27 | return y; 28 | } 29 | 30 | public Point2D asPoint2D() { 31 | return new Point2D.Double(x, y); 32 | } 33 | 34 | public boolean equals( Object obj ) { 35 | if (obj instanceof Point) { 36 | return x == ((Point) obj).x && y == ((Point) obj).y; 37 | } 38 | return super.equals(obj); 39 | } 40 | 41 | public int hashCode() { 42 | return (int) x * 31 + (int) y; 43 | } 44 | 45 | public Object clone() throws CloneNotSupportedException { 46 | return super.clone(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/github/thealchemist/pg_hibernate/types/Polygon.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.types; 2 | 3 | import java.io.Serializable; 4 | import java.util.Arrays; 5 | 6 | /** 7 | * A simple immutable polygon object. 8 | * 9 | * @author Jesse Costello-Good 10 | * @version $Id$ 11 | */ 12 | public class Polygon implements Serializable, Cloneable { 13 | 14 | private final Point[] points; 15 | 16 | public Polygon( Point[] points ) { 17 | this.points = points; 18 | } 19 | 20 | public Point[] getPoints() { 21 | return (Point[]) points.clone(); 22 | } 23 | 24 | public int hashCode() { 25 | return points.hashCode(); 26 | } 27 | 28 | public boolean equals( Object obj ) { 29 | if (obj instanceof Polygon) { 30 | return Arrays.equals(points, ((Polygon) obj).points); 31 | } 32 | return super.equals(obj); 33 | } 34 | 35 | public Object clone() throws CloneNotSupportedException { 36 | return super.clone(); 37 | } 38 | 39 | /* 40 | public GeneralPath asGeneralPath() { 41 | GeneralPath path = new GeneralPath(); 42 | path.append(new PolygonPathIterator(points), false); 43 | return path; 44 | } 45 | 46 | private static class PolygonPathIterator implements PathIterator { 47 | 48 | private final Point[] points; 49 | private int index = 0; 50 | 51 | public PolygonPathIterator( Point[] points ) { 52 | this.points = points; 53 | } 54 | 55 | public int getWindingRule() { 56 | return 0; 57 | } 58 | 59 | public boolean isDone() { 60 | return index > points.length; 61 | } 62 | 63 | public void next() { 64 | index++; 65 | } 66 | 67 | public int currentSegment( float[] coords ) { 68 | if (index < points.length) { 69 | coords[0] = (float) points[index-1].getX(); 70 | coords[1] = (float) points[index-1].getY(); 71 | } else { 72 | coords[0] = (float) points[0].getX(); 73 | coords[1] = (float) points[0].getY(); 74 | } 75 | return segmentType(); 76 | } 77 | 78 | public int currentSegment( double[] coords ) { 79 | if (index < points.length) { 80 | coords[0] = points[index-1].getX(); 81 | coords[1] = points[index-1].getY(); 82 | } else { 83 | coords[0] = points[0].getX(); 84 | coords[1] = points[0].getY(); 85 | } 86 | return segmentType(); 87 | } 88 | 89 | private int segmentType() { 90 | if (index == 1) { 91 | return SEG_MOVETO; 92 | } else if (index == points.length) { 93 | return SEG_CLOSE; 94 | } else { 95 | return SEG_LINETO; 96 | } 97 | } 98 | } 99 | */ 100 | } 101 | -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/BoxTypeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.test.context.jdbc.Sql; 6 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 7 | 8 | import static org.hamcrest.Matchers.equalTo; 9 | import static org.hamcrest.Matchers.is; 10 | import static org.junit.Assert.*; 11 | 12 | import com.github.thealchemist.pg_hibernate.spring.BoxTypeEntity; 13 | import com.github.thealchemist.pg_hibernate.types.Point; 14 | import com.github.thealchemist.pg_hibernate.types.Box; 15 | 16 | 17 | /** 18 | * @author Jesse Costello-Good, The Alchemist 19 | */ 20 | @RunWith(SpringJUnit4ClassRunner.class) 21 | public class BoxTypeTest extends HibernateTest { 22 | 23 | private static final double X1 = 54; 24 | private static final double Y1 = 5.6667; 25 | private static final double X2 = 1.342366234; 26 | private static final double Y2 = 0.0001; 27 | 28 | @Override 29 | @Test 30 | public void testSet() throws Exception { 31 | Box rect = new Box(new Point(X1, Y1), new Point(X2, Y2)); 32 | BoxTypeEntity entity = new BoxTypeEntity(); 33 | entity.setBox(rect); 34 | 35 | this.em.persist(entity); 36 | 37 | assertNotNull(entity.getId()); 38 | assertTrue(entity.getId().intValue() > 0); 39 | 40 | BoxTypeEntity fromDb = this.em.find(BoxTypeEntity.class, entity.getId()); 41 | 42 | assertNotNull(fromDb); 43 | assertNotNull(fromDb.getBox()); 44 | 45 | assertTrue(rect.equals(fromDb.getBox())); 46 | 47 | } 48 | 49 | @Test 50 | @Sql 51 | public void testGet() throws Exception { 52 | 53 | BoxTypeEntity fromDb = this.em.find(BoxTypeEntity.class, 37); 54 | 55 | assertNotNull(fromDb); 56 | Box box = fromDb.getBox(); 57 | assertNotNull(box); 58 | /* 59 | * points are re-ordered from upper right to lower left 60 | * 61 | * http://www.postgresql.org/docs/9.2/static/datatype-geometric.html 62 | */ 63 | assertThat(box.getP1(), is(equalTo(new Point(5, 5)))); 64 | assertThat(box.getP2(), is(equalTo(new Point(1, 1)))); 65 | 66 | } 67 | 68 | @Override 69 | @Test 70 | public void testNull() throws Exception { 71 | BoxTypeEntity entity = new BoxTypeEntity(); 72 | 73 | this.em.persist(entity); 74 | 75 | BoxTypeEntity fromDb = this.em.find(BoxTypeEntity.class, entity.getId()); 76 | 77 | assertNotNull(fromDb); 78 | assertNull(fromDb.getBox()); 79 | 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/CircleTypeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import static org.hamcrest.Matchers.*; 4 | import static org.junit.Assert.*; 5 | 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.test.context.jdbc.Sql; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | 11 | import com.github.thealchemist.pg_hibernate.spring.CircleTypeEntity; 12 | import com.github.thealchemist.pg_hibernate.types.Circle; 13 | import com.github.thealchemist.pg_hibernate.types.Point; 14 | 15 | 16 | /** 17 | * @author Jesse Costello-Good, The Alchemist 18 | */ 19 | @RunWith(SpringJUnit4ClassRunner.class) 20 | public class CircleTypeTest extends HibernateTest { 21 | 22 | private static final double X = 5.23; 23 | private static final double Y = 9.71234; 24 | private static final double R = 4.10293; 25 | 26 | @Override 27 | @Test 28 | public void testSet() throws Exception { 29 | Circle circle = new Circle(new Point(X, Y), R); 30 | CircleTypeEntity entity = new CircleTypeEntity(); 31 | entity.setCircle(circle); 32 | 33 | em.persist(entity); 34 | 35 | assertNotNull(entity.getId()); 36 | assertTrue(entity.getId().intValue() > 0); 37 | 38 | CircleTypeEntity fromDb = em.find(CircleTypeEntity.class, entity.getId()); 39 | 40 | assertNotNull(fromDb); 41 | assertNotNull(fromDb.getCircle()); 42 | 43 | assertTrue(circle.equals(fromDb.getCircle())); 44 | 45 | } 46 | 47 | @Test 48 | @Sql 49 | public void testGet() { 50 | CircleTypeEntity fromDb = em.find(CircleTypeEntity.class, 37); 51 | assertNotNull(fromDb); 52 | assertThat(fromDb.getCircle().getRadius(), is(equalTo(37.0))); 53 | assertThat(fromDb.getCircle().getCenter(), is(equalTo(new Point(1.0, 1.0)))); 54 | } 55 | 56 | @Override 57 | @Test 58 | public void testNull() throws Exception { 59 | CircleTypeEntity entity = new CircleTypeEntity(); 60 | 61 | em.persist(entity); 62 | 63 | CircleTypeEntity fromDb = em.find(CircleTypeEntity.class, entity.getId()); 64 | 65 | assertNotNull(fromDb); 66 | assertNull(fromDb.getCircle()); 67 | 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/HibernateTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import javax.annotation.Resource; 4 | import javax.inject.Inject; 5 | import javax.persistence.EntityManager; 6 | import javax.persistence.PersistenceContext; 7 | import javax.sql.DataSource; 8 | 9 | import org.junit.Before; 10 | import org.junit.Ignore; 11 | import org.junit.runner.RunWith; 12 | import org.springframework.core.env.Environment; 13 | import org.springframework.core.io.ResourceLoader; 14 | import org.springframework.jdbc.core.JdbcTemplate; 15 | import org.springframework.jdbc.core.simple.SimpleJdbcInsert; 16 | import org.springframework.jdbc.datasource.init.DatabasePopulator; 17 | import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; 18 | import org.springframework.test.context.ContextConfiguration; 19 | import org.springframework.test.context.TestExecutionListeners; 20 | import org.springframework.test.context.jdbc.Sql; 21 | import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener; 22 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.test.context.transaction.TransactionConfiguration; 25 | import org.springframework.test.context.transaction.TransactionalTestExecutionListener; 26 | import org.springframework.transaction.annotation.Transactional; 27 | 28 | import com.github.thealchemist.pg_hibernate.spring.PersistenceJPAConfig; 29 | 30 | 31 | /** 32 | * @author The Alchemist 33 | */ 34 | @RunWith(SpringJUnit4ClassRunner.class) 35 | @ContextConfiguration(classes= {PersistenceJPAConfig.class}) 36 | @Transactional() 37 | @TestExecutionListeners({ 38 | DependencyInjectionTestExecutionListener.class, 39 | TransactionalTestExecutionListener.class, 40 | SqlScriptsTestExecutionListener.class 41 | }) 42 | @TransactionConfiguration(defaultRollback=true) 43 | @Sql("classpath:/schema.sql") 44 | public abstract class HibernateTest { 45 | 46 | /* 47 | * created by Spring 48 | */ 49 | @Inject 50 | protected DataSource ds; 51 | @Inject 52 | protected Environment env; 53 | @Inject 54 | protected JdbcTemplate jdbcTemplate; 55 | @Inject 56 | protected SimpleJdbcInsert simpleJdbcInsert; 57 | @Resource 58 | protected ResourceLoader resourceLoader; 59 | 60 | @PersistenceContext 61 | protected EntityManager em; 62 | 63 | public abstract void testSet() throws Exception; 64 | public abstract void testNull() throws Exception; 65 | public abstract void testGet() throws Exception; 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/HstoreTypeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import static org.junit.Assert.*; 4 | import static org.hamcrest.Matchers.*; 5 | 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.util.Map; 9 | 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.springframework.dao.DataAccessException; 13 | import org.springframework.jdbc.core.ResultSetExtractor; 14 | import org.springframework.test.context.jdbc.Sql; 15 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 16 | 17 | import com.github.thealchemist.pg_hibernate.spring.HstoreTypeEntity; 18 | import com.google.common.collect.ImmutableMap; 19 | 20 | 21 | /** 22 | * @author Jesse Costello-Good, The Alchemist 23 | */ 24 | @RunWith(SpringJUnit4ClassRunner.class) 25 | public class HstoreTypeTest extends HibernateTest { 26 | 27 | @Override 28 | @Test 29 | public void testSet() throws Exception { 30 | Map m = ImmutableMap.of("a", "1", "b", "2", "c", "3"); 31 | HstoreTypeEntity entity = new HstoreTypeEntity(); 32 | entity.setMap(m); 33 | 34 | em.persist(entity); 35 | 36 | assertNotNull(entity.getId()); 37 | assertTrue(entity.getId().intValue() > 0); 38 | 39 | HstoreTypeEntity fromDb = em.find(HstoreTypeEntity.class, entity.getId()); 40 | 41 | assertNotNull(fromDb); 42 | assertNotNull(fromDb.getMap()); 43 | 44 | assertThat(entity.getMap(), is(equalTo(fromDb.getMap()))); 45 | 46 | } 47 | 48 | @Test 49 | @Sql 50 | public void testSetWithNewMap() throws Exception { 51 | HstoreTypeEntity fromDb = em.find(HstoreTypeEntity.class, 37); 52 | Map map = fromDb.getMap(); 53 | map.remove("name"); 54 | map.put("power", "ball"); 55 | em.flush(); 56 | 57 | String newMapValue = this.jdbcTemplate.query("select map from HstoreTypeEntity where id = 37", new ResultSetExtractor(){ 58 | 59 | @Override 60 | public String extractData(ResultSet rs) throws SQLException, DataAccessException { 61 | rs.next(); 62 | return rs.getString(1); 63 | }}); 64 | assertNotNull(newMapValue); 65 | /* 66 | * postgres puts a space around the '=>' operator, which I wanna ignore 67 | */ 68 | assertThat(newMapValue.replace(" => ", "=>"), is(equalTo("\"power\"=>\"ball\""))); 69 | } 70 | 71 | @Override 72 | @Test 73 | @Sql 74 | public void testGet() { 75 | HstoreTypeEntity fromDb = em.find(HstoreTypeEntity.class, 37); 76 | Map map = fromDb.getMap(); 77 | assertThat(map.entrySet(), hasSize(2)); 78 | assertThat(map, hasEntry("name", "kp")); 79 | assertThat(map, hasEntry("fruits", "apple,pear, lemon")); 80 | } 81 | 82 | @Override 83 | @Test 84 | public void testNull() throws Exception { 85 | HstoreTypeEntity entity = new HstoreTypeEntity(); 86 | 87 | em.persist(entity); 88 | 89 | HstoreTypeEntity fromDb = em.find(HstoreTypeEntity.class, entity.getId()); 90 | 91 | assertNotNull(fromDb); 92 | assertNull(fromDb.getMap()); 93 | 94 | } 95 | 96 | @Test 97 | @Sql 98 | public void testGetWithNullInMap() { 99 | HstoreTypeEntity fromDb = em.find(HstoreTypeEntity.class, 37); 100 | Map map = fromDb.getMap(); 101 | assertNotNull(map); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/InetAddressTypeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | 4 | import static org.hamcrest.Matchers.*; 5 | import static org.junit.Assert.*; 6 | 7 | import java.net.InetAddress; 8 | 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.springframework.test.context.jdbc.Sql; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | 14 | import com.github.thealchemist.pg_hibernate.spring.InetAddressEntity; 15 | import com.google.common.net.InetAddresses; 16 | 17 | /** 18 | * @author Jesse Costello-Good, The Alchemist 19 | */ 20 | @RunWith(SpringJUnit4ClassRunner.class) 21 | public class InetAddressTypeTest extends HibernateTest { 22 | 23 | @Override 24 | @Test 25 | public void testSet() throws Exception { 26 | InetAddress address = InetAddress.getByAddress(new byte[]{127, 0, 0, 1}); 27 | InetAddressEntity entity = new InetAddressEntity(); 28 | entity.setAddress(address); 29 | 30 | this.em.persist(entity); 31 | 32 | assertNotNull(entity.getId()); 33 | assertTrue(entity.getId().intValue() > 0); 34 | 35 | InetAddressEntity fromDb = this.em.find(InetAddressEntity.class, entity.getId()); 36 | 37 | assertNotNull(fromDb); 38 | assertNotNull(fromDb.getAddress()); 39 | 40 | assertEquals(address, fromDb.getAddress()); 41 | 42 | } 43 | 44 | @Test 45 | @Sql 46 | public void testGet() throws Exception { 47 | InetAddressEntity fromDb = em.find(InetAddressEntity.class, 37); 48 | InetAddress address = fromDb.getAddress(); 49 | assertThat(address.getHostAddress(), is(equalTo("192.168.1.137"))); 50 | } 51 | 52 | @Test 53 | @Sql 54 | public void testGetIpv6() throws Exception { 55 | InetAddressEntity fromDb = em.find(InetAddressEntity.class, 37); 56 | InetAddress address = fromDb.getAddress(); 57 | assertThat(address, is(equalTo(InetAddresses.forString("2001:db8::1")))); 58 | } 59 | 60 | 61 | @Override 62 | @Test 63 | public void testNull() throws Exception { 64 | InetAddressEntity entity = new InetAddressEntity(); 65 | 66 | this.em.persist(entity); 67 | InetAddressEntity fromDb = this.em.find(InetAddressEntity.class, entity.getId()); 68 | 69 | assertNotNull(fromDb); 70 | assertNull(fromDb.getAddress()); 71 | 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/IntegerArrayTypeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | 4 | import java.util.Arrays; 5 | 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.test.context.jdbc.Sql; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | 12 | import com.github.thealchemist.pg_hibernate.spring.IntegerArrayEntity; 13 | 14 | import static org.hamcrest.Matchers.*; 15 | import static org.junit.Assert.*; 16 | /** 17 | * @author Jesse Costello-Good, The Alchemist 18 | */ 19 | @RunWith(SpringJUnit4ClassRunner.class) 20 | @Sql 21 | public class IntegerArrayTypeTest extends HibernateTest { 22 | 23 | @Override 24 | @Test 25 | public void testSet() throws Exception { 26 | Integer[] values = new Integer[]{10, 22, 37}; 27 | IntegerArrayEntity entity = new IntegerArrayEntity(); 28 | entity.setIntegers(values); 29 | 30 | this.em.persist(entity); 31 | 32 | assertNotNull(entity.getId()); 33 | assertTrue(entity.getId().intValue() > 0); 34 | 35 | IntegerArrayEntity fromDb = this.em.find(IntegerArrayEntity.class, entity.getId()); 36 | 37 | assertNotNull(fromDb); 38 | assertNotNull(fromDb.getIntegers()); 39 | 40 | assertTrue(Arrays.equals(values, fromDb.getIntegers())); 41 | } 42 | 43 | @Test 44 | @Sql 45 | public void testSetViaSql() throws Exception { 46 | IntegerArrayEntity fromDb = this.em.find(IntegerArrayEntity.class, 39); 47 | 48 | assertNotNull(fromDb); 49 | assertNotNull(fromDb.getIntegers()); 50 | 51 | assertThat(fromDb.getIntegers(), is(equalTo(new Integer[]{76, -5, 1001}))); 52 | } 53 | 54 | @Test 55 | @Sql 56 | public void testGet() throws Exception { 57 | 58 | IntegerArrayEntity fromDb = this.em.find(IntegerArrayEntity.class, 37); 59 | 60 | assertNotNull(fromDb); 61 | assertThat(fromDb.getIntegers(), is(equalTo(new Integer[]{10, 22, 37}))); 62 | 63 | } 64 | 65 | @Override 66 | @Test 67 | public void testNull() throws Exception { 68 | IntegerArrayEntity entity = new IntegerArrayEntity(); 69 | 70 | this.em.persist(entity); 71 | 72 | IntegerArrayEntity fromDb = this.em.find(IntegerArrayEntity.class, entity.getId()); 73 | 74 | assertNotNull(fromDb); 75 | assertNull(fromDb.getIntegers()); 76 | 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/LineSegmentTypeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.test.context.jdbc.Sql; 6 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 7 | 8 | import static org.hamcrest.Matchers.equalTo; 9 | import static org.hamcrest.Matchers.is; 10 | import static org.junit.Assert.*; 11 | 12 | import com.github.thealchemist.pg_hibernate.spring.LineSegmentTypeEntity; 13 | import com.github.thealchemist.pg_hibernate.types.LineSegment; 14 | import com.github.thealchemist.pg_hibernate.types.Point; 15 | 16 | /** 17 | * @author Jesse Costello-Good, The Alchemist 18 | */ 19 | 20 | @RunWith(SpringJUnit4ClassRunner.class) 21 | public class LineSegmentTypeTest extends HibernateTest { 22 | 23 | private static final double X1 = 54; 24 | private static final double Y1 = 5.6667; 25 | private static final double X2 = 1.342366234; 26 | private static final double Y2 = 0.0001; 27 | 28 | @Override 29 | @Test 30 | public void testSet() throws Exception { 31 | LineSegment line = new LineSegment(new Point(X1, Y1), new Point(X2, Y2)); 32 | LineSegmentTypeEntity entity = new LineSegmentTypeEntity(); 33 | entity.setLine(line); 34 | 35 | this.em.persist(entity); 36 | 37 | assertNotNull(entity.getId()); 38 | assertTrue(entity.getId().intValue() > 0); 39 | 40 | LineSegmentTypeEntity fromDb = this.em.find(LineSegmentTypeEntity.class, entity.getId()); 41 | 42 | assertNotNull(fromDb); 43 | assertNotNull(fromDb.getLine()); 44 | 45 | assertTrue(line.equals(fromDb.getLine())); 46 | 47 | } 48 | @Test 49 | @Sql 50 | public void testGet() throws Exception { 51 | LineSegmentTypeEntity fromDb = this.em.find(LineSegmentTypeEntity.class, 37); 52 | 53 | assertNotNull(fromDb); 54 | LineSegment line = fromDb.getLine(); 55 | assertNotNull(line); 56 | assertThat(line.getP1().getX(), is(equalTo(1.0))); 57 | assertThat(line.getP1().getY(), is(equalTo(1.0))); 58 | assertThat(line.getP2().getX(), is(equalTo(5.0))); 59 | assertThat(line.getP2().getY(), is(equalTo(5.0))); 60 | } 61 | 62 | @Test 63 | @Sql 64 | public void testGetAlternateSyntax() throws Exception { 65 | LineSegmentTypeEntity fromDb = this.em.find(LineSegmentTypeEntity.class, 37); 66 | 67 | assertNotNull(fromDb); 68 | LineSegment line = fromDb.getLine(); 69 | assertNotNull(line); 70 | assertThat(line.getP1().getX(), is(equalTo(6.0))); 71 | assertThat(line.getP1().getY(), is(equalTo(7.0))); 72 | assertThat(line.getP2().getX(), is(equalTo(8.0))); 73 | assertThat(line.getP2().getY(), is(equalTo(9.0))); 74 | } 75 | 76 | @Override 77 | @Test 78 | public void testNull() throws Exception { 79 | LineSegmentTypeEntity entity = new LineSegmentTypeEntity(); 80 | 81 | this.em.persist(entity); 82 | 83 | LineSegmentTypeEntity fromDb = this.em.find(LineSegmentTypeEntity.class, entity.getId()); 84 | 85 | assertNotNull(fromDb); 86 | assertNull(fromDb.getLine()); 87 | 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/PointTypeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | 4 | import static org.hamcrest.Matchers.equalTo; 5 | import static org.hamcrest.Matchers.is; 6 | import static org.junit.Assert.*; 7 | 8 | import java.awt.geom.Point2D; 9 | 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.springframework.test.context.jdbc.Sql; 13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 14 | 15 | import com.github.thealchemist.pg_hibernate.spring.PointTypeEntity; 16 | import com.github.thealchemist.pg_hibernate.types.Point; 17 | 18 | /** 19 | * @author Jesse Costello-Good, The Alchemist 20 | */ 21 | @RunWith(SpringJUnit4ClassRunner.class) 22 | public class PointTypeTest extends HibernateTest { 23 | 24 | private static final double X = 1.342366234; 25 | private static final double Y = Math.PI; 26 | private static final double EPSILON = .00000001; 27 | 28 | @Override 29 | @Test 30 | public void testSet() throws Exception { 31 | Point point = new Point(X, Y); 32 | PointTypeEntity entity = new PointTypeEntity(); 33 | entity.setPoint(point); 34 | 35 | this.em.persist(entity); 36 | 37 | assertNotNull(entity.getId()); 38 | assertTrue(entity.getId().intValue() > 0); 39 | 40 | PointTypeEntity fromDb = this.em.find(PointTypeEntity.class, entity.getId()); 41 | 42 | assertNotNull(fromDb); 43 | assertNotNull(fromDb.getPoint()); 44 | 45 | Point2D p1 = point.asPoint2D(); 46 | Point2D p2 = fromDb.getPoint().asPoint2D(); 47 | assertTrue(p1.distance(p2) < EPSILON); 48 | 49 | } 50 | 51 | @Test 52 | @Sql 53 | public void testGet() throws Exception { 54 | PointTypeEntity fromDb = this.em.find(PointTypeEntity.class, 101); 55 | 56 | assertNotNull(fromDb); 57 | assertThat(fromDb.getPoint().getX(), is(equalTo(38.0))); 58 | assertThat(fromDb.getPoint().getY(), is(equalTo(39.0))); 59 | 60 | } 61 | @Override 62 | @Test 63 | public void testNull() throws Exception { 64 | PointTypeEntity entity = new PointTypeEntity(); 65 | 66 | this.em.persist(entity); 67 | 68 | PointTypeEntity fromDb = this.em.find(PointTypeEntity.class, entity.getId()); 69 | 70 | assertNotNull(fromDb); 71 | assertNull(fromDb.getPoint()); 72 | 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/PolygonTypeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | import static org.hamcrest.Matchers.arrayWithSize; 4 | import static org.junit.Assert.*; 5 | 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.test.context.jdbc.Sql; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | 11 | import com.github.thealchemist.pg_hibernate.spring.PolygonTypeEntity; 12 | import com.github.thealchemist.pg_hibernate.types.Point; 13 | import com.github.thealchemist.pg_hibernate.types.Polygon; 14 | 15 | 16 | /** 17 | * @author Jesse Costello-Good, The Alchemist 18 | */ 19 | @RunWith(SpringJUnit4ClassRunner.class) 20 | public class PolygonTypeTest extends HibernateTest { 21 | 22 | Point[] points = new Point[]{ 23 | new Point(1, 1), 24 | new Point(5, 8), 25 | new Point(4.25232, 9.1232837), 26 | new Point(9.123723, 4.1), 27 | new Point(0.000001, 65) 28 | }; 29 | 30 | @Override 31 | @Test 32 | public void testSet() throws Exception { 33 | Polygon polygon = new Polygon(points); 34 | PolygonTypeEntity entity = new PolygonTypeEntity(); 35 | entity.setPolygon(polygon); 36 | 37 | this.em.persist(entity); 38 | 39 | assertNotNull(entity.getId()); 40 | assertTrue(entity.getId().intValue() > 0); 41 | 42 | PolygonTypeEntity fromDb = this.em.find(PolygonTypeEntity.class, entity.getId()); 43 | 44 | assertNotNull(fromDb); 45 | assertNotNull(fromDb.getPolygon()); 46 | 47 | assertTrue(polygon.equals(fromDb.getPolygon())); 48 | 49 | } 50 | @Test 51 | @Sql 52 | public void testGet() throws Exception { 53 | PolygonTypeEntity fromDb = this.em.find(PolygonTypeEntity.class, 37); 54 | assertNotNull(fromDb); 55 | Point[] p = fromDb.getPolygon().getPoints(); 56 | assertThat(p, arrayWithSize(4)); 57 | } 58 | 59 | @Override 60 | @Test 61 | public void testNull() throws Exception { 62 | PolygonTypeEntity entity = new PolygonTypeEntity(); 63 | 64 | this.em.persist(entity); 65 | 66 | PolygonTypeEntity fromDb = this.em.find(PolygonTypeEntity.class, entity.getId()); 67 | 68 | assertNotNull(fromDb); 69 | assertNull(fromDb.getPolygon()); 70 | 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/StringArrayTypeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate; 2 | 3 | 4 | import java.util.Arrays; 5 | 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.test.context.jdbc.Sql; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | 11 | import com.github.thealchemist.pg_hibernate.spring.StringArrayEntity; 12 | 13 | import static org.hamcrest.Matchers.*; 14 | import static org.junit.Assert.*; 15 | /** 16 | * @author Jesse Costello-Good, The Alchemist 17 | */ 18 | @RunWith(SpringJUnit4ClassRunner.class) 19 | public class StringArrayTypeTest extends HibernateTest { 20 | 21 | @Override 22 | @Test 23 | public void testSet() throws Exception { 24 | String[] values = new String[]{"a", "z", "mm"}; 25 | StringArrayEntity entity = new StringArrayEntity(); 26 | entity.setStrings(values); 27 | 28 | this.em.persist(entity); 29 | 30 | assertNotNull(entity.getId()); 31 | assertTrue(entity.getId().intValue() > 0); 32 | 33 | StringArrayEntity fromDb = this.em.find(StringArrayEntity.class, entity.getId()); 34 | 35 | assertNotNull(fromDb); 36 | assertNotNull(fromDb.getStrings()); 37 | 38 | assertTrue(Arrays.equals(values, fromDb.getStrings())); 39 | 40 | } 41 | 42 | @Test 43 | @Sql 44 | public void testGet() throws Exception { 45 | 46 | StringArrayEntity fromDb = this.em.find(StringArrayEntity.class, 37); 47 | 48 | assertNotNull(fromDb); 49 | assertThat(fromDb.getStrings(), is(equalTo(new String[]{"a", "b", "c"}))); 50 | 51 | } 52 | 53 | @Override 54 | @Test 55 | public void testNull() throws Exception { 56 | StringArrayEntity entity = new StringArrayEntity(); 57 | 58 | this.em.persist(entity); 59 | 60 | StringArrayEntity fromDb = this.em.find(StringArrayEntity.class, entity.getId()); 61 | 62 | assertNotNull(fromDb); 63 | assertNull(fromDb.getStrings()); 64 | 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/spring/BoxTypeEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.spring; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | 9 | import org.hibernate.annotations.Type; 10 | 11 | import com.github.thealchemist.pg_hibernate.types.Box; 12 | 13 | @Entity 14 | public class BoxTypeEntity { 15 | 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.AUTO) 18 | private Integer id; 19 | @Column(name="my_box") 20 | @Type(type="box") 21 | private Box myBox; 22 | 23 | public Integer getId() { 24 | return id; 25 | } 26 | 27 | public void setId( Integer id ) { 28 | this.id = id; 29 | } 30 | 31 | public Box getBox() { 32 | return myBox; 33 | } 34 | 35 | public void setBox( Box box ) { 36 | this.myBox = box; 37 | } 38 | } -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/spring/CircleTypeEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.spring; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | 9 | import org.hibernate.annotations.Type; 10 | 11 | import com.github.thealchemist.pg_hibernate.types.Circle; 12 | 13 | @Entity 14 | public class CircleTypeEntity { 15 | 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.AUTO) 18 | private Integer id; 19 | @Column(name="my_circle") 20 | @Type(type="circle") 21 | private Circle myCircle; 22 | 23 | public Integer getId() { 24 | return id; 25 | } 26 | 27 | public void setId( Integer id ) { 28 | this.id = id; 29 | } 30 | 31 | public Circle getCircle() { 32 | return myCircle; 33 | } 34 | 35 | public void setCircle( Circle circle ) { 36 | this.myCircle = circle; 37 | } 38 | } -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/spring/HstoreTypeEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.spring; 2 | 3 | import java.util.Map; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.GenerationType; 8 | import javax.persistence.Id; 9 | 10 | import org.hibernate.annotations.Type; 11 | 12 | @Entity 13 | public class HstoreTypeEntity { 14 | 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.AUTO) 17 | private Integer id; 18 | @Type(type="hstore") 19 | private Map map; 20 | 21 | public Integer getId() { 22 | return id; 23 | } 24 | 25 | public void setId( Integer id ) { 26 | this.id = id; 27 | } 28 | 29 | public Map getMap() { 30 | return map; 31 | } 32 | 33 | public void setMap( Map map ) { 34 | this.map = map; 35 | } 36 | } -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/spring/InetAddressEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.spring; 2 | 3 | import java.net.InetAddress; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.GenerationType; 8 | import javax.persistence.Id; 9 | 10 | import org.hibernate.annotations.Type; 11 | 12 | @Entity 13 | public class InetAddressEntity { 14 | 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.AUTO) 17 | private Integer id; 18 | @Type(type="inet") 19 | private InetAddress address; 20 | 21 | public Integer getId() { 22 | return id; 23 | } 24 | 25 | public void setId( Integer id ) { 26 | this.id = id; 27 | } 28 | 29 | public InetAddress getAddress() { 30 | return address; 31 | } 32 | 33 | public void setAddress( InetAddress address ) { 34 | this.address = address; 35 | } 36 | } -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/spring/IntegerArrayEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.spring; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | 8 | import org.hibernate.annotations.Type; 9 | 10 | @Entity 11 | public class IntegerArrayEntity { 12 | 13 | @Id 14 | @GeneratedValue(strategy = GenerationType.IDENTITY) 15 | private Integer id; 16 | @Type(type="intarray") 17 | private Integer[] integers; 18 | 19 | public Integer getId() { 20 | return id; 21 | } 22 | 23 | public void setId( Integer id ) { 24 | this.id = id; 25 | } 26 | 27 | public Integer[] getIntegers() { 28 | return integers; 29 | } 30 | 31 | public void setIntegers( Integer[] strings ) { 32 | this.integers = strings; 33 | } 34 | } -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/spring/LineSegmentTypeEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.spring; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | 8 | import org.hibernate.annotations.Type; 9 | 10 | import com.github.thealchemist.pg_hibernate.types.LineSegment; 11 | 12 | @Entity 13 | public class LineSegmentTypeEntity { 14 | 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.AUTO) 17 | private Integer id; 18 | @Type(type="lseg") 19 | private LineSegment line; 20 | 21 | public Integer getId() { 22 | return id; 23 | } 24 | 25 | public void setId( Integer id ) { 26 | this.id = id; 27 | } 28 | 29 | public LineSegment getLine() { 30 | return line; 31 | } 32 | 33 | public void setLine( LineSegment line ) { 34 | this.line = line; 35 | } 36 | } -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/spring/Marker.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.spring; 2 | 3 | public abstract class Marker { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/spring/PersistenceJPAConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.spring; 2 | 3 | 4 | import java.util.Properties; 5 | 6 | import javax.annotation.PostConstruct; 7 | import javax.inject.Inject; 8 | import javax.persistence.EntityManagerFactory; 9 | import javax.sql.DataSource; 10 | 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.context.annotation.PropertySource; 14 | import org.springframework.core.env.Environment; 15 | import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; 16 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 17 | import org.springframework.jdbc.core.JdbcTemplate; 18 | import org.springframework.jdbc.core.simple.SimpleJdbcInsert; 19 | import org.springframework.jdbc.datasource.DriverManagerDataSource; 20 | import org.springframework.orm.jpa.JpaTransactionManager; 21 | import org.springframework.orm.jpa.JpaVendorAdapter; 22 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 23 | import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; 24 | import org.springframework.transaction.PlatformTransactionManager; 25 | import org.springframework.transaction.annotation.EnableTransactionManagement; 26 | 27 | import com.google.common.collect.ImmutableMap; 28 | import com.google.common.collect.ImmutableSet; 29 | 30 | /** 31 | * Inspiration from http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/#javaconfig 32 | * 33 | * @author kpietrzak 34 | */ 35 | @Configuration 36 | @EnableTransactionManagement 37 | @EnableJpaRepositories(basePackageClasses=Marker.class) 38 | @PropertySource("classpath:/database.properties") 39 | public class PersistenceJPAConfig { 40 | 41 | @Inject 42 | private Environment env; 43 | 44 | private String driverClassName; 45 | private String username; 46 | private String password; 47 | private String url; 48 | 49 | @PostConstruct 50 | private void initDatabaseProperties() { 51 | driverClassName = env.getProperty("database.driverClassName"); 52 | username = env.getProperty("database.username"); 53 | password = env.getProperty("database.password"); 54 | url = env.getProperty("database.url"); 55 | } 56 | 57 | @Bean 58 | public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 59 | LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); 60 | em.setDataSource(dataSource()); 61 | em.setPackagesToScan(new String[] { Marker.class.getPackage().getName() }); 62 | 63 | JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 64 | em.setJpaVendorAdapter(vendorAdapter); 65 | em.setJpaProperties(additionalProperties()); 66 | 67 | return em; 68 | } 69 | 70 | @Bean 71 | public DataSource dataSource() { 72 | DriverManagerDataSource dataSource = new DriverManagerDataSource(); 73 | 74 | 75 | dataSource.setDriverClassName(driverClassName); 76 | dataSource.setUrl(url); 77 | 78 | dataSource.setUsername(username); 79 | dataSource.setPassword(password); 80 | 81 | return dataSource; 82 | } 83 | 84 | @Bean 85 | public JdbcTemplate jdbcTemplate() { 86 | return new JdbcTemplate(dataSource()); 87 | } 88 | 89 | @Bean 90 | public SimpleJdbcInsert simpleJdbcInsert() { 91 | return new SimpleJdbcInsert(dataSource()); 92 | } 93 | 94 | @Bean 95 | public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { 96 | JpaTransactionManager transactionManager = new JpaTransactionManager(); 97 | transactionManager.setEntityManagerFactory(emf); 98 | 99 | return transactionManager; 100 | } 101 | 102 | @Bean 103 | public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { 104 | return new PersistenceExceptionTranslationPostProcessor(); 105 | } 106 | 107 | Properties additionalProperties() { 108 | Properties properties = new Properties(); 109 | ImmutableSet keys = ImmutableSet.of("hibernate.hbm2ddl.auto", "hibernate.dialect", "hibernate.show_sql", "hibernate.format_sql"); 110 | for(String key : keys) { 111 | properties.setProperty(key, env.getProperty(key)); 112 | } 113 | return properties; 114 | } 115 | } -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/spring/PointTypeEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.spring; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | 9 | import org.hibernate.annotations.Type; 10 | 11 | import com.github.thealchemist.pg_hibernate.types.Point; 12 | 13 | @Entity 14 | public class PointTypeEntity { 15 | 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.AUTO) 18 | private Integer id; 19 | @Column(name="my_Point") 20 | @Type(type="point") 21 | private Point point; 22 | 23 | public Integer getId() { 24 | return id; 25 | } 26 | 27 | public void setId( Integer id ) { 28 | this.id = id; 29 | } 30 | 31 | public Point getPoint() { 32 | return point; 33 | } 34 | 35 | public void setPoint( Point point ) { 36 | this.point = point; 37 | } 38 | } -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/spring/PolygonTypeEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.spring; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | 9 | import org.hibernate.annotations.Type; 10 | 11 | import com.github.thealchemist.pg_hibernate.types.Polygon; 12 | 13 | @Entity 14 | public class PolygonTypeEntity { 15 | 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.AUTO) 18 | private Integer id; 19 | @Column(name="my_polygon") 20 | @Type(type="polygon") 21 | private Polygon myPolygon; 22 | 23 | public Integer getId() { 24 | return id; 25 | } 26 | 27 | public void setId( Integer id ) { 28 | this.id = id; 29 | } 30 | 31 | public Polygon getPolygon() { 32 | return myPolygon; 33 | } 34 | 35 | public void setPolygon( Polygon polygon ) { 36 | this.myPolygon = polygon; 37 | } 38 | } -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/spring/StringArrayEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.thealchemist.pg_hibernate.spring; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | 8 | import org.hibernate.annotations.Type; 9 | 10 | @Entity 11 | public class StringArrayEntity { 12 | 13 | @Id 14 | @GeneratedValue(strategy = GenerationType.AUTO) 15 | private Integer id; 16 | @Type(type="stringarray") 17 | private String[] strings; 18 | 19 | public Integer getId() { 20 | return id; 21 | } 22 | 23 | public void setId( Integer id ) { 24 | this.id = id; 25 | } 26 | 27 | public String[] getStrings() { 28 | return strings; 29 | } 30 | 31 | public void setStrings( String[] strings ) { 32 | this.strings = strings; 33 | } 34 | } -------------------------------------------------------------------------------- /src/test/java/com/github/thealchemist/pg_hibernate/spring/package-info.java: -------------------------------------------------------------------------------- 1 | @TypeDefs( 2 | { 3 | @TypeDef(name = "circle", typeClass = com.github.thealchemist.pg_hibernate.CircleType.class), 4 | @TypeDef(name = "hstore", typeClass = com.github.thealchemist.pg_hibernate.HstoreType.class), 5 | @TypeDef(name = "lseg", typeClass = com.github.thealchemist.pg_hibernate.LineSegmentType.class), 6 | @TypeDef(name = "point", typeClass = com.github.thealchemist.pg_hibernate.PointType.class), 7 | @TypeDef(name = "box", typeClass = com.github.thealchemist.pg_hibernate.BoxType.class), 8 | @TypeDef(name = "polygon", typeClass = com.github.thealchemist.pg_hibernate.PolygonType.class), 9 | @TypeDef(name = "stringarray", typeClass = com.github.thealchemist.pg_hibernate.StringArrayType.class), 10 | @TypeDef(name = "inet", typeClass = com.github.thealchemist.pg_hibernate.InetAddressType.class), 11 | @TypeDef(name = "intarray", typeClass = com.github.thealchemist.pg_hibernate.IntegerArrayType.class), 12 | @TypeDef(name = "xml", typeClass = com.github.thealchemist.pg_hibernate.XMLType.class) 13 | }) 14 | 15 | package com.github.thealchemist.pg_hibernate.spring; 16 | 17 | import org.hibernate.annotations.TypeDef; 18 | import org.hibernate.annotations.TypeDefs; 19 | 20 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/BoxTypeTest.testGet.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE BoxTypeEntity ( 2 | id serial not null, 3 | my_box box, 4 | primary key (id) 5 | ); 6 | 7 | insert into BoxTypeEntity (id, my_box) VALUES (37, '(1, 1), (5,5)'); -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/CircleTypeTest.testGet.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE CircleTypeEntity ( 2 | id serial not null, 3 | my_circle circle, 4 | primary key (id) 5 | ); 6 | 7 | insert into CircleTypeEntity (id, my_circle) VALUES (37, '( (1, 1), 37 )') 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/HstoreTypeTest.testGet.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE HstoreTypeEntity ( 2 | id serial not null, 3 | map hstore, 4 | primary key (id) 5 | ); 6 | 7 | insert into HstoreTypeEntity (id, map) VALUES (37, '"name" => "kp","fruits" => "apple,pear, lemon"') 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/HstoreTypeTest.testGetWithNullInMap.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE HstoreTypeEntity ( 2 | id serial not null, 3 | map hstore, 4 | primary key (id) 5 | ); 6 | 7 | insert into HstoreTypeEntity (id, map) VALUES (37, null) 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/HstoreTypeTest.testSetWithNewMap.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE HstoreTypeEntity ( 2 | id serial not null, 3 | map hstore, 4 | primary key (id) 5 | ); 6 | 7 | insert into HstoreTypeEntity (id, map) VALUES (37, '"name" => "kp"') 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/InetAddressTypeTest.testGet.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE InetAddressEntity ( 2 | id serial not null, 3 | address inet, 4 | primary key (id) 5 | ); 6 | 7 | insert into InetAddressEntity (id, address) VALUES (37, inet '192.168.1.137') 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/InetAddressTypeTest.testGetIpv6.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE InetAddressEntity ( 2 | id serial not null, 3 | address inet, 4 | primary key (id) 5 | ); 6 | 7 | insert into InetAddressEntity (id, address) VALUES (37, inet '2001:db8::1') 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/IntArrayTypeTest.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS IntArrayEntity; 2 | 3 | CREATE TABLE IntArrayEntity ( 4 | id serial not null, 5 | integers int[], 6 | primary key (id) 7 | ); 8 | 9 | insert into IntArrayEntity (id, integers) VALUES (37, '{10, 22, 37}') 10 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/IntArrayTypeTest.testGet.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IntArrayEntity ( 2 | id serial not null, 3 | integers int[], 4 | primary key (id) 5 | ); 6 | 7 | insert into IntArrayEntity (id, integers) VALUES (37, '{10, 22, 37}') 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/IntArrayTypeTest.testSetViaSql.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IntArrayEntity ( 2 | id serial not null, 3 | integers int[], 4 | primary key (id) 5 | ) WITHOUT OIDS; 6 | 7 | insert into IntArrayEntity (id, integers) VALUES (39, '{76, -5, 1001}') 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/IntegerArrayTypeTest.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS IntegerArrayEntity; 2 | 3 | CREATE TABLE IntegerArrayEntity ( 4 | id serial not null, 5 | integers int[], 6 | primary key (id) 7 | ); 8 | 9 | insert into IntegerArrayEntity (id, integers) VALUES (37, '{10, 22, 37}') 10 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/IntegerArrayTypeTest.testGet.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IntegerArrayEntity ( 2 | id serial not null, 3 | integers int[], 4 | primary key (id) 5 | ); 6 | 7 | insert into IntegerArrayEntity (id, integers) VALUES (37, '{10, 22, 37}') 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/IntegerArrayTypeTest.testSetViaSql.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IntegerArrayEntity ( 2 | id serial not null, 3 | integers int[], 4 | primary key (id) 5 | ) ; 6 | 7 | insert into IntegerArrayEntity (id, integers) VALUES (39, '{76, -5, 1001}') 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/LineSegmentTypeTest.testGet.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE LineSegmentTypeEntity ( 2 | id serial not null, 3 | line lseg, 4 | primary key (id) 5 | ); 6 | 7 | insert into LineSegmentTypeEntity (id, line) VALUES (37, '(1, 1), (5, 5)'); 8 | insert into LineSegmentTypeEntity (id, line) VALUES (38, '[ (6, 7), (8, 9) ]'); 9 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/LineSegmentTypeTest.testGetAlternateSyntax.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE LineSegmentTypeEntity ( 2 | id serial not null, 3 | line lseg, 4 | primary key (id) 5 | ); 6 | 7 | insert into LineSegmentTypeEntity (id, line) VALUES (37, '[ (6, 7), (8, 9) ]'); 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/PointTypeTest.testGet.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE PointTypeEntity ( 2 | id serial not null, 3 | my_point Point, 4 | primary key (id) 5 | ); 6 | 7 | insert into PointTypeEntity (id, my_Point) VALUES (101, '(38, 39.0)') 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/PolygonTypeTest.testGet.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE PolygonTypeEntity ( 2 | id serial not null, 3 | my_polygon Polygon, 4 | primary key (id) 5 | ); 6 | 7 | insert into PolygonTypeEntity (id, my_polygon) VALUES (37, '( (0,0), (0, 1), (1,1), (1, 0) )') 8 | -------------------------------------------------------------------------------- /src/test/resources/com/github/thealchemist/pg_hibernate/StringArrayTypeTest.testGet.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE StringArrayEntity ( 2 | id serial not null, 3 | strings text[], 4 | primary key (id) 5 | ); 6 | 7 | insert into StringArrayEntity (id, strings) VALUES (37, '{"a", "b", "c"}') 8 | -------------------------------------------------------------------------------- /src/test/resources/database.properties: -------------------------------------------------------------------------------- 1 | database.driverClassName=org.postgresql.Driver 2 | database.url=jdbc:postgresql://localhost:5432/pg_hibernate?ApplicationName=maven 3 | database.username=postgres 4 | database.password= 5 | database.targetSchema=public 6 | hibernate.format_sql=true 7 | hibernate.show_sql=true 8 | hibernate.dialect=org.hibernate.dialect.PostgreSQL82Dialect 9 | hibernate.hbm2ddl.auto= 10 | 11 | -------------------------------------------------------------------------------- /src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/test/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS PointTypeEntity; 2 | DROP TABLE IF EXISTS LineSegmentTypeEntity; 3 | DROP TABLE IF EXISTS CircleTypeEntity; 4 | DROP TABLE IF EXISTS RectangleTypeEntity; 5 | DROP TABLE IF EXISTS PolygonTypeEntity; 6 | DROP TABLE IF EXISTS Inet4AddressTypeEntity; 7 | DROP TABLE IF EXISTS NumericTypeEntity; 8 | DROP TABLE IF EXISTS StringArrayTypeEntity; 9 | drop sequence IF EXISTS hibernate_sequence; 10 | 11 | 12 | create sequence hibernate_sequence; 13 | create extension if not exists hstore; --------------------------------------------------------------------------------