Package org.internna.iwebmvc.model.hibernate

Source Code of org.internna.iwebmvc.model.hibernate.UUIDType

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.model.hibernate;

import org.internna.iwebmvc.model.UUID;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

/**
* Really simple implementation of a Hibernate User Type that handles UUIDs
*
* @author Jose Noheda
* @since 1.0
*/
public class UUIDType implements UserType {

    private static final int[] SQL_TYPES = new int[] { Types.BINARY };

    public int[] sqlTypes() {
        return SQL_TYPES;
    }

    public Class<?> returnedClass() {
        return UUID.class;
    }

    public boolean equals(Object x, Object y) throws HibernateException {
        return (x == y) || ( x != null && y != null && x.equals(y));
    }

    public int hashCode(Object x) throws HibernateException {
        return x.hashCode();
    }

    public Object nullSafeGet(ResultSet rs, String[] names, Object arg2) throws HibernateException, SQLException {
        byte[] bytes = rs.getBytes(names[0]);
        if (rs.wasNull()) return null;
        return new UUID(bytes);
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
        if (value == null) st.setNull(index, Types.BINARY);
        else st.setBytes(index, ((UUID) value).getBytes());
    }

    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }

    public boolean isMutable() {
        return false;
    }

    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) value;
    }

    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return cached;
    }

    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }

}
TOP

Related Classes of org.internna.iwebmvc.model.hibernate.UUIDType

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.