嗨,我正在尝试解决休眠搜索以索引由经典Sql查询更改的列,如下所示:
@Override
public boolean updateColumn(K entityId, String columnName, String columnValue) {
String entityName = daoType.getSimpleName();
ClassMetadata employeeMeta = currentSession().getSessionFactory().getClassMetadata(daoType);
String primaryKey = employeeMeta.getIdentifierPropertyName();
String queryString = "update " + entityName + " set " + columnName + "='" + columnValue + "' where " + primaryKey + "=" + entityId;
org.hibernate.Query query = currentSession().createQuery(queryString);
boolean result = query.executeUpdate() > 0;
return result;
}
调用上述方法如下:
belgeSatirService.updateColumn(1, "basvuruNo", "thgm");
更新“ basvuruNo”列后,Hibernate不会自动更新basvuruNo列。该列的定义如下:
@Field(store = Store.YES)
@Column(name = "BasvuruNo", length = 30)//13/95088973/0735/000001
@Analyzer(definition = "whitespaceanalyzer")
private String basvuruNo;
Hibernate Search不支持拦截使用查询应用的更新更改。
我建议重写DAO模式以使用Hibernate友好模式:通过在域模型上使用实际的对象和设置器。
您还将从中获得其他一些好处,例如:
这样的方法看起来像这样:
public void updateBasvuruNo(K entityId, Class<E> type, String newBasvuruNo) {
E yourEntity = currentSession().load( type, entityId );
yourEntity.setBasvuruNo( newBasvuruNo );
}
仅在需要时,才根据需要将托管实体上的状态刷新到数据库,并由您的应用程序控制:通常由事务范围控制。因此,您可能根本不想使用这样的帮助程序,这只是不必要的样板。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句