【问题标题】:Can Hibernate generate tables without Spring?Hibernate 可以在没有 Spring 的情况下生成表吗?
【发布时间】:2014-07-04 02:17:01
【问题描述】:

我的项目中有 Hibernate,

但还没有春天。

我想知道是否:

问题一:

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>

【问题讨论】:

    标签: java spring hibernate jpa


    【解决方案1】:

    persistence.xml中的Hibernate注解和方言属性是否足以让Hibernate生成表?

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

    必须有类似 Spring 的东西来查找注解并触发生成事件并启动其他一些事情?

    不,这也是 Hibernate 的工作,只需添加 hibernate.hbm2ddl.auto 属性。此处对此进行了解释:

    【讨论】:

    • 我在某些类中有表和实体注释,并且 ´´ 在 persistence.xml 中。服务器启动控制台没有错误,但没有表生成动作。
    • @merveotesi 是映射在您的 hibernafe.cfg.xml 文件中的那些类吗?
    • 我认为您的 hibernate.cfg.xml 概念与我的 persistence.xml 相同,并且我认为这两者都不符合“自动”生成表的需要,他们希望一些老板告诉他们怎么办,Spring之类的大佬
    • @merveotesi 如果您使用的是 JPA,那么您应该在 persistence.xml 中添加配置。如果您只使用 Hibernate,您应该在 hibernate.cfg.xml 中发布配置。无论如何,在配置文件中,您应该映射类并添加必要的字段以自动生成表。
    • @merveotesi 注释将替换 XML 配置。但是您仍然需要添加persistence.xml 或hibernate.cfg.xml,它们是JPA/Hibernate 将使用的映射类。没有工具可以自动检测你的带有@Entity 注释的类并说看,这是一个可以与JPA 一起使用的类
    猜你喜欢
    • 2015-09-01
    • 2017-10-30
    • 1970-01-01
    • 2018-07-15
    • 2017-10-09
    • 2015-04-13
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多