我正在尝试使用JPA2元数据来确定从数据库插入/删除行的顺序,因此约束不是问题(稍后将在Java代码中使用)。这是使用JPA的备份/恢复方法的一部分。
这是我的方法:
就绪准备就绪我的意思是填充所有相关的表记录,以便外键对插入有效,或者没有其他表引用此表中的记录。
我确信它会是某种递归方法,但我卡住了。任何帮助都受到欢迎。
到目前为止,这是代码:
/**
* Get the execution order from the EntityManager meta data model.
*
* This will fail if the EntityManager is not JP2 compliant
* @param em EntityManager to get the metadata from
* @return ArrayList containing the order to process tables
*/
protected static ArrayList<String> getProcessingOrder(EntityManager em) {
ArrayList<String> tables = new ArrayList<String>();
//This holds the amount of relationships and the tables with that same amount
HashMap<Integer, ArrayList<String>> tableStats = new HashMap<Integer, ArrayList<String>>();
//This holds the table and the tables referenced by it
HashMap<String, ArrayList<String>> references = new HashMap<String, ArrayList<String>>();
for (EntityType et : em.getMetamodel().getEntities()) {
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.FINER, et.getName());
int amount = 0;
Iterator<SingularAttribute> sIterator = et.getSingularAttributes().iterator();
while (sIterator.hasNext()) {
SingularAttribute next = sIterator.next();
switch (next.getPersistentAttributeType()) {
case BASIC:
case ELEMENT_COLLECTION:
case EMBEDDED:
case ONE_TO_MANY:
case ONE_TO_ONE:
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.FINER,
"Ignoring: {0}", next.getName());
break;
case MANY_TO_MANY:
case MANY_TO_ONE:
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.INFO,
"{3} has a {2} relationship: {0} with: {1}",
new Object[]{next.getName(), next.getBindableJavaType(),
next.getPersistentAttributeType().name(), et.getName()});
if (!references.containsKey(et.getName())) {
references.put(et.getName(), new ArrayList<String>());
}
references.get(et.getName()).add(next.getBindableJavaType().getSimpleName());
amount++;
break;
default:
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.SEVERE,
"Unexpected value: {0}", next.getName());
break;
}
}
Iterator<PluralAttribute> pIterator = et.getPluralAttributes().iterator();
while (pIterator.hasNext()) {
PluralAttribute next = pIterator.next();
switch (next.getPersistentAttributeType()) {
case BASIC:
case ELEMENT_COLLECTION:
case EMBEDDED:
case ONE_TO_MANY:
case MANY_TO_MANY:
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.FINER,
"Ignoring: {0}", next.getName());
break;
case MANY_TO_ONE:
case ONE_TO_ONE:
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.INFO,
"{3} has a {2} relationship: {0} with: {1}",
new Object[]{next.getName(), next.getBindableJavaType(),
next.getPersistentAttributeType().name(), et.getName()});
if (!references.containsKey(et.getName())) {
references.put(et.getName(), new ArrayList<String>());
}
references.get(et.getName()).add(next.getBindableJavaType().getSimpleName());
amount++;
break;
default:
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.SEVERE,
"Unexpected value: {0}", next.getName());
break;
}
}
if (!tableStats.containsKey(amount)) {
tableStats.put(amount, new ArrayList<String>());
}
tableStats.get(amount).add(et.getName());
}
Iterator<String> iterator = references.keySet().iterator();
while (iterator.hasNext()) {
String next = iterator.next();
Iterator<String> iterator1 = references.get(next).iterator();
StringBuilder refs = new StringBuilder();
while (iterator1.hasNext()) {
refs.append(iterator1.next()).append("\n");
}
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.FINER, "References for {0}:\n{1}", new Object[]{next, refs.toString()});
}
//Need to sort entities with relationships even further
ArrayList<String> temp = new ArrayList<String>();
for (Entry<Integer, ArrayList<String>> e : tableStats.entrySet()) {
if (e.getKey() > 0) {
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.INFO, "Tables with {0} references", e.getKey());
for (String t : e.getValue()) {
//Check the relationships of the tables
//Here's where I need help
boolean ready = true;
for (String ref : references.get(t)) {
if (!temp.contains(ref)) {
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.INFO,
"{0} is not ready. Referenced table {1} is not ready yet", new Object[]{t, ref});
ready = false;
}
}
if (ready) {
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.INFO, "{0} is ready.", t);
temp.add(t);
}
}
//-------------------------------------------------------
} else {
temp.addAll(e.getValue());
}
}
for (Entry<Integer, ArrayList<String>> e : tableStats.entrySet()) {
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.FINER,
"Amount of relationships: {0}", e.getKey());
StringBuilder list = new StringBuilder();
for (String t : e.getValue()) {
list.append(t).append("\n");
}
Logger.getLogger(XincoBackupManager.class.getSimpleName()).log(Level.FINER, list.toString());
}
tables.addAll(temp);
return tables;
}
答案 0 :(得分:1)
我将使用JDBC中的数据库元数据解决此问题。
此处使用java.sql.DatabaseMetadata中的以下方法:
// to get the tables
getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)
// to get the reference to the table
public ResultSet getExportedKeys(String catalog,
String schema,
String table)
throws SQLException
我在一些应用程序中使用了这种方法,它运行得很好。
虽然这种方法不遵循JPA元模型的使用,但我认为在JDBC元数据级别上运行更适合您的问题。
由于可能存在难以通过这样的外键依赖关系图处理的循环依赖关系,您可以选择
删除
添加