首页>代码>用Hibernate实现领域对象的自定义字段>/customfieldsdemo/src/com/enterra/customfieldsdemo/CustomizableEntityManagerImpl.java
package com.enterra.customfieldsdemo; import java.util.Iterator; import org.hibernate.mapping.Column; import org.hibernate.mapping.Component; import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.Property; import org.hibernate.mapping.SimpleValue; 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); } }

dd1995 LV9
2020年1月17日
luohaipeng LV23
2019年11月19日
RicardoWX LV7
2018年6月10日
小白袁 LV1
2018年5月17日
andycoder LV2
2018年5月3日
nonaddress LV1
2018年2月28日
xiayulin1983 LV2
2017年11月13日
bhkkvip LV1
2017年7月3日
a337214749 LV2
2017年6月27日
sf0001 LV2
2016年12月18日