Hibernate可以在没有Spring的情况下生成表吗?

时间:2014-05-15 14:41:55

标签: java spring hibernate jpa

我的项目有Hibernate,

但还没有春天。

我想知道是否:

问题1:

persistence.xml中的Hibernate注释和方言属性足以让Hibernate生成表吗?

问题2: 另外我想,必须有类似Spring的东西来寻找注释并解雇生成事件并开始其他一些事情吗?

编辑:

我的persistence.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="Kutuphane2" transaction-type="RESOURCE_LOCAL">
        <non-jta-data-source>kutuphaneDS</non-jta-data-source>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/kutuphane"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="root"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        </properties>
    </persistence-unit>
</persistence>

1 个答案:

答案 0 :(得分:5)

  

persistence.xml中的Hibernate注释和方言属性足以让Hibernate生成表吗?

是的,Hibernate独立工作,Spring只是帮助Hibernate提供数据源并帮助您访问Hibernate API。

  

必须有类似Spring的东西来寻找注释并解雇生成事件并开始其他事情吗?

不,这也是Hibernate的工作,只需添加hibernate.hbm2ddl.auto属性即可。这在这里解释:

相关问题