Have a Snippet?

Keep, track and share your code snippets with your friends



Java: Creating scripts from Hibernate model classes Share on Vkontakte

This main method shows how get scripts created by Hibernate

import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.MySQLDialect;
import com.globallogic.volokh.beans.Authority;
import com.globallogic.volokh.beans.Contact;

/**
 * @author danylo.volokh
 * 
 *         This class is made to get Creation schema script generated by
 *         Hibernate. Need this dependency
 * 
 *          
 *                              org.hibernate.common
 *                              hibernate-commons-annotations
 *                              4.0.1.Final 
 *         
 * 
 */
public class HibernateScriptCreator {

        public static void main(String[] args) {
                Configuration cfg = new Configuration();
// add classes that are models and add Dialect depends witch DB are used
                cfg.addAnnotatedClass(Authority.class);
                cfg.addAnnotatedClass(Contact.class);
                String[] lines = cfg.generateSchemaCreationScript(new MySQLDialect());

                System.out.println(lines);
                System.out.println(lines.length);
                for (int i = 0; i < lines.length; i++) {
                        System.out.println(lines[i] + ";");
                }
        }

}


Tag: Java, Database, Project

0 Comments