-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: PropertyNotFoundException: Could not find a getter
PostPosted: Sun Feb 15, 2009 1:31 pm 
Newbie

Joined: Sun Feb 15, 2009 1:23 pm
Posts: 4
I am new to Hibernate and trying to run the below example and having few problems...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>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2009 5:14 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Does the persistent class need a property, getters and setters when modifying its Class Mapping ?


No, the class doesn't need that, but if there is no getter/setter or property in a class you need to provide another mechanism that takes care of the values. If you check the Hibernate documentation (http://www.hibernate.org/hib_docs/v3/re ... n-property) you will find that for a <property> definition you can specify the attribute access="field|property|ClassName". The default values is access="property" which means that Hibernate expects a getter/setter pair. The access="field" option means that Hibernate uses a class variable. The access="ClassName" option specifies a class that must implement the org.hibernate.property.PropertyAccessor interface.

So, from your description it seems like you want the last case. Eg. you need to create a PropertyAccessor implementation. Then, you specify the name of that class in a call to Property.setPropertyAccessorName(). Eg.

Code:
Property prop = new Property();
prop.setName("motto");
prop.setValue(value);
prop.setPropertyAccessorName("the.name.of.a.PropertyAccessor");


Quote:
If I add the property motto and its methods as per the exception in persistent class, I get an error


You also need to call Property.setNodeName(). Use the same value as for Property.setName().

We are doing something similar in our project. Below are some links to our code.

http://base.thep.lu.se/browser/tags/2.9 ... .java#L357
http://base.thep.lu.se/browser/tags/2.9 ... essor.java


Top
 Profile  
 
 Post subject: regarding hbm.xml
PostPosted: Tue Feb 17, 2009 10:33 pm 
Newbie

Joined: Sun Feb 15, 2009 1:23 pm
Posts: 4
Hi Borg...Thanks for the reply...Its working.. I am trying to understand few concepts:

1. I was wondering if the .hbm.xml of User class changes due to the changes to the below code:

Code:
Property prop = new Property();
         prop.setName("motto");
         prop.setValue(value);
         prop.setNodeName(prop.getName());         
         UserMappng.addProperty(prop);


and

2. does the database change accordingly due to the below code and if not what does it update..

Code:
Column column = new Column();
         column.setName("MOTTO");
         column.setNullable(false);
         column.setUnique(true);
         
         UserMappng.getTable().addColumn(column);


If neither of the changes mentioned are done dynamically, where do we use this application.

Thanks...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 3:40 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
1. I was wondering if the .hbm.xml of User class changes due to the changes to the below code:


No.

Quote:
2. does the database change accordingly due to the below code and if not what does it update..


No, this alone doesn't change the database. You can try to use the SchemaUpdate tool for this. It's constructor takes a Configuration object as it's only parameter. We are doing this:

Code:
new SchemaUpdate(cfg).execute(false, true)


Note that the SchemaUpdate tools has several limitations. See http://forum.hibernate.org/viewtopic.ph ... 83#2397983 for some more information.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 11:02 pm 
Newbie

Joined: Sun Feb 15, 2009 1:23 pm
Posts: 4
Thanks a lot for the reply


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.