Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for hibernate 5 #12

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions .rultor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ merge:
sudo sh -c 'echo "host all all localhost trust" > /etc/postgresql/9.3/main/pg_hba.conf'
sudo sh -c 'echo "local all all trust" >> /etc/postgresql/9.3/main/pg_hba.conf'
sudo service postgresql start
sleep 30
sleep 30s
psql -c 'create database nativejson;' -U postgres
mvn install -Pci -B --settings ../settings.xml

Expand All @@ -40,13 +40,7 @@ env:

release:
script: |-
sudo apt-get update -y
sudo apt-get install -y postgresql-9.3
sudo sh -c 'echo "host all all localhost trust" > /etc/postgresql/9.3/main/pg_hba.conf'
sudo sh -c 'echo "local all all trust" >> /etc/postgresql/9.3/main/pg_hba.conf'
sudo service postgresql start
sleep 30
psql -c 'create database nativejson;' -U postgres
./initialize_postgres.sh
mvn versions:set "-DnewVersion=${tag}"
git commit -am "${tag}"
mvn deploy -Pci -B -Prelease --settings ../settings.xml -Dgpg.homedir=..
Expand Down
15 changes: 15 additions & 0 deletions initialize_postgres.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
# install postgresql, wait for it to start and then create test database

set -x

sudo apt-get update -y
sudo apt-get install -y postgresql-9.3
sudo sh -c 'echo "host all all localhost trust" > /etc/postgresql/9.3/main/pg_hba.conf'
sudo sh -c 'echo "local all all trust" >> /etc/postgresql/9.3/main/pg_hba.conf'
sudo service postgresql start
until runuser -l postgres -c 'pg_isready' 2>/dev/null; do
>&2 echo "Postgres is unavailable - sleeping for 2 seconds"
sleep 2s
done
psql -c 'create database nativejson;' -U postgres
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.21.Final</version>
<version>5.2.16.Final</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,58 @@
*/
package com.marvinformatics.hibernate.json;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.hibernate.HibernateException;
import org.hibernate.collection.internal.PersistentList;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.usertype.UserCollectionType;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
* @author Marvin H Froeder
*/
public class JsonListUserType extends JsonUserType implements UserCollectionType {



@Override
public JavaType createJavaType(ObjectMapper mapper) {
return mapper.getTypeFactory().constructCollectionType(List.class, returnedClass());
}

@Override
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister)
throws HibernateException {
return new PersistentList(session);
}

@Override
public PersistentCollection instantiate(SharedSessionContractImplementor sharedSessionContractImplementor,
CollectionPersister collectionPersister) throws HibernateException {
return new PersistentList(sharedSessionContractImplementor);
}


private PersistentList cast(Object collection) {
return (PersistentList) collection;
}

@Override
public PersistentCollection wrap(SessionImplementor session, Object collection) {
return new PersistentList(session, (List<?>) collection);
}

@Override
public PersistentCollection wrap(SharedSessionContractImplementor sharedSessionContractImplementor,
Object collection) {
return new PersistentList(sharedSessionContractImplementor, (List<?>) collection);
}

@Override
public Iterator<?> getElementsIterator(Object collection) {
return cast(collection).iterator();
Expand All @@ -69,9 +82,13 @@ public Object indexOf(Object collection, Object entity) {
return cast(collection).indexOf(entity);
}

@Override
public Object replaceElements(Object original, Object target, CollectionPersister persister, Object owner,
@SuppressWarnings("rawtypes") Map copyCache, SessionImplementor session) throws HibernateException {
public Object replaceElements(
Object original,
Object target,
CollectionPersister persister,
Object owner,
@SuppressWarnings("rawtypes") Map copyCache,
SessionImplementor session) throws HibernateException {

PersistentList originalList = cast(original);
PersistentList targetList = cast(target);
Expand All @@ -81,6 +98,23 @@ public Object replaceElements(Object original, Object target, CollectionPersiste
return target;
}

@Override
public Object replaceElements(
Object original,
Object target,
CollectionPersister persister,
Object owner,
@SuppressWarnings("rawtypes") Map copyCache,
SharedSessionContractImplementor session) throws HibernateException {

PersistentList originalList = cast(original);
PersistentList targetList = cast(target);
targetList.clear();
targetList.addAll(originalList);

return target;
}

@Override
public Object instantiate(int anticipatedSize) {
return new PersistentList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
*/
package com.marvinformatics.hibernate.json;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.hibernate.HibernateException;
import org.hibernate.collection.internal.PersistentSet;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.usertype.UserCollectionType;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class JsonSetUserType extends JsonUserType implements UserCollectionType {

public JsonSetUserType() {
}

Expand All @@ -39,6 +42,12 @@ public PersistentCollection instantiate(SessionImplementor session, CollectionPe
return new PersistentSet(session);
}

@Override
public PersistentCollection instantiate(SharedSessionContractImplementor sharedSessionContractImplementor,
CollectionPersister collectionPersister) throws HibernateException {
return new PersistentSet(sharedSessionContractImplementor);
}

private PersistentSet cast(Object collection) {
return (PersistentSet) collection;
}
Expand All @@ -47,6 +56,12 @@ public PersistentCollection wrap(SessionImplementor session, Object collection)
return new PersistentSet(session, (Set<?>) collection);
}

@Override
public PersistentCollection wrap(SharedSessionContractImplementor sharedSessionContractImplementor,
Object collection) {
return new PersistentSet(sharedSessionContractImplementor, (Set<?>) collection);
}

public Iterator<?> getElementsIterator(Object collection) {
return this.cast(collection).iterator();
}
Expand All @@ -72,6 +87,21 @@ public Object replaceElements(Object original,
return target;
}

@Override
public Object replaceElements(
Object original,
Object target,
CollectionPersister persister,
Object owner,
Map copyCache,
SharedSessionContractImplementor session) throws HibernateException {
PersistentSet originalSet = this.cast(original);
PersistentSet targetSet = this.cast(target);
targetSet.clear();
targetSet.addAll(originalSet);
return target;
}

public Object instantiate(int anticipatedSize) {
return new PersistentSet();
}
Expand Down
32 changes: 19 additions & 13 deletions src/main/java/com/marvinformatics/hibernate/json/JsonUserType.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
*/
package com.marvinformatics.hibernate.json;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.SimpleType;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.DynamicParameterizedType;
import org.hibernate.usertype.UserType;
import org.postgresql.util.PGobject;

import java.io.IOException;
import java.io.Serializable;
import java.sql.PreparedStatement;
Expand All @@ -23,17 +34,6 @@
import java.sql.Types;
import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.usertype.DynamicParameterizedType;
import org.hibernate.usertype.UserType;
import org.postgresql.util.PGobject;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.SimpleType;

/**
* Define a Jackson Serializer/Deserializer use to persist
*
Expand Down Expand Up @@ -68,7 +68,6 @@ public boolean isMutable() {
return true;
}

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this method is no longer part of the implementation, I think it's safe to take it out.

PGobject dataObject = new PGobject();
dataObject.setType("json");
Expand All @@ -79,7 +78,14 @@ public void nullSafeSet(PreparedStatement st, Object value, int index, SessionIm
st.setObject(index, dataObject);
}

@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
return nullSafeGet(rs, names, (SessionImplementor) session, owner);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this cast is either safe nor necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right. I blindly copied the fix from my other project, which has jsonb working with hibernate using sprint boot 2.0.1. ill update this.

}

public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
nullSafeSet(st, value, index, (SessionImplementor) session);
}

public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
Object result = rs.getObject(names[0]);
if (result instanceof PGobject)
Expand Down