work on javadoc for UserType and CompositeUserType

This commit is contained in:
Gavin King 2022-11-08 08:56:43 +01:00
parent 2f1e85095c
commit aaeed841c8
6 changed files with 57 additions and 53 deletions

View File

@ -6,11 +6,8 @@
*/ */
package org.hibernate.metamodel.spi; package org.hibernate.metamodel.spi;
import java.util.function.Supplier;
import org.hibernate.Incubating; import org.hibernate.Incubating;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.Instantiator;
/** /**
* Contract for instantiating embeddable values * Contract for instantiating embeddable values

View File

@ -43,12 +43,11 @@ import org.hibernate.usertype.UserVersionType;
* handle the case of the wrapped type implementing them so we can pass them * handle the case of the wrapped type implementing them so we can pass them
* along. * along.
* *
* todo (6.0) : ^^ this introduces a problem in code that relies on `instance of` checks
* against any of these interfaces when the wrapped type does not
*
* @author Gavin King * @author Gavin King
* @author Steve Ebersole * @author Steve Ebersole
*/ */
// todo (6.0) : ^^ this introduces a problem in code that relies on `instanceof` checks
// against any of these interfaces when the wrapped type does not
public class CustomType<J> public class CustomType<J>
extends AbstractType extends AbstractType
implements ConvertedBasicType<J>, ProcedureParameterNamedBinder<J>, ProcedureParameterExtractionAware<J> { implements ConvertedBasicType<J>, ProcedureParameterNamedBinder<J>, ProcedureParameterExtractionAware<J> {

View File

@ -15,39 +15,47 @@ import org.hibernate.metamodel.spi.EmbeddableInstantiator;
import org.hibernate.metamodel.spi.ValueAccess; import org.hibernate.metamodel.spi.ValueAccess;
/** /**
* A {@link UserType} that may be dereferenced in a query. * This interface should be implemented by user-defined custom types
* that have persistent attributes and can be thought of as something
* more like an {@linkplain jakarta.persistence.Embeddable embeddable}
* object. However, these persistent "attributes" need not necessarily
* correspond directly to Java fields or properties.
* <p> * <p>
* This interface allows a custom type to define "properties". * A value type managed by a {@code CompositeUserType} may be used in
* These need not necessarily correspond to physical JavaBeans * almost every way that a regular embeddable type may be used. It may
* style properties. * even contain many-to-one associations.
* <p> * <p>
* A {@code CompositeUserType} may be used in almost every way * To "map" the attributes of a composite custom type, each
* that an {@linkplain jakarta.persistence.Embeddable embeddable} * {@code CompositeUserType} provides a {@linkplain #embeddable()
* type may be used. It may even contain many-to-one associations. * regular embeddable class} with the same logical structure as the
* {@linkplain #returnedClass() value type managed by the custom
* type}.
* <p> * <p>
* Implementors must be immutable and must declare a public * For example, if we implement a {@code CompositeUserType} for a
* default constructor. * {@code MonetaryAmount} class, we would also provide a
* {@code MonetaryAmountEmbeddable} class with a field for each
* logical persistent attribute of the custom type. Of course,
* {@code MonetaryAmountEmbeddable} is never instantiated at runtime,
* and is never referenced in any entity class. It is a source of
* metadata only.
* <p> * <p>
* Unlike {@code UserType}, cacheability does not depend upon * Properties of this embeddable class are sorted alphabetically by
* serializability. Instead, {@link #assemble(Serializable, Object)} * name, and assigned an index based on this ordering.
* and {@link #disassemble(Object)} provide conversion to and
* from a cacheable representation.
* <p> * <p>
* Properties are ordered by name, that is, they are sprted * Every implementor of {@code CompositeUserType} must be immutable
* alphabetically such that * and must declare a public default constructor.
* {@code properties[i].name < properties[i+1].name}
* for all {@code i >= 0}.
*/ */
@Incubating @Incubating
public interface CompositeUserType<J> extends EmbeddableInstantiator { public interface CompositeUserType<J> extends EmbeddableInstantiator {
/** /**
* Get the value of a property. * Get the value of the property with the given index. Properties
* of the {@link #embeddable()} are sorted by name and assigned an
* index based on this ordering.
* *
* @param component an instance of class mapped by this "type" * @param component an instance of class mapped by this "type"
* @param property the property index * @param property the property index
* @return the property value * @return the property value
* @throws HibernateException
*/ */
Object getPropertyValue(J component, int property) throws HibernateException; Object getPropertyValue(J component, int property) throws HibernateException;
@ -56,15 +64,11 @@ public interface CompositeUserType<J> extends EmbeddableInstantiator {
/** /**
* The class that represents the embeddable mapping of the type. * The class that represents the embeddable mapping of the type.
*
* @return Class
*/ */
Class<?> embeddable(); Class<?> embeddable();
/** /**
* The class returned by {@code instantiate()}. * The class returned by {@code instantiate()}.
*
* @return Class
*/ */
Class<J> returnedClass(); Class<J> returnedClass();

View File

@ -16,7 +16,7 @@ import org.hibernate.metamodel.CollectionClassification;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
/** /**
* A custom type for mapping user-written classes that implement {@code PersistentCollection} * A custom type for mapping user-written classes which implement {@link PersistentCollection}.
* *
* @see PersistentCollection * @see PersistentCollection
* *

View File

@ -21,33 +21,37 @@ import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.spi.TypeConfiguration; import org.hibernate.type.spi.TypeConfiguration;
/** /**
* This interface should be implemented by user-defined "types". * This interface should be implemented by user-defined custom types.
* A "type" class is <em>not</em> the actual property type - it * A custom type is <em>not</em> an actual persistent attribute type,
* is a class that knows how to serialize instances of another * rather it is a class responsible for serializing instances of some
* class to and from JDBC. * other class to and from JDBC. This other class should have "value"
* semantics, since its identity is lost as part of this serialization
* process.
* <p> * <p>
* This interface * Every implementor of {@code UserType} must be immutable and must
* declare a public default constructor.
* <p>
* This interface:
* <ul> * <ul>
* <li>abstracts user code from future changes to the {@code Type} * <li>abstracts user code away from changes to the interface
* interface,</li> * {@link org.hibernate.type.Type},
* <li>simplifies the implementation of custom types and</li> * <li>simplifies the implementation of custom types, and
* <li>hides certain "internal" interfaces from user code.</li> * <li>hides certain SPI interfaces from user code.
* </ul> * </ul>
* The class {@link org.hibernate.type.CustomType} automatically adapts
* between {@code UserType} and {@link org.hibernate.type.Type}.
* <p> * <p>
* Implementors must be immutable and must declare a public * In principle, a custom type could implement {@code Type} directly,
* default constructor. * or extend one of the abstract classes in {@link org.hibernate.type}.
* But this approach risks breakage resulting from future incompatible
* changes to classes or interfaces in that package, and is therefore
* discouraged.
* <p> * <p>
* The actual class mapped by a {@code UserType} may be just * A custom type implemented as a {@code UserType} is treated as a
* about anything. * non-composite value, and does not have persistent attributes which
* <p> * may be used in queries. If a custom type does have attributes, and
* {@code CompositeUserType} provides an extended version of * can be thought of as something more like an embeddable object, it
* this interface that is useful for more complex cases. * might be better to implement {@link CompositeUserType}.
* <p>
* Alternatively, custom types could implement {@code Type}
* directly or extend one of the abstract classes in
* {@link org.hibernate.type}. This approach risks future
* incompatible changes to classes or interfaces in that
* package.
* *
* @see org.hibernate.type.Type * @see org.hibernate.type.Type
* @see org.hibernate.type.CustomType * @see org.hibernate.type.CustomType

View File

@ -33,7 +33,7 @@ public class ImmutableMonetoryAmountUserType implements CompositeUserType<Immuta
} }
@Override @Override
public Class returnedClass() { public Class<ImmutableMonetoryAmount> returnedClass() {
return ImmutableMonetoryAmount.class; return ImmutableMonetoryAmount.class;
} }