Created
February 12, 2011 19:45
-
-
Save huljas/824042 to your computer and use it in GitHub Desktop.
Example on how you could use Hibernate schema update when creating database migrations for play applications.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package play.modules.migrate; | |
import org.hibernate.dialect.MySQL5Dialect; | |
import org.hibernate.ejb.Ejb3Configuration; | |
import org.hibernate.tool.hbm2ddl.DatabaseMetadata; | |
import play.Play; | |
import play.db.DB; | |
import play.db.DBPlugin; | |
import play.utils.Utils; | |
import javax.persistence.Entity; | |
import java.io.File; | |
import java.sql.SQLException; | |
import java.util.List; | |
import java.util.Properties; | |
/** | |
* Example on how you could use Hibernate schema update when creating database migrations for play applications. | |
* | |
* @author huljas | |
*/ | |
public class PrintSchemaUpdate { | |
public static void main(String[] args) throws SQLException { | |
String id = ""; | |
if (args.length > 0) { | |
id = args[0]; | |
} | |
Ejb3Configuration configuration = new Ejb3Configuration(); | |
Play.init(new File("."), id); | |
configuration.setProperty("hibernate.dialect", "play.db.jpa.MySQLDialect"); | |
Properties fromConf = (Properties) Utils.Maps.filterMap(Play.configuration, "^hibernate\\..*"); | |
configuration.addProperties(fromConf); | |
List<Class> classes = Play.classloader.getAnnotatedClasses(Entity.class); | |
for (Class clazz : classes) { | |
configuration.addAnnotatedClass(clazz); | |
} | |
configuration.buildEntityManagerFactory(); | |
DBPlugin plugin = new DBPlugin(); | |
plugin.onApplicationStart(); | |
DatabaseMetadata metadata = new DatabaseMetadata(DB.getConnection(), new MySQL5Dialect()); | |
String[] sa = configuration.getHibernateConfiguration().generateSchemaUpdateScript(new MySQL5Dialect(), metadata); | |
System.out.println("Schema update scripts:"); | |
for (String s : sa) { | |
System.out.println(s); | |
} | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment