Hibernate不会创建表,但也不会抛出任何错误

时间:2016-10-07 13:04:00

标签: java mysql hibernate tomcat

Hibernate不会创建任何表:

我有一台运行jsf/hibernate项目的Tomcat服务器。数据库服务器是MySQL服务器。开始没有问题,但不创建任何表。

我创建了一个没有Tomcat服务器和其他任何东西的新项目。只有Hibernate相关的代码。仍然没有错误和警告,但也没有创建表。

Hibernate配置(hibernate.cfg.xml):

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/3bt_database</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="hbm2ddl.auto">create</property>
        <mapping class="model.Testtable"/>
    </session-factory>
</hibernate-configuration>

的HibernateUtil

public class HibernateUtil {

    private static final SessionFactory sessionfactory = buildSessionFactory();

    public static SessionFactory buildSessionFactory() throws HibernateException {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();

            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            return configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionfactory(){
        return sessionfactory;
    }
}

StartStopListener

@WebListener
public class StartStopListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        HibernateUtil.getSessionfactory().openSession();
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        HibernateUtil.getSessionfactory().close();
    }
}

TestTable的

@Entity
@Table(name="tblTest")
public class Testtable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int testID;
    private String testname;
}

请不要告诉我手动创建它们。这是我正在寻找的答案。

2 个答案:

答案 0 :(得分:0)

尝试在每个属性名称中添加 hibernate。

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/3bt_database</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <mapping class="model.Testtable"/>
    </session-factory>
</hibernate-configuration>

更新: - 再提供一处房产。试着开始..

 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    //or try with
 <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

答案 1 :(得分:0)

(代表OP发布)

我解决了这个问题。 XML中的映射设置无法正常工作。 This link非常帮助我。