hibernate.cfg.xml中dialect 配置方言为org.hibernate.dialect.MySQLInnoDBDialect是不能自动建表,改为org.hibernate.dialect.MySQLDialect或org.hibernate.dialect.MySQL5Dialect
另外hbm2ddl.auto设置为update即可
另附SessionFactoryFactory
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
public static SessionFactory buildSessionFactory(){
try{
// Create the SessionFactory from hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build());
return sf;
}catch (Throwable ex){
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation filaed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}