├── src
├── test
│ ├── java
│ │ └── com
│ │ │ └── github
│ │ │ └── thealchemist
│ │ │ └── pg_hibernate
│ │ │ ├── spring
│ │ │ ├── Marker.java
│ │ │ ├── StringArrayEntity.java
│ │ │ ├── IntegerArrayEntity.java
│ │ │ ├── HstoreTypeEntity.java
│ │ │ ├── InetAddressEntity.java
│ │ │ ├── LineSegmentTypeEntity.java
│ │ │ ├── BoxTypeEntity.java
│ │ │ ├── PointTypeEntity.java
│ │ │ ├── CircleTypeEntity.java
│ │ │ ├── PolygonTypeEntity.java
│ │ │ ├── package-info.java
│ │ │ └── PersistenceJPAConfig.java
│ │ │ ├── StringArrayTypeTest.java
│ │ │ ├── CircleTypeTest.java
│ │ │ ├── PolygonTypeTest.java
│ │ │ ├── PointTypeTest.java
│ │ │ ├── InetAddressTypeTest.java
│ │ │ ├── IntegerArrayTypeTest.java
│ │ │ ├── BoxTypeTest.java
│ │ │ ├── HibernateTest.java
│ │ │ ├── LineSegmentTypeTest.java
│ │ │ └── HstoreTypeTest.java
│ └── resources
│ │ ├── com
│ │ └── github
│ │ │ └── thealchemist
│ │ │ └── pg_hibernate
│ │ │ ├── BoxTypeTest.testGet.sql
│ │ │ ├── IntArrayTypeTest.testGet.sql
│ │ │ ├── HstoreTypeTest.testGetWithNullInMap.sql
│ │ │ ├── PointTypeTest.testGet.sql
│ │ │ ├── CircleTypeTest.testGet.sql
│ │ │ ├── HstoreTypeTest.testSetWithNewMap.sql
│ │ │ ├── IntegerArrayTypeTest.testGet.sql
│ │ │ ├── InetAddressTypeTest.testGet.sql
│ │ │ ├── IntArrayTypeTest.testSetViaSql.sql
│ │ │ ├── IntegerArrayTypeTest.testSetViaSql.sql
│ │ │ ├── StringArrayTypeTest.testGet.sql
│ │ │ ├── InetAddressTypeTest.testGetIpv6.sql
│ │ │ ├── HstoreTypeTest.testGet.sql
│ │ │ ├── LineSegmentTypeTest.testGetAlternateSyntax.sql
│ │ │ ├── PolygonTypeTest.testGet.sql
│ │ │ ├── IntArrayTypeTest.sql
│ │ │ ├── IntegerArrayTypeTest.sql
│ │ │ └── LineSegmentTypeTest.testGet.sql
│ │ ├── database.properties
│ │ ├── logback.xml
│ │ └── schema.sql
└── main
│ └── java
│ └── com
│ └── github
│ └── thealchemist
│ └── pg_hibernate
│ ├── types
│ ├── Point.java
│ ├── LineSegment.java
│ ├── Box.java
│ ├── Circle.java
│ └── Polygon.java
│ ├── XMLType.java
│ ├── PointType.java
│ ├── HstoreType.java
│ ├── InetAddressType.java
│ ├── LineSegmentType.java
│ ├── BoxType.java
│ ├── StringArrayType.java
│ ├── PolygonType.java
│ ├── CircleType.java
│ └── IntegerArrayType.java
├── .gitignore
├── .travis.yml
├── README.md
├── LICENSE
└── pom.xml
/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 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/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/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/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/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/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.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/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/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/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.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/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/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/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/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/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/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/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/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 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/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;
--------------------------------------------------------------------------------
/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/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/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/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/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/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/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/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/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/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/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/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/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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/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/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/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/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/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/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/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/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/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/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/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