Hi I am trying to solve Hibernate Search to index a column changed by classic Sql query as follows:
@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;
}
Calling above method as follows:
belgeSatirService.updateColumn(1, "basvuruNo", "thgm");
After updating "basvuruNo" column, Hibernate does not automatically update basvuruNo column. The definition of this column is below:
@Field(store = Store.YES)
@Column(name = "BasvuruNo", length = 30)//13/95088973/0735/000001
@Analyzer(definition = "whitespaceanalyzer")
private String basvuruNo;
Solved
Hibernate Search doesn't support intercepting update changes applied using a query.
I would recommend to rewrite the DAO pattern to use Hibernate friendly patterns: by using actual objects and setters on your domain model.
You'll have several other benefits from it, such as:
- make it possible to enable 2nd level caching.
- benefit from dirty checking: avoid unnecessary database connections and operations.
- high performance state processing, should you want to enable bytecode instrumentation or similar.
- Hibernate Envers, Hibernate Search, and other tools properly integrated.
- you'll also be saving loads of memory at runtime: much better performance.
A method like that would look like something like this:
public void updateBasvuruNo(K entityId, Class type, String newBasvuruNo) {
E yourEntity = currentSession().load( type, entityId );
yourEntity.setBasvuruNo( newBasvuruNo );
}
State on managed entities is flushed to the database as needed, only if needed, and controlled by your application: normally by the transaction scope. So you'll probably not want to use such an helper at all, it's just unnecessary boilerplate.
No comments:
Post a Comment