I am new to Hibernate and trying to run the below example...Any response would be of gr8 help.
I am trying to run an example to modify configuation-time metamodel of a class. I did not get how it works. I get the following error
org.hibernate.PropertyNotFoundException: Could not find a getter for motto
1. Does the persistent class need a property, getters and setters when modifying its Class Mapping ?
If so, whats the reason to dynamically modify...Can't it be done manually.
2. If I add the property motto and its methods as per the exception in persistent class, I get an error org.hibernate.MappingException: no node name for property: motto
Below is the code I am trying to run. Can you help me with the idea behind it.
Code to run:
Configuration cfg = new Configuration();
//cfg.addResource("com/Cavatier/Classes/User.hbm.xml");
PrintWriter pw = response.getWriter();
pw.println("Entered Servlet");
cfg.configure();
PersistentClass UserMappng= cfg.getClassMapping
("com.Cavatier.Classes.User");
System.err.println("----UserMapping--------"+UserMappng);
Column column = new Column();
column.setName("MOTTO");
column.setNullable(false);
column.setUnique(true);
UserMappng.getTable().addColumn(column);
SimpleValue value = new SimpleValue();
value.setTable(UserMappng.getTable());
value.addColumn(column);
value.setTypeName(String.class.getName());
Property prop = new Property();
prop.setName("motto");
prop.setValue(value);
UserMappng.addProperty(prop);
SessionFactory sf = cfg.buildSessionFactory();
ClassMetadata meta = sf.getClassMetadata(User.class);
String[] metaPropertyNames = meta.getPropertyNames();
Object[] propertyValues = meta
.getPropertyValues(User.class, EntityMode.POJO);
for (int i = 0; i < metaPropertyNames.length; i++) {
pw.println("----metaPropertyNames --- "+ metaPropertyNames[i]);
pw.println("----propertyValues --- "+propertyValues[i]);
System.out.println("----metaPropertyNames --- "+ metaPropertyNames[i]);
System.out.println("-----propertyValues --- "+propertyValues[i]);
}
pw.println(cfg.getClassMapping("User.class"));
System.out.println(cfg.getClassMapping("User.class"));
Persistent Class
User.java
motto is the property to be added
package com.Cavatier.Classes;
import java.io.Serializable;
public class User implements Serializable{
private Integer id;
private String username;
private String address;
// private String motto;
//
// public String getMotto() {
// return motto;
// }
//
// public void setMotto(String motto) {
// this.motto = motto;
// }
public User() {
// TODO Auto-generated constructor stub
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
/*public MonetaryAmount calcShippingCosts(Address fromLocation){
}*/
}
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.Cavatier.Classes">
<class name="User" table="USER">
<id name="id" column="USER_ID" type="long">
<generator class="native"></generator>
</id>
<property name="username" column="USERNAME" type="string"></property>
<property name="address" column="ADDRESS" type="string"></property>
</class>
</hibernate-mapping>
|