Hello,
I have been having trouble trying to get the Hibernate tools reverse engineering to generate my hbm.xml files with dynamic-update="true". I am trying to implement optimistic locking and according to the hibernate docs, dynamic-update has to be set to true. Can someone help me out here?
Here's my custom ReverseEngineeringStrategy.
Code:
package com.voyager.vpd.hibernate.reveng;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.TableIdentifier;
public class VPDReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {
private static Logger log = Logger.getLogger(VPDReverseEngineeringStrategy.class);
private static final String COL_OPTIMISTIC_LOCK = "ModifiedDate";
public VPDReverseEngineeringStrategy (ReverseEngineeringStrategy delegate) {
super(delegate);
}
/**
* Overrides the default generation strategy from assigned to native.
*/
@Override
public String getTableIdentifierStrategyName(TableIdentifier tableIdentifier) {
String configuredName = super.getTableIdentifierStrategyName(tableIdentifier);
if (configuredName == null) {
configuredName = "native";
log.debug("Used default strategy of " + configuredName);
}else {
log.debug("Used configured strategy of " + configuredName);
}
return configuredName;
}
/**
* Overrides the default collection name generation.
* Fixes generation of names like 'AccessKeyies', 'Surveyies', etc...
*/
@Override
public String foreignKeyToCollectionName(String keyname, TableIdentifier fromTable, List fromColumns,
TableIdentifier referencedTable, List referencedColumns, boolean uniqueReference) {
String collectionName = fromTable.getName();
if (collectionName.matches(".*[a,e,i,o,u]y$")) {
collectionName += "s";
log.debug("Set name configured as " + collectionName);
}else {
collectionName = super.foreignKeyToCollectionName(keyname, fromTable, fromColumns, referencedTable, referencedColumns, uniqueReference);
log.debug("Set name configured as " + collectionName + "(hibernate tools default)");
}
return collectionName;
}
@Override
public boolean useColumnForOptimisticLock(TableIdentifier identifier, String column) {
if (column.equals(COL_OPTIMISTIC_LOCK)) {
log.debug("using " + column + " for optimistic locking");
return true;
}else {
return false;
}
}
@Override
public String getOptimisticLockColumnName(TableIdentifier identifier) {
log.debug("called getOptimisticLockColumnName on table: " + identifier.getName());
return COL_OPTIMISTIC_LOCK;
}
}
Thanks