public class HibTest {
private static final String TEST_FIELD_NAME = "s1";
private static final String TEST_VALUE = "
[email protected]";
public static String CUSTOM_COMPONENT_NAME = "customProperties";
private Class entityClass = Contact.class;
public HibTest() {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
configuration.buildSessionFactory().openSession();
Column column = new Column(TEST_FIELD_NAME);
column.setName(TEST_FIELD_NAME);
column.setNullable(false);
column.setUnique(false);
PersistentClass persistentClass = configuration.getClassMapping(entityClass.getName());
persistentClass.getTable().addColumn(column);
SimpleValue simpleValue = new SimpleValue();
column.setValue(simpleValue);
simpleValue.addColumn(column);
simpleValue.setTypeName("string");
simpleValue.setTable(persistentClass.getTable());
Property property = new Property();
property.setName(TEST_FIELD_NAME);
property.setValue(simpleValue);
property.setNodeName(property.getName());
Component customProperties = (Component) persistentClass.getProperty(CUSTOM_COMPONENT_NAME).getValue();
customProperties.addProperty(property);
session = configuration.buildSessionFactory().openSession();
try {
Contact contact = (Contact) session.get(Contact.class, 4);
Object value = contact.getValueOfCustomField(TEST_FIELD_NAME);
System.out.println("value = " + value);
} catch (Exception e) {
System.out.println("e = " + e);
}
}
public static void main(String[] args) {
HibTest test = new HibTest();
}
}
Exception in thread "main" org.hibernate.MappingException: property mapping has wrong number of columns: Contact.customProperties type: component[]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:441)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
at HibTest.<init>(HibTest.java:46)
at HibTest.main(HibTest.java:57)
1. I wish to add a property to the dynamic-component at runtime. However, I get an exception (see above). What's wrong?
2. I want to manipulate metadata at runtime in JEE environment (Jboss 4.2). How do I do that?