예제 #1
0
 public int generateHashCode(SessionFactoryImplementor factory) {
   int result = 17;
   result = 37 * result + entityName.hashCode();
   result = 37 * result + uniqueKeyName.hashCode();
   result = 37 * result + keyType.getHashCode(key, factory);
   return result;
 }
예제 #2
0
 /**
  * Construct a new key for a collection or entity instance. Note that an entity name should always
  * be the root entity name, not a subclass entity name.
  *
  * @param id The identifier associated with the cached data
  * @param type The Hibernate type mapping
  * @param entityOrRoleName The entity or collection-role name.
  * @param tenantId The tenant identifier associated this data.
  * @param factory The session factory for which we are caching
  */
 public CacheKey(
     final Serializable id,
     final Type type,
     final String entityOrRoleName,
     final String tenantId,
     final SessionFactoryImplementor factory) {
   this.key = id;
   this.type = type;
   this.entityOrRoleName = entityOrRoleName;
   this.tenantId = tenantId;
   this.hashCode = type.getHashCode(key, factory);
 }
  /**
   * Construct a new key for a caching natural identifier resolutions into the second level cache.
   * Note that an entity name should always be the root entity name, not a subclass entity name.
   *
   * @param naturalIdValues The naturalIdValues associated with the cached data
   * @param persister The persister for the entity
   * @param session The originating session
   */
  public NaturalIdCacheKey(
      final Object[] naturalIdValues,
      final EntityPersister persister,
      final SessionImplementor session) {

    this.entityName = persister.getRootEntityName();
    this.tenantId = session.getTenantIdentifier();

    this.naturalIdValues = new Serializable[naturalIdValues.length];

    final SessionFactoryImplementor factory = session.getFactory();
    final int[] naturalIdPropertyIndexes = persister.getNaturalIdentifierProperties();
    final Type[] propertyTypes = persister.getPropertyTypes();

    final int prime = 31;
    int result = 1;
    result = prime * result + ((this.entityName == null) ? 0 : this.entityName.hashCode());
    result = prime * result + ((this.tenantId == null) ? 0 : this.tenantId.hashCode());
    for (int i = 0; i < naturalIdValues.length; i++) {
      final Type type = propertyTypes[naturalIdPropertyIndexes[i]];
      final Object value = naturalIdValues[i];

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

      this.naturalIdValues[i] = type.disassemble(value, session, null);
    }

    this.hashCode = result;
    this.toString =
        new ValueHolder<String>(
            new ValueHolder.DeferredInitializer<String>() {
              @Override
              public String initialize() {
                // Complex toString is needed as naturalIds for entities are not simply based on a
                // single value like primary keys
                // the only same way to differentiate the keys is to included the disassembled
                // values in the string.
                final StringBuilder toStringBuilder =
                    new StringBuilder(entityName).append("##NaturalId[");
                for (int i = 0; i < naturalIdValues.length; i++) {
                  toStringBuilder.append(naturalIdValues[i]);
                  if (i + 1 < naturalIdValues.length) {
                    toStringBuilder.append(", ");
                  }
                }
                toStringBuilder.append("]");

                return toStringBuilder.toString();
              }
            });
  }
 public int generateHashCode() {
   int result = 17;
   result = 37 * result + role.hashCode();
   result = 37 * result + keyType.getHashCode(key, entityMode, factory);
   return result;
 }