I performed the backup task through JDBC originally, however, found it's very easy to switch to JPA.
It's impressed that it's very easy to use JPA2/Hibernate to backup database or generate the package files.
JPA2/Hibernate only brings a minor performance impact (can be ignored). It's much easier to program and the program is more elegant.
The cost is to 1) introduce additional jars 2) study JPA/Hibernate
JPA2/Hibernate only brings a minor performance impact (can be ignored). It's much easier to program and the program is more elegant.
The cost is to 1) introduce additional jars 2) study JPA/Hibernate
To run the example, need persistence.xml and *.jar
import
java.io.BufferedWriter;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.OutputStreamWriter;
import
java.util.List;
import
javax.persistence.EntityManager;
import
javax.persistence.EntityManagerFactory;
import
javax.persistence.Persistence;
public
class PackageFilesGenerator
{
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory(
"DEV");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createNativeQuery(
NO_SCHEMA).executeUpdate();
em.createNativeQuery(
PRETTY).executeUpdate();
em.createNativeQuery(
NO_PHYSICAL_PROP).executeUpdate();
em.createNativeQuery(
NO_SEGMENT_PROP).executeUpdate();
em.createNativeQuery(
SQL_TERMINATOR).executeUpdate();
List<String> objectNames = (List<String>)em.createNativeQuery(
SQL_OJBECTS).getResultList();
String ddlSpec = getDDL(em,
"PACKAGE_SPEC",objectName);
write(
"pks."+objectName.toLowerCase()+".sql",ddlSpec);
String ddlBody = getDDL(em,
"PACKAGE_BODY",objectName);
write(
"pkb."+objectName.toLowerCase()+".sql",ddlBody);
}
em.getTransaction().commit();
em.close();
System.
out.printf("Done. Spent %d secs.", (end - start) / 1000);
}
String sql =
"select DBMS_METADATA.GET_DDL('"+"PACKAGE_SPEC"+"','"+objectName+"') from DUAL";
}
BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileStr)));
writer.append(content);
writer.newLine();
writer.close();
}
}
Nice. Exactly what I am looking for. Thanks.
ReplyDelete