Example usage for org.hibernate.type Type getHashCode

List of usage examples for org.hibernate.type Type getHashCode

Introduction

In this page you can find the example usage for org.hibernate.type Type getHashCode.

Prototype

int getHashCode(Object x, SessionFactoryImplementor factory) throws HibernateException;

Source Link

Document

Get a hash code, consistent with persistence "equality".

Usage

From source file:com.hazelcast.hibernate.region.NaturalIdCacheKey.java

License:Open Source License

@SuppressWarnings("checkstyle:magicnumber")
public NaturalIdCacheKey(final Object[] naturalIdValues, Type[] propertyTypes, int[] naturalIdPropertyIndexes,
        final String entityName, final SharedSessionContractImplementor session) {

    this.naturalIdValues = new Serializable[naturalIdValues.length];
    this.entityName = entityName;
    this.tenantId = session.getTenantIdentifier();

    SessionFactoryImplementor factory = session.getFactory();

    int result = 1;
    result = 31 * result + (entityName != null ? entityName.hashCode() : 0);
    result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0);

    for (int i = 0; i < naturalIdValues.length; i++) {
        final int naturalIdPropertyIndex = naturalIdPropertyIndexes[i];
        final Type type = propertyTypes[naturalIdPropertyIndex];
        final Object value = naturalIdValues[i];

        result = 31 * result + (value != null ? type.getHashCode(value, factory) : 0);

        if (type instanceof EntityType
                && type.getSemiResolvedType(factory).getReturnedClass().isInstance(value)) {
            this.naturalIdValues[i] = (Serializable) value;
        } else {//from   w  w  w  . jav  a  2  s  . co  m
            this.naturalIdValues[i] = type.disassemble(value, session, null);
        }
    }
    this.hashCode = result;
}