我得到了以下错误:
java.lang.NoSuchMethodException: Unknown property 'language' on class 'class java.lang.Class'
使用休息电话:/测试/休息/语言/插入/日语/瑞典语%206
我正在使用:org.apache.commons.beanutils.PropertyUtils;
我的语言类中有指定的属性'language',所以我不确定它为什么会抛出错误。它在我声明它的时候起作用:
语言lanobj = new Lanaguages();
//语言课
public class Languages implements java.io.Serializable {
private String language;
private String displayName;
public Languages() {
this.language = null;
this.displayName = null;
}
public Languages(String language, String displayName) {
this.language = language;
this.displayName = displayName;
}
public void setLanguageId(Long languageId) {
this.languageId = languageId;
}
@Column(name = "language", nullable = false, length = 50)
public String getLanguage() {
return this.language;
}
public void setLanguage(String language) {
this.language = language;
}
@Column(name = "displayName", nullable = false, length = 50)
public String getDisplayName() {
return this.displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
}
///资源管理器代码
@Path("{subResources}/insertobj/{language-id}/{displayname-id}")
//@POST
@GET // Using get for the sake of making rest call with browser
@Produces(MediaType.TEXT_HTML)
public String getLanguageById(@PathParam("subResources") String id,
@PathParam("language-id") String language_id,
@PathParam("displayname-id") String displayname_id)
{
try{
Class<?> c = Class.forName("com.test."+id);
PropertyUtils.setProperty(c, "language",language_id);
PropertyUtils.setProperty(c, "displayName",displayname_id);
TransactionManager.createObject(c);
}catch (Exception exc){
exc.printStackTrace();
}
}
// TransactionManager代码段:
public static void createObject(Object entityObject) {
Transaction tx = null;
//SessionFactoryUtil.getInstance().openSession();
Session session = SessionFactoryUtil.openSession();
try {
tx = session.beginTransaction();
session.save(entityObject);
tx.commit();
}
catch (RuntimeException e)
{
if (tx != null && tx.isActive())
{
try
{
// Second try catch as the rollback could fail as well
tx.rollback();
}
catch (HibernateException e1) {
//logger.debug(“Error rolling back transaction”);
}
// throw again the first exception
throw e;
}
}
finally
{
session.close();
}
}
答案 0 :(得分:5)
您正尝试在language
的Class对象上设置属性"com.test."+id
。您需要创建此类的实例以设置值。类似的东西:
try{
Class<?> c = Class.forName("com.test."+id);
Object o = c.newInstance();
PropertyUtils.setProperty(o, "language",language_id);
PropertyUtils.setProperty(o, "displayName",displayname_id);
TransactionManager.createObject(o);
}catch (Exception exc){
exc.printStackTrace();
}
c
引用的类必须具有零参数构造函数才能使此代码生效。
答案 1 :(得分:1)
setProperty
需要一个实例,而您正在通过该课程,请参阅documentation
Class<?> c = Class.forName("com.test."+id);
Language language = (Language)c.newInstance();
PropertyUtils.setProperty(language , "language",language_id);
要么特定地投射到Language
,要么就可以将其保留为Object
Object object = Class.forName("com.test."+id).newInstance();
PropertyUtils.setProperty(object, "language",language_id);
答案 2 :(得分:0)
由于您的类Languages
没有定义默认构造函数,因此beanutils无法进行反射。确保您在类中定义了默认构造函数。