I have downloaded a hsqldb.jar and I set to project buildpath,then next I wrote the program
Class.forName(\"org.hsqldb.jdbc.JDBCDriver\");
Connection conn
You can dump your HSQL in-memory database.
AbstractTestDao:
public abstract class AbstractTestDao {
/**
* Generic method for persisting entities into database.
*
* @param entity Java object from a (hibernate) class annotated with @Entity
*/
void persistEntity(T entity) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.persist(entity);
session.beginTransaction().commit();
session.close();
}
/**
* Generic method for retrieving entities from database.
*
* @param cls Object class
* @param id Entity unique identifier (Primary Key)
* @return Casted Entity object from db
*/
T getEntityById(Class cls, Long id) {
Session session = HibernateUtil.getSessionFactory().openSession();
T entity = cls.cast(session.load(cls, id));
session.close();
return entity;
}
/**
* Don't forget to close your connection so that the in-mem db can release it resources.
*
* @return Connection object to the in-memory db HyperSQL.
* @throws SQLException
*/
public static Connection getNewConn() throws SQLException {
return DriverManager.getConnection("jdbc:hsqldb:mem:testdb;shutdown=false", "SA", "");
}
/**
* Makes a script from the current in-memory database and place a txt file in a directory.
* The script describes the current in-mem db including constrains.
*
* @param testName A String representing the junit test name used for describing the file.
* @throws SQLException
*/
void createDatabaseScript(final String testName) throws SQLException {
final String filePath = new File("").getAbsolutePath();
final String targetDir = "/target/Temp/Databasedump/";
final String directoryPath = filePath.concat(targetDir);
final File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdir();
}
final String fileName = uniqueFileNameGenerator(directoryPath, testName);
final Connection conn = getNewConn();
Statement st = null;
ResultSet rs = null;
try {
st = conn.createStatement();
//Example: st.executeQuery("SCRIPT '../Temp/dump4.txt'")
final String sqlStatement = String.format("SCRIPT '%s%s'", directoryPath, fileName);
rs = st.executeQuery(sqlStatement);
} finally {
if (st != null) {
st.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
}
/**
* Search for a unique filename in a directory with 1 to 2147483647 (Integer.MAX_VALUE) possibilities per date.
* May throw a StackOverflowError if all 2147483646 file names are given away.
*
* @param directoryPath - String representing the directory you want to use.
* @param name - String representing the filename you want to use.
* @return String representing a unique filename
*/
private String uniqueFileNameGenerator(final String directoryPath, final String name) {
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
final String todayDate = dateFormat.format(Calendar.getInstance().getTime());
final Integer randomNumber = (int) (Math.random() * Integer.MAX_VALUE + 1);
final String fileName = name + "_" + todayDate + "_" + randomNumber + ".txt";
File file = new File(directoryPath + fileName);
if (file.exists()) {
return uniqueFileNameGenerator(directoryPath, name);
}
return fileName;
}
}
HSQL DB version:
hsqldb
hsqldb
1.8.0.10