Tuesday, June 11, 2013

JPA example to backup the packages/tables of oracle database

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


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
{
//do not include the schema prefix
private static final String NO_SCHEMA = "BEGIN DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'EMIT_SCHEMA', false); END;";
//make it look pretty
private static final String PRETTY = "BEGIN DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'PRETTY', true); END;";
//don't include the physical properties
private static final String NO_PHYSICAL_PROP = "BEGIN DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'PHYSICAL_PROPERTIES', false); END;";
//don't include the segment attributes
private static final String NO_SEGMENT_PROP = "BEGIN DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SEGMENT_ATTRIBUTES', false); END;";
//include a sql terminator for each statement
private static final String SQL_TERMINATOR = "BEGIN DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR', true); END;";
private static final String SQL_OJBECTS = "select object_name from user_objects where object_type = 'PACKAGE' and instr(object_name,'TEST')=0 order by 1";
@SuppressWarnings("unchecked")
public static void main( String[] args ) throws IOException
{
long start = System.currentTimeMillis();
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();
for(String objectName:objectNames){
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();
long end = System.currentTimeMillis();
System.
out.printf("Done. Spent %d secs.", (end - start) / 1000);
}
private static String getDDL(EntityManager em,String type,String objectName){
String sql =
"select DBMS_METADATA.GET_DDL('"+"PACKAGE_SPEC"+"','"+objectName+"') from DUAL";
return em.createNativeQuery(sql).getSingleResult().toString();
}
public static void write(String fileStr,String content) throws IOException{
BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileStr)));
writer.append(content);
writer.newLine();
writer.close();
}
}

1 comment: