我在hashmap中有一些要更新的字段,我试图把它放在 session.update(对象对象)中。但我得到异常错误未知实体:java.util.HashMap 。这是我的代码。
class_name.addCondition("loginName", "dadan_evil");
class_name.addCondition("lastName", "Pirate");
session.update(filter.getFilterMap());//I tried this, but not working
更新这些字段:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="in.co.User" table="user">
<meta attribute="class-description">
This class contains the user detail.
</meta>
<id name = "loginName" type="string" column="loginName">
</id>
<property name="passCode" column="passCode" type="string"/>
<property name="firstName" column="firstName" type="string"/>
<property name="lastName" column="lastName" type="string"/>
<property name="alternateEMail" column="alternateEMail" type="string"/>
<property name="mobile" column="mobile" type="string"/>
<property name="sysAdmin" column="sysAdmin" type="boolean"/>
<property name="deleted" column="deleted" type="boolean"/>
<property name="authenticationCode" column="authenticationCode" type="string"/>
<property name="authenticated" column="authenticated" type="boolean"/>
</class>
</hibernate-mapping>
User.hbm.xml:
private static void updateFromQuery(DBFilter filter) throws Exception {
Session session = factory.openSession();
try{
Class<?> className = filter.getCollectionClass();
Criteria criteria = session.createCriteria(className);
criteria.add(Restrictions.allEq(filter.getFilterMap()));
//I dont know how to update this, just an imaginary parameter
session.update(filter.getFilterMap());
}catch(HibernateException e){
e.printStackTrace();
}finally{
session.close();
}
}
我的更新方法:
<h4>Angular-xeditable Editable row (Bootstrap 3)</h4>
<div ng-app="app" ng-controller="Ctrl">
<table class="table table-bordered table-hover table-condensed">
<tr style="font-weight: bold">
<td style="width:35%">Name</td>
<td style="width:20%">Status</td>
<td style="width:20%">Group</td>
<td style="width:25%">Edit</td>
</tr>
<tr ng-repeat="user in users">
<td>
<!-- editable username (text with validation) -->
<span editable-text="user.name" e-name="name" e-form="rowform" onbeforesave="checkName($data, user.id)" e-required>
{{ user.name || 'empty' }}
</span>
</td>
<td>
<!-- editable status (select-local) -->
<span editable-select="user.status" e-name="status" e-form="rowform" e-ng-options="s.value as s.text for s in statuses">
{{ showStatus(user) }}
</span>
</td>
<td>
<!-- editable group (select-remote) -->
<span editable-select="user.group" e-name="group" onshow="loadGroups()" e-form="rowform" e-ng-options="g.id as g.text for g in groups">
{{ showGroup(user) }}
</span>
</td>
<td style="white-space: nowrap">
<!-- form -->
<form editable-form name="rowform" onbeforesave="saveUser($data, user.id)" ng-show="rowform.$visible" class="form-buttons form-inline" shown="inserted == user">
<button type="submit" ng-disabled="rowform.$waiting" class="btn btn-primary">
save
</button>
<button type="button" ng-disabled="rowform.$waiting" ng-click="rowform.$cancel()" class="btn btn-default">
cancel
</button>
</form>
<div class="buttons" ng-show="!rowform.$visible">
<button class="btn btn-primary" ng-click="rowform.$show()">edit</button>
<button class="btn btn-danger" ng-click="removeUser($index)">del</button>
</div>
</td>
</tr>
</table>
<button class="btn btn-default" ng-click="addUser()">Add row</button>
</div>
我无法写下字符串查询,因为我有多个表和不同的值。我甚至动态地获得了类Names。所以我不能使用字符串查询对所有人都是通用的。那么,如果可以,请告诉我。
现在,如何将这两个值更新到数据库。请帮忙
答案 0 :(得分:0)
我是通过使用 java.lang.reflect.Field 完成的。所以我从数据库中检索了特定的对象,我将获得键和值与Map分离,并通过引用我需要的类与Field绑定。现在这个工作正常。即使我得到比我更好的答案,也没关系。
private static void setField(Class<?> class_name,Object object, String fieldName, Object value) throws Exception {
Field field = class_name.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(object, value);
}
private static void updateFromQuery(DBFilter filter) throws Exception {
Session session = factory.openSession();
try {
Class<?> className = filter.getCollectionClass();
Criteria criteria = session.createCriteria(className);
criteria.add(Restrictions.allEq(filter.getFilterMap()));
List objectList = criteria.list();
Transaction tx;
tx = session.beginTransaction();
for (Object object : objectList) {
for (Map.Entry<String, Object> entry : filter.getValueMap().entrySet()) {
setField(className,object, entry.getKey(), entry.getValue());
}
session.update(object);
}
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
}
感谢所有支持的人。