清单8:getClassMapping(Class entityClass)方法
处理映射
一旦我们有了可用的业务实体类(Contact)和与Hibernate交互的主类,我们就能开始工作了。我们能创建、保存Contact类的实例。甚至可以在Map对象customProperties里面放置一些数据,但是需要注意的是存储在Map对象customProperties里面的数据并不会被保存到数据库里。
为了保存数据,我们需要让这个机制能在类里面创建自定义字段,并且要让Hibernate知道该如何处理它们。
为了实现对类映射的处理,我们需要创建一些接口。叫它CustomizableEntityManager吧。名字应该表现出该接口管理业务实体及其内容、属性的意图:
package com.enterra.customfieldsdemo; import org.hibernate.mapping.Component; public interface CustomizableEntityManager { public static String CUSTOM_COMPONENT_NAME = "customProperties"; void addCustomField(String name); void removeCustomField(String name); Component getCustomProperties(); Class getEntityClass(); } |
清单9:CustomizableEntityManager接口
接口中重要的方法是void addCustomField(String name)和void removeCustomField(String name)。它们将分别在相应类的映射里创建、删除我们的自定义字段。
下面是实现该接口的情况:
import org.hibernate.cfg.Configuration; import org.hibernate.mapping.*; import java.util.Iterator; public class CustomizableEntityManagerImpl implements CustomizableEntityManager { private Component customProperties; private Class entityClass; public CustomizableEntityManagerImpl(Class entityClass) { this.entityClass = entityClass; } public Class getEntityClass() { return entityClass; } public Component getCustomProperties() { if (customProperties == null) { Property property = getPersistentClass().getProperty(CUSTOM_COMPONENT_NAME); customProperties = (Component) property.getValue(); } return customProperties; } public void addCustomField(String name) { SimpleValue simpleValue = new SimpleValue(); simpleValue.addColumn(new Column("fld_" + name)); simpleValue.setTypeName(String.class.getName()); PersistentClass persistentClass = getPersistentClass(); simpleValue.setTable(persistentClass.getTable()); Property property = new Property(); property.setName(name); property.setValue(simpleValue); getCustomProperties().addProperty(property); updateMapping(); } public void removeCustomField(String name) { Iterator propertyIterator = customProperties.getPropertyIterator(); while (propertyIterator.hasNext()) { Property property = (Property) propertyIterator.next(); if (property.getName().equals(name)) { propertyIterator.remove(); updateMapping(); return; } } } private synchronized void updateMapping() { MappingManager.updateClassMapping(this); HibernateUtil.getInstance().reset(); // updateDBSchema(); } private PersistentClass getPersistentClass() { return HibernateUtil.getInstance().getClassMapping(this.entityClass); } } |
清单10:接口CustomizableEntityManager的实现
首先需要指出的是,在构造CustomizableEntityManager时,我们要指定管理器操作的业务实体类。该业务实体类作为参数传递给CustomizableEntityManager的构造函数:
private Class entityClass; public CustomizableEntityManagerImpl(Class entityClass) { this.entityClass = entityClass; } public Class getEntityClass() { return entityClass; } |
清单11:CustomizableEntityManagerImpl构造函数
现在我们应该对void addCustomField(String name)方法的实现更感兴趣:
public void addCustomField(String name) { SimpleValue simpleValue = new SimpleValue(); simpleValue.addColumn(new Column("fld_" + name)); simpleValue.setTypeName(String.class.getName()); PersistentClass persistentClass = getPersistentClass(); simpleValue.setTable(persistentClass.getTable()); Property property = new Property(); property.setName(name); property.setValue(simpleValue); getCustomProperties().addProperty(property); updateMapping(); } |