java - "hibernate.hbm2ddl.auto"属性/创建&更新

标签 java mysql spring hibernate

我尝试将具有某些功能的 Person 实例添加到我的项目中的数据库中。我使用 mysql 和 jpa 注释。我已经使用 hibernate.hbm2ddl.auto>create< 自动创建了 mysql 表。在MySQLWorkBench现在,我想使用“添加人员”按钮添加新实体并将它们列在 JSF View 页面上。但是,它不会向数据库添加新实例。我认为,我应该更改 hibernate.hbm2ddl.auto>create<hibernate.hbm2ddl.auto>update<hibernate.hbm2ddl.auto>create-update< .

但是,它们都不起作用并向数据库添加新实体。

applicationContext.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />


    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <beans:property name="url"
            value="jdbc:mysql://localhost:3306/TestDB" />
        <beans:property name="username" value="root" />
        <beans:property name="password" value="secret_root_password" />
    </beans:bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.springhibernatejsf.model.Person</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
                <beans:prop key="hibernate.hbm2ddl.auto">create</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

    <beans:bean id="personDAO"
        class="com.springhibernatejsf.dao.PersonDAOImpl">
        <beans:property name="sessionFactory"
            ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>
    <beans:bean id="personService"
        class="com.springhibernatejsf.service.PersonServiceImpl">
        <beans:property name="personDAO" ref="personDAO"></beans:property>
    </beans:bean>

    <context:component-scan base-package="com.springhibernatejsf" />

    <tx:annotation-driven transaction-manager="transactionManager" />

    <beans:bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory"
            ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>

</beans:beans>

我应该如何修改 hibernate 属性才能添加新实体?

编辑:

PersonDAOImpl.java:

package com.springhibernatejsf.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.springhibernatejsf.model.Person;

@Repository
public class PersonDAOImpl implements PersonDAO {

    private static final Logger logger = LoggerFactory.getLogger(PersonDAOImpl.class);

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sf){
        this.sessionFactory = sf;
    }
    @Override
    public void addPerson(Person p){
        Session session = this.sessionFactory.getCurrentSession();
        session.persist(p);
        session.flush();
        logger.info("Person saved successfully, Person Details="+p);
    }
    @SuppressWarnings("unchecked")
    @Override
    public List<Person> listPersons() {
        Session session = this.sessionFactory.getCurrentSession();
        List<Person> personsList = session.createQuery("from Person").list();
        for(Person p : personsList){
            logger.info("Person List::"+p);
        }
        return personsList;
    }
}

点击“添加人员”按钮后,日志中显示以下内容:

    21:47:34.021 [http-bio-8085-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtaining JDBC connection
21:47:34.021 [http-bio-8085-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtained JDBC connection
21:47:34.021 [http-bio-8085-exec-4] DEBUG o.h.e.t.spi.AbstractTransactionImpl - begin
21:47:34.021 [http-bio-8085-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - initial autocommit status: true
21:47:34.021 [http-bio-8085-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - disabling autocommit
21:47:34.022 [http-bio-8085-exec-4] DEBUG org.hibernate.SQL - select person0_.id as id1_0_, person0_.country as country2_0_, person0_.name as name3_0_ from PERSON person0_
Hibernate: select person0_.id as id1_0_, person0_.country as country2_0_, person0_.name as name3_0_ from PERSON person0_
21:47:34.023 [http-bio-8085-exec-4] DEBUG o.h.e.t.spi.AbstractTransactionImpl - committing
21:47:34.023 [http-bio-8085-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - committed JDBC Connection
21:47:34.023 [http-bio-8085-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - re-enabling autocommit
21:47:34.023 [http-bio-8085-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Releasing JDBC connection
21:47:34.023 [http-bio-8085-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Released JDBC connection
21:47:34.033 [http-bio-8085-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtaining JDBC connection
21:47:34.034 [http-bio-8085-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtained JDBC connection
21:47:34.034 [http-bio-8085-exec-4] DEBUG o.h.e.t.spi.AbstractTransactionImpl - begin
21:47:34.034 [http-bio-8085-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - initial autocommit status: true
21:47:34.034 [http-bio-8085-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - disabling autocommit
21:47:34.034 [http-bio-8085-exec-4] DEBUG o.h.e.t.spi.AbstractTransactionImpl - committing
21:47:34.034 [http-bio-8085-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - committed JDBC Connection
21:47:34.034 [http-bio-8085-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - re-enabling autocommit
21:47:34.034 [http-bio-8085-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Releasing JDBC connection
21:47:34.035 [http-bio-8085-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Released JDBC connection
21:47:34.037 [http-bio-8085-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtaining JDBC connection
21:47:34.037 [http-bio-8085-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtained JDBC connection
21:47:34.037 [http-bio-8085-exec-4] DEBUG o.h.e.t.spi.AbstractTransactionImpl - begin
21:47:34.037 [http-bio-8085-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - initial autocommit status: true
21:47:34.037 [http-bio-8085-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - disabling autocommit
21:47:34.038 [http-bio-8085-exec-4] DEBUG org.hibernate.SQL - select person0_.id as id1_0_, person0_.country as country2_0_, person0_.name as name3_0_ from PERSON person0_
Hibernate: select person0_.id as id1_0_, person0_.country as country2_0_, person0_.name as name3_0_ from PERSON person0_
21:47:34.039 [http-bio-8085-exec-4] DEBUG o.h.e.t.spi.AbstractTransactionImpl - committing
21:47:34.039 [http-bio-8085-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - committed JDBC Connection
21:47:34.039 [http-bio-8085-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - re-enabling autocommit
21:47:34.039 [http-bio-8085-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Releasing JDBC connection
21:47:34.039 [http-bio-8085-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Released JDBC connection

最佳答案

仅供引用,如果您将 hibernate.hbm2ddl.auto 设置为 create,它将在每次重新启动服务器时创建一个新架构,以便删除旧表数据。您更愿意将 hibernate.hbm2ddl.auto 保留为 update。

此外,如果您无法将 Person 对象持久保存在数据库中,通常是刷新模式的问题。除非您的 session 被刷新,否则数据不会保留在数据库中。检查刷新模式,它不应该是NEVER

关于java - "hibernate.hbm2ddl.auto"属性/创建&更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29986284/

相关文章:

java - 为什么 Java 调度程序在 Windows 上表现出明显的时间漂移​​?

java - native 查询返回 Spring Boot 中 #pageable 的所有记录

mysql - 将 Apache Hadoop 数据输出存储到 Mysql 数据库

MySQL,权限问题

java - Ant 中的目标覆盖

java - Java 日志记录、Log4J、Logback 的性能和内存占用

mysql - 如何从数据文件中恢复 MySQL 表?

java - 如何从 RestTemplate 获取重定向的 url?

java - @Where 子句在 hibernate 连接查询中不起作用

java - Spring Boot 无法正确渲染 View