After implementing ICompositeUserType using nhib.pdf, You can query attributes of composite objects in HQL. For example
Class diagram:
Table Structure:
Xml configuration: <Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.2" namespace = "ThoughtSoft. CMS. Box" assembly = "thoughtsoft. cms">
<Class name = "BoxParamDao" table = "Cms_Box_Param">
<Id name = "BoxParamId">
<Column name = "BOX_PARAM_ID" SQL-type = "nvarchar2" length = "36" not-null = "true"/>
<Generator class = "assigned"/>
</Id>
<Property name = "BoxId">
<Column name = "BOX_ID" SQL-type = "number (8, 0)" not-null = "true"/>
</Property>
<Property name = "AttributeInfo" type = "ThoughtSoft. CMS. Box. BoxParamCompositeType, thoughtsoft. cms">
<Column name = "BOX_PARAM_TYPE" SQL-type = "nvarchar2" length = "20" not-null = "true"/>
<Column name = "BOX_PARAM_NAME" SQL-type = "nvarchar2" length = "20" not-null = "true"/>
<Column name = "BOX_PARAM_DESC" SQL-type = "nvarchar2" length = "50" not-null = "false"/>
<Column name = "BOX_PARAM_LEN" SQL-type = "nvarchar2" length = "50" not-null = "false"/>
</Property>
<Property name = "Value">
<Column name = "BOX_PARAM_VALUE" SQL-type = "nvarchar2" length = "2000" not-null = "false"/>
</Property>
</Class>
</Hibernate-mapping>
When you use nhibdao to access BoxParamDao, You can implement an ICompositeUserType interface (ThoughtSoft. CMS. Box. BoxParamCompositeType) for the BoxAttributeInfo type. You can query in HQL as follows: IQuery query = session. CreateQuery ("from BoxParamDao t where t. BoxId =? And t. AttributeInfo. Name like? ")
. SetInt32 (0,104)
. SetString (1, "Title % ");
Here, we can correct an error in nhib.pdf series 04 enumeration of custom type component types 3. Custom type and custom ing type IUserType. In that example, A DateTime type attribute is stored in two CHAR fields (CREATE_DATE and CREATE_TIME) in the database, and IUserType is implemented for this attribute. Therefore, in the nhib1_test series 05 Critetia, HQL, Native SQL, Named Query 2. in the HQL section, there is no way to query this attribute in HQL.
The above method is incorrect. In this case, the DateTime attribute is completely a Composite object, and a Composite Class should be defined to implement the ICompositeUserType interface during storage ing.
In this way, the responsibilities of the ICompositeUserType and IUserType interfaces are clear: ICompositeUserType is fully used for the ing of the combination type; IUserType is used for the custom ing of special attributes.
Although you can also use the IUserType interface to map a single attribute to multiple database fields, in this case, for nhib.pdf, it is just a simple type attribute, rather than a combination object, you cannot query this attribute in HQL or Criteria.
In fact, this difference can be seen from the ICompositeUserType and IUserType interfaces themselves: ICompositeUserType requires the public IType [] PropertyTypes () method to return the attribute list of the composite object; public object GetPropertyValue (object component, int property) and public void SetPropertyValue (object component, int property, object value) must be implemented because the composite object has multiple attributes. IUserType has only one public SqlType [] SqlTypes (). In fact, this interface method should be changed to public SqlType (), which is more appropriate because one attribute is mapped to multiple fields, this is not a combination relationship. This method is very embarrassing or special, and once used in this way, other features such as HQL are not supported, so this is a redundant design, the user may misunderstand this interface.