我以發現解決了這個問題,並更換task.Below是將取代懶惰渴望逆向工程process.I所有關係beanvalidationtask認爲這將幫助你。
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));
}
}
}
}