Java &引用;hibernate.hbm2ddl.auto;物业/创造及保养;使现代化

Java &引用;hibernate.hbm2ddl.auto;物业/创造及保养;使现代化,java,mysql,spring,hibernate,Java,Mysql,Spring,Hibernate,我尝试在我的项目中向数据库中添加一个具有某些功能的Person实例。我使用mysql和jpa注释。我已经使用hibernate.hbm2ddl.auto>create自动创建了mysql表,如果您将hibernate.hbm2ddl.auto设置为create它将在每次重新启动服务器时创建一个新的模式,以便旧表数据将被删除。您希望将hibernate.hbm2ddl.auto保持为update. 此外,如果不能在数据库中持久保存Person对象,则通常是刷新模式的问题。除非刷新会话,否则数据不会

我尝试在我的项目中向数据库中添加一个具有某些功能的Person实例。我使用mysql和jpa注释。我已经使用
hibernate.hbm2ddl.auto>create自动创建了mysql表,如果您将
hibernate.hbm2ddl.auto
设置为
create
它将在每次重新启动服务器时创建一个新的模式,以便旧表数据将被删除。您希望将
hibernate.hbm2ddl.auto
保持为
update.


此外,如果不能在数据库中持久保存Person对象,则通常是刷新模式的问题。除非刷新会话,否则数据不会持久保存在数据库中(&D)。检查刷新模式,它不应该是
NEVER

hibernate.hbm2ddl.auto
用于DDL(创建|更改|添加表),而不用于持久性操作请发布dao类代码我理解创建和更新之间的区别,感谢解释。是的,我无法将person实例持久化到数据库中。在dao类中,会话仅在addPerson()中刷新。我添加了dao类,请检查是否缺少某些内容?logger.info(“person已成功保存,person Details=“+p”);在日志中填充p的值合适吗?不,这就是问题所在,我在日志中没有看到这个logger.info值。单击“添加人员”按钮后,我添加了日志中显示的内容。单击“添加人员”按钮时,请检查浏览器控制台,如果其中包含人员值,请检查发送到服务器的数据。另外,在服务器端代码读取控制器中person对象的位置放置一个调试器。放置调试器将帮助您找到失踪人员:)单击“添加人员”按钮后看到空白的浏览器控制台是否奇怪?:)
    <?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>
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