Hi,
ich hab mal was hinbekommen, falls es irgend jemanden mal interessieren sollte.
cfg ist die Hibernate Configuration.
Code:
try {
// The "sql" data types.
Type type = new StringType();
//Type dynamicType = new DynamicComponentType(new String[]{"bar"}, new Type[]{type}, new int[]{-1}, new Cascades.CascadeStyle[]{Cascades.STYLE_NONE});
//Type dynamicType = new DynamicComponentType(new String[]{"foo", "bar"}, new Type[]{type, type}, new int[]{-1}, new Cascades.CascadeStyle[]{Cascades.STYLE_NONE, Cascades.STYLE_NONE});
Type dynamicType = new DynamicComponentType(new String[]{"foo", "bar", "tender"},
new Type[]{type, type, type}, new int[]{-1},
new Cascades.CascadeStyle[]{Cascades.STYLE_NONE, Cascades.STYLE_NONE, Cascades.STYLE_NONE});
// The persistent dynamic attribute.
PersistentClass persistentClass = cfg.createMappings().getClass(Dynamic.class);
Property dynamicProperty = persistentClass.getProperty("dynamicAttributes");
// Get the dynamic component and
Component dynamicComponent = (Component) dynamicProperty.getValue();
dynamicComponent.setType(dynamicType);
// Add the new attribute if the component is a dynamic component.
if (dynamicComponent.isDynamic()) {
dynamicComponent.addProperty(addDynamicAttribute("foo", type, persistentClass));
dynamicComponent.addProperty(addDynamicAttribute("bar", type, persistentClass));
dynamicComponent.addProperty(addDynamicAttribute("tender", type, persistentClass));
}
// Database schema update.
try {
//sessionFactoryBean.updateDatabaseSchema();
new SchemaUpdate(cfg).execute(true, true);
}
catch (HibernateException he) {
he.printStackTrace();
}
}
catch (MappingException me) {
me.printStackTrace();
}
die append Methode
Code:
private Property addDynamicAttribute(String name, Type type, PersistentClass persistentClass) {
// Create a new db column specification.
Column column = new Column();
column.setName(name.toUpperCase());
column.setType(type);
// Add column to table also.
persistentClass.getTable().addColumn(column);
// Create a simple value that contains the column and will be set to the property as information.
SimpleValue simpleValue = new SimpleValue(persistentClass.getTable());
simpleValue.addColumn(column);
simpleValue.setType(type);
// Create a new property.
Property property = new Property();
property.setName(name);
property.setPropertyAccessorName("property");
property.setCascade("none");
property.setValue(simpleValue);
return property;
}
und hier ein Beispielmapping.
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<!--
The dynmic test mapping.
@author Romsl
-->
<hibernate-mapping>
<class name="bo.Dynamic" table="Z_DYNAMIC_COMPONENT">
<id name="id" type="long" unsaved-value="-1">
<column name="ID" not-null="true"/>
<generator class="native"/>
</id>
<property name="name" type="string">
<column name="NAME" not-null="true"/>
</property>
<dynamic-component name="dynamicAttributes"/>
</class>
</hibernate-mapping>
Viel Spaß damit,
Romsl