Java Code Examples for org.hibernate.engine.spi.SharedSessionContractImplementor

The following are top voted examples for showing how to use org.hibernate.engine.spi.SharedSessionContractImplementor. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: MMORPG_Prototype   File: JsonUserType.java   View source code 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
        throws HibernateException, SQLException
{
    final String cellContent = rs.getString(names[0]);
    if (cellContent == null)
        throw new RuntimeException("cell Content is null");

    final ObjectMapper mapper = SpringContext.getBean(ObjectMapper.class);
    try
    {
        return mapper.readValue(cellContent, returnedClass());
    } catch (final IOException ex)
    {
        throw new RuntimeException("Failed to convert String to Invoice: " + ex.getMessage(), ex);
    }

}
 
Example 2
Project: MMORPG_Prototype   File: JsonUserType.java   View source code 6 votes vote down vote up
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
        throws HibernateException, SQLException
{
    if (value == null)
    {
        st.setNull(index, Types.VARCHAR);
        return;
    }
    try
    {
        final ObjectMapper mapper = new ObjectMapper();
        final StringWriter w = new StringWriter();
        mapper.writeValue(w, value);
        w.flush();
        st.setObject(index, w.toString(), Types.VARCHAR);
    } catch (final Exception ex)
    {
        throw new RuntimeException("Failed to convert Invoice to String: " + ex.getMessage(), ex);
    }

}
 
Example 3
Project: SqlSauce   File: ArrayListLongUserType.java   View source code 6 votes vote down vote up
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
        throws HibernateException, SQLException {
    Array array = rs.getArray(names[0]);
    if (array == null) {
        return null;
    }
    Long[] javaArray = (Long[]) array.getArray();
    ArrayList<Long> result = new ArrayList<>();
    Collections.addAll(result, javaArray); //do not use Arrays.asList(), that method returns a fake ArrayList
    return result;
}
 
Example 4
Project: SqlSauce   File: ArrayListLongUserType.java   View source code 6 votes vote down vote up
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
        throws HibernateException, SQLException {
    Connection connection = st.getConnection();

    if (value == null) {
        st.setNull(index, sqlTypes()[0]);
    } else {
        @SuppressWarnings("unchecked") ArrayList<Long> castObject = (ArrayList) value;

        Long[] longs = castObject.toArray(new Long[castObject.size()]);
        Array array = connection.createArrayOf("bigint", longs);

        st.setArray(index, array);
    }
}
 
Example 5
Project: flow-platform   File: CredentialDetailAdaptor.java   View source code 6 votes vote down vote up
@Override
public Object nullSafeGet(ResultSet rs,
                          String[] names,
                          SharedSessionContractImplementor session,
                          Object owner) throws HibernateException, SQLException {

    String detailRaw = rs.getString(names[0]);
    if (detailRaw == null) {
        return null;
    }

    // according to credential.hbm.xml property sequence
    String typeRaw = rs.getString(2);
    CredentialType type = CredentialType.valueOf(typeRaw);

    return GSON.fromJson(detailRaw, type.getClazz());
}
 
Example 6
Project: flow-platform   File: ByteAdaptor.java   View source code 6 votes vote down vote up
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
    throws HibernateException, SQLException {
    // set to null
    if (value == null) {
        st.setBytes(index, null);
        return;
    }

    // value already in string type
    if (value instanceof byte[]) {
        st.setBytes(index, (byte[]) value);
        return;
    }

    byte[] bytes = stringToByte((String) value);
    st.setBytes(index, bytes);

}
 
Example 7
Project: flow-platform   File: BaseAdaptor.java   View source code 6 votes vote down vote up
/**
 * @param st
 * @param value
 * @param index
 * @param session
 * @throws HibernateException
 * @throws SQLException
 */
@Override
public void nullSafeSet(PreparedStatement st,
    Object value,
    int index,
    SharedSessionContractImplementor session) throws HibernateException, SQLException {

    // set to null
    if (value == null) {
        st.setString(index, null);
        return;
    }

    // value already in string type
    if (value instanceof String) {
        st.setString(index, (String) value);
        return;
    }

    String str = objectToJson(value);
    st.setString(index, str);
}
 
Example 8
Project: hibernate-single-table-bulk-id-strategy   File: SingleGlobalTemporaryTableBulkIdStrategy.java   View source code 6 votes vote down vote up
private void cleanUpRows(SharedSessionContractImplementor session, Queryable persister) {
    final String sql = "delete from " + fullyQualifiedTableName + " where " + discriminatorColumn + "=?";
    PreparedStatement ps = null;
    try {
        ps = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql, false);
        ps.setString(1, generateDiscriminatorValue(persister));
        StringType.INSTANCE.set(ps, generateDiscriminatorValue(persister), 1, session);
        session.getJdbcCoordinator().getResultSetReturn().executeUpdate(ps);
    } catch (SQLException e) {
        throw session.getJdbcServices().getSqlExceptionHelper().convert(e, "Unable to clean up id table [" + fullyQualifiedTableName + "]", sql);
    } finally {
        if (ps != null) {
            session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(ps);
        }
    }
}
 
Example 9
Project: dhis2-core   File: JsonListBinaryType.java   View source code 6 votes vote down vote up
@Override
public void nullSafeSet( PreparedStatement ps, Object value, int idx, SharedSessionContractImplementor session )
    throws HibernateException,
    SQLException
{
    if ( value == null )
    {
        ps.setObject( idx, null );
        return;
    }

    PGobject pg = new PGobject();
    pg.setType( "jsonb" );
    pg.setValue( convertObjectToJson( value ) );

    ps.setObject( idx, pg );
}
 
Example 10
Project: owsi-core-parent   File: StringClobType.java   View source code 6 votes vote down vote up
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
	Reader reader = rs.getCharacterStream( names[0] );
	if ( reader == null ) return null;
	StringBuilder result = new StringBuilder( 4096 );
	try {
		char[] charbuf = new char[4096];
		for ( int i = reader.read( charbuf ); i > 0 ; i = reader.read( charbuf ) ) {
			result.append( charbuf, 0, i );
		}
	}
	catch (IOException e) {
		throw new SQLException( e.getMessage() );
	}
	return result.toString();
}
 
Example 11
Project: webanno   File: PersistentEnumUserType.java   View source code 6 votes vote down vote up
@Override
public Object nullSafeGet(ResultSet rs, String[] names,
        SharedSessionContractImplementor session, Object owner)
    throws HibernateException, SQLException
{
    String name = rs.getString(names[0]);
    if (rs.wasNull()) {
        return null;
    }
    for (PersistentEnum value : returnedClass().getEnumConstants()) {
        if (name.equals(value.getId())) {
            return value;
        }
    }
    throw new IllegalStateException(
            "Unknown " + returnedClass().getSimpleName() + " value [" + name + "]");
}
 
Example 12
Project: pedal-dialect   File: PostgresqlBitStringUserType.java   View source code 6 votes vote down vote up
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
        throws HibernateException, SQLException {
    byte[] bytes = rs.getBytes(names[0]);

    if (rs.wasNull()) {
        return null;
    } else {
        BitSet bits = new BitSet(bytes.length);
        int index = 0;
        for (byte b : bytes) {
            // 1 and 0 are encoded as ascii values of '1' and '0'
            bits.set(index++, b - '0' != 0);
        }
        return bits;
    }
}
 
Example 13
Project: hibernate-l2-memcached   File: AbstractReadWriteMemcachedAccessStrategy.java   View source code 6 votes vote down vote up
/**
 * Returns <code>null</code> if the item is not readable.  Locked items are not readable, nor are items created
 * afterQuery the start of this transaction.
 */
public final Object get(SharedSessionContractImplementor session, Object key, long txTimestamp) throws CacheException {
    readLockIfNeeded(key);
    try {
        final Lockable item = (Lockable) region().get(key);

        final boolean readable = item != null && item.isReadable(txTimestamp);
        if (readable) {
            return item.getValue();
        } else {
            return null;
        }
    } finally {
        readUnlockIfNeeded(key);
    }
}
 
Example 14
Project: hibernate-l2-memcached   File: AbstractReadWriteMemcachedAccessStrategy.java   View source code 6 votes vote down vote up
/**
 * Returns <code>false</code> and fails to put the value if there is an existing un-writeable item mapped to this
 * key.
 */
@Override
public final boolean putFromLoad(
        SharedSessionContractImplementor session,
        Object key,
        Object value,
        long txTimestamp,
        Object version,
        boolean minimalPutOverride)
        throws CacheException {

    region.getCache().lock(key);
    try {
        final Lockable item = (Lockable) region().get(key);
        final boolean writeable = item == null || item.isWriteable(txTimestamp, version, versionComparator);
        if (writeable) {
            region().put(key, new Item(value, version, region.nextTimestamp()));
            return true;
        } else {
            return false;
        }
    } finally {
        region.getCache().unlock(key);
    }
}
 
Example 15
Project: hibernate-l2-memcached   File: TransactionalMemcachedEntityRegionAccessStrategy.java   View source code 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean putFromLoad(
        SharedSessionContractImplementor session,
        Object key,
        Object value,
        long txTimestamp,
        Object version,
        boolean minimalPutOverride) throws CacheException {

    if (minimalPutOverride && cache.get(key) != null) {
        return false;
    }
    //OptimisticCache? versioning?
    cache.put(key, value);
    return true;
}
 
Example 16
Project: hibernate-l2-memcached   File: ReadWriteMemcachedEntityRegionAccessStrategy.java   View source code 6 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * Inserts will only succeed if there is no existing value mapped to this key.
 */
@Override
public boolean afterInsert(SharedSessionContractImplementor session, Object key, Object value, Object version) throws CacheException {
    region.getCache().lock(key);
    try {
        final Lockable item = (Lockable) region().get(key);
        if (item == null) {
            region().put(key, new Item(value, version, region.nextTimestamp()));
            return true;
        } else {
            return false;
        }
    } finally {
        region.getCache().unlock(key);
    }
}
 
Example 17
Project: hibernate-l2-memcached   File: ReadWriteMemcachedEntityRegionAccessStrategy.java   View source code 6 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * Updates will only succeed if this entry was locked by this transaction and exclusively this transaction for the
 * duration of this transaction.  It is important to also note that updates will fail if the soft-lock expired during
 * the course of this transaction.
 */
@Override
public boolean afterUpdate(SharedSessionContractImplementor session, Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock)
        throws CacheException {
    //what should we do with previousVersion here?
    region.getCache().lock(key);
    try {
        final Lockable item = (Lockable) region().get(key);

        if (item != null && item.isUnlockable(lock)) {
            final Lock lockItem = (Lock) item;
            if (lockItem.wasLockedConcurrently()) {
                decrementLock(key, lockItem);
                return false;
            } else {
                region().put(key, new Item(value, currentVersion, region.nextTimestamp()));
                return true;
            }
        } else {
            handleLockExpiry(key, item);
            return false;
        }
    } finally {
        region.getCache().unlock(key);
    }
}
 
Example 18
Project: postguice   File: TextArrayUserType.java   View source code 5 votes vote down vote up
@Override
public Object nullSafeGet(final ResultSet rs, final String[] names, final SharedSessionContractImplementor session, final Object owner) throws HibernateException, SQLException {
	if (rs.wasNull()) {
		return null;
	}

	final String[] array = (String[])rs.getArray(names[0]).getArray();
	return array;
}
 
Example 19
Project: postguice   File: TextArrayUserType.java   View source code 5 votes vote down vote up
@Override
public void nullSafeSet(final PreparedStatement st, final Object value, final int index, final SharedSessionContractImplementor session) throws HibernateException, SQLException {
	if (value == null) {
		st.setNull(index, SQL_TYPES[0]);
	} else {
		final Array array = session.connection().createArrayOf("text", (String[])value);
		st.setArray(index, array);
	}
}
 
Example 20
Project: postguice   File: JSONUserType.java   View source code 5 votes vote down vote up
@Override
public Object nullSafeGet(final ResultSet rs, final String[] names, final SharedSessionContractImplementor session, final Object owner) throws HibernateException, SQLException {
	String value = rs.getString(names[0]);

	if (value == null) {
		return null;
	} else {
		try {
			return MAPPER.readValue(value, this.javaType);
		} catch (IOException e) {
			throw new HibernateException("Unable to read object from result set", e);
		}
	}
}
 
Example 21
Project: postguice   File: JSONUserType.java   View source code 5 votes vote down vote up
@Override
public void nullSafeSet(final PreparedStatement st, final Object value, final int index, final SharedSessionContractImplementor session) throws HibernateException, SQLException {
	if (value == null) {
		st.setNull(index, Types.OTHER);
	} else {
		try {
			st.setObject(index, MAPPER.writeValueAsString(value), Types.OTHER);
		} catch (JsonProcessingException e) {
			throw new HibernateException("Unable to set object to result set", e);
		}
	}
}
 
Example 22
Project: postguice   File: SimpleStringUserType.java   View source code 5 votes vote down vote up
@Override
public Object nullSafeGet(final ResultSet rs, final String[] names, final SharedSessionContractImplementor session, final Object owner) throws HibernateException, SQLException {
	String value = rs.getString(names[0]);

	if (value == null) {
		return null;
	} else {
		return construct(value);
	}
}
 
Example 23
Project: postguice   File: SimpleStringUserType.java   View source code 5 votes vote down vote up
@Override
public void nullSafeSet(final PreparedStatement st, final Object value, final int index, final SharedSessionContractImplementor session) throws HibernateException, SQLException {
	if (value == null) {
		st.setNull(index, Types.VARCHAR);
	} else {
		st.setString(index, value.toString());
	}
}
 
Example 24
Project: flow-platform   File: SettingContentAdaptor.java   View source code 5 votes vote down vote up
@Override
public Object nullSafeGet(ResultSet rs,
    String[] names,
    SharedSessionContractImplementor session,
    Object owner) throws HibernateException, SQLException {
    String str = rs.getString(names[0]);
    if (str == null) {
        return null;
    }
    SettingContent settingContent = GSON.fromJson(str, SettingContent.class);
    return GSON.fromJson(str, settingContent.getType().getClazz());
}
 
Example 25
Project: flow-platform   File: ByteAdaptor.java   View source code 5 votes vote down vote up
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
    throws HibernateException, SQLException {
    byte[] bytes = rs.getBytes(names[0]);
    if (bytes == null) {
        return null;
    }
    return byteToString(bytes);
}
 
Example 26
Project: flow-platform   File: BaseAdaptor.java   View source code 5 votes vote down vote up
/**
 * @param rs
 * @param names
 * @param session
 * @param owner
 * @return
 * @throws HibernateException
 * @throws SQLException
 */
@Override
public Object nullSafeGet(ResultSet rs,
    String[] names,
    SharedSessionContractImplementor session,
    Object owner) throws HibernateException, SQLException {
    String str = rs.getString(names[0]);
    if (str == null) {
        return null;
    }
    return jsonToObject(str);
}
 
Example 27
Project: cr-private-server   File: SCID.java   View source code 5 votes vote down vote up
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
	final String value = rs.getString(names[0]);
	if (value == null) {
		return null;
	}

	return new SCID(Long.valueOf(value));
}
 
Example 28
Project: cr-private-server   File: SCID.java   View source code 5 votes vote down vote up
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
	if (value == null) {
		st.setNull(index, Types.BIGINT);
		return;
	}

	st.setLong(index, ((SCID)value).getValue());
}
 
Example 29
Project: cr-private-server   File: AssignedIdentityGenerator.java   View source code 5 votes vote down vote up
@Override
public Serializable generate(SharedSessionContractImplementor session, Object obj) {
	if (obj instanceof Identifiable) {
		Identifiable identifiable = (Identifiable)obj;
		Serializable id = identifiable.getId();
		if (id != null) {
			return id;
		}
	}

	return super.generate(session, obj);
}
 
Example 30
Project: hibernate-types   File: PostgreSQLEnumType.java   View source code 5 votes vote down vote up
public void nullSafeSet(
        PreparedStatement st,
        Object value,
        int index,
        SharedSessionContractImplementor session)
        throws HibernateException, SQLException {
    if (value == null) {
        st.setNull(index, Types.OTHER);
    } else {
        st.setObject(index, value.toString(), Types.OTHER);
    }
}
 
Example 31
Project: hibernate-types   File: NullableCharacterType.java   View source code 5 votes vote down vote up
@Override
public void set(PreparedStatement st, Character value, int index,
                SharedSessionContractImplementor session) throws SQLException {
    if (value == null) {
        st.setNull(index, Types.CHAR);
    } else {
        st.setString(index, String.valueOf(value));
    }
}
 
Example 32
Project: infotranspub-backend   File: ServiceDateUserType.java   View source code 5 votes vote down vote up
public Object nullSafeGet(ResultSet rs, String[] names,
                          SharedSessionContractImplementor sharedSessionContractImplementor,
                          Object o) throws HibernateException, SQLException {
    String value = rs.getString(names[0]);

    if (rs.wasNull())
        return null;

    try {
        return ServiceDate.parseString(value);
    } catch (ParseException ex) {
        throw new SQLException("error parsing service date value: " + value, ex);
    }
}
 
Example 33
Project: infotranspub-backend   File: ServiceDateUserType.java   View source code 5 votes vote down vote up
public void nullSafeSet(PreparedStatement st, Object value, int index,
                        SharedSessionContractImplementor sharedSessionContractImplementor) throws HibernateException, SQLException {
    if (value == null) {
        st.setNull(index, SQL_TYPES[0]);
    } else {
        ServiceDate serviceDate = (ServiceDate) value;
        st.setString(index, serviceDate.getAsString());
    }
}
 
Example 34
Project: hazelcast-hibernate5   File: AbstractGeneralRegion.java   View source code 5 votes vote down vote up
@Override
public Object get(final SharedSessionContractImplementor session, final Object key) throws CacheException {
    try {
        return getCache().get(key, nextTimestamp());
    } catch (OperationTimeoutException e) {
        return null;
    }
}
 
Example 35
Project: hazelcast-hibernate5   File: AbstractGeneralRegion.java   View source code 5 votes vote down vote up
@Override
public void put(final SharedSessionContractImplementor session, final Object key, final Object value) throws CacheException {
    try {
        getCache().put(key, value, nextTimestamp(), null);
    } catch (OperationTimeoutException e) {
        Logger.getLogger(AbstractGeneralRegion.class).finest(e);
    }
}
 
Example 36
Project: gorm-hibernate5   File: IdentityEnumType.java   View source code 5 votes vote down vote up
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
    Object id = type.nullSafeGet(rs, names[0], session);
    if ((!rs.wasNull()) && id != null) {
        return bidiMap.getEnumValue(id);
    }
    return null;
}
 
Example 37
Project: gorm-hibernate5   File: IdentityEnumType.java   View source code 5 votes vote down vote up
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
    if (value == null) {
        st.setNull(index, sqlTypes[0]);
    }
    else {
        type.nullSafeSet(st, bidiMap.getKey(value), index, session);
    }
}
 
Example 38
Project: dhis2-core   File: EnumUserType.java   View source code 5 votes vote down vote up
@Override
public Object nullSafeGet( ResultSet resultSet, String[] names, SharedSessionContractImplementor impl, Object owner )
    throws HibernateException, SQLException
{
    String name = resultSet.getString( names[0] );
    E result = null;
    if ( !resultSet.wasNull() )
    {
        result = Enum.valueOf( clazz, name );
    }
    return result;
}
 
Example 39
Project: dhis2-core   File: EnumUserType.java   View source code 5 votes vote down vote up
@Override
public void nullSafeSet( PreparedStatement preparedStatement, Object value, int index, SharedSessionContractImplementor impl )
    throws HibernateException, SQLException
{
    if ( null == value )
    {
        preparedStatement.setNull( index, Types.VARCHAR );
    }
    else
    {
        preparedStatement.setString( index, ((Enum<?>) value).name() );
    }
}
 
Example 40
Project: dhis2-core   File: TranslationPropertyUserType.java   View source code 5 votes vote down vote up
@Override
public Object nullSafeGet( ResultSet resultSet, String[] names, SharedSessionContractImplementor impl, Object owner )
    throws HibernateException, SQLException
{
    String name = resultSet.getString( names[0] );
    TranslationProperty result = null;
    if ( !resultSet.wasNull() )
    {
        result = TranslationProperty.fromValue( name );
    }
    return result;
}