在如何配置Hibernate逆向工程工具(在Eclipse上)以定义域类fetch = FetchType.EAGER的属性方面,我找不到任何解决方案。
PS:默认情况下,定义fetch = FetchType.LAZY的所有外键
例如,我在reveng.xml中定义了这一点,它适用于主键:
<table name="fin_expence">
<primary-key>
<generator class="identity"></generator>
</primary-key>
</table>我需要这样的解决方案。
<foreign-key constraint-name="rel_expence_to_tag">
<set lazy="false"></set>
</foreign-key>我已经测试了这段代码,但是它没有工作,它抛出了一个错误。有人能帮忙吗?谢谢。
发布于 2015-09-11 08:20:42
我用查找和替换task.Below解决了这个问题,这是一项验证任务,它将取代懒惰而渴望在逆向工程过程中的所有关系,我认为这对你有帮助。
public class BeanValidationTask {
private static final Logger LOGGER = LoggerFactory
.getLogger(BeanValidationTask.class);
private final String MANY_TO_ONE_EAGER = "ManyToOne(fetch=FetchType.EAGER";
private final String MANY_TO_ONE_LAZY = "ManyToOne(fetch=FetchType.LAZY";
private final String ONE_TO_ONE_LAZY_CHILD_REFERENCE = "@OneToOne(fetch=FetchType.LAZY)";
private final String ONE_TO_ONE_EAGER_CHILD_REFERENCE = "@OneToOne(fetch=FetchType.EAGER) @PrimaryKeyJoinColumn";
private final String ONE_TO_ONE_LAZY_PARENT_REFERENCE = "@OneToOne(fetch=FetchType.EAGER,";
private final String ONE_TO_ONE_LAZY_TRANSIENT_PARENT_REFERENCE = "@Transient @OneToOne(fetch=FetchType.EAGER,";
private final String ONE_TO_MANY = "OneToMany(fetch=FetchType.LAZY";
private final String ONE_TO_MANY_CASCADE = "OneToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL}";
private final String PRIMARY_KEY_JOIN_COLUMN_IMPORT = "javax.persistence.PrimaryKeyJoinColumn;";
private final String IMPORT = "import ";
private File searchDirectory;
public BeanValidationTask(File searchDirectory) {
this.searchDirectory = searchDirectory;
}
/**
* It changes the below fetch type condition to EAGER.
*
* @ManyToOne(fetch=FetchType.LAZY to @ManyToOne(fetch=FetchType.EAGER , cascade = {CascadeType.ALL}
* @OneToOne(fetch=FetchType.LAZY to @OneToOne(fetch=FetchType.EAGER
*/
private String changeLazyToEagerAndCascade(String content) {
try {
content = StringUtils.replace(content, MANY_TO_ONE_LAZY, MANY_TO_ONE_EAGER);
content = addImportStatement(content,PRIMARY_KEY_JOIN_COLUMN_IMPORT);
content = StringUtils.replace(content, ONE_TO_ONE_LAZY_CHILD_REFERENCE, ONE_TO_ONE_EAGER_CHILD_REFERENCE);
content = StringUtils.replace(content, ONE_TO_ONE_LAZY_PARENT_REFERENCE, ONE_TO_ONE_LAZY_TRANSIENT_PARENT_REFERENCE);
if (!content.contains(ONE_TO_MANY_CASCADE)) {
content = StringUtils.replace(content, ONE_TO_MANY, ONE_TO_MANY_CASCADE);
}
return content;
} catch (Exception ex) {
LOGGER.error("Change fetch type from LAZY to EAGER failed for relationship 1-1 and 1-M in file {} at path{}. Root cause {}", ExceptionUtils.getRootCauseMessage(ex));
return "";
}
}
private String addImportStatement(String content, final String importStatement) {
final StringBuffer importString = new StringBuffer(IMPORT).append(importStatement).append("\n").append(IMPORT);
content = StringUtils.replaceOnce(content, IMPORT, importString.toString());
return content;
}
public void execute() {
String[] extensions = new String[]{"java"};
if (searchDirectory.exists()) {
Collection<File> filesInDir = FileUtils.listFiles(searchDirectory, extensions, false);
for (File file : filesInDir)
try {
String content = WMFileUtils.readFileToString(file);
content = this.changeLazyToEagerAndCascade(content);
WMFileUtils.writeStringToFile(file, content);
} catch (IOException e) {
LOGGER.error("Failed to add/update validation into java class file", file.getAbsolutePath(), ExceptionUtils.getRootCauseMessage(e));
}
}
}
}https://stackoverflow.com/questions/30523744
复制相似问题