" hibernate.hbm2ddl.auto" property / create& update

时间:2015-05-01 11:47:04

标签: java mysql spring hibernate

我尝试在我的项目中将具有一些功能的Person实例添加到数据库中。我使用mysql和jpa annotation.I在hibernate.hbm2ddl.auto>create<中自动创建了MySQLWorkBench的mysql表。现在,我想要使用&#34; Add Person&#34;添加新实体按钮并在JSF视图页面上列出它们。但是,它没有向数据库添加新实例。我认为,我应该将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;
    }
}

点击&#34;添加人物&#34;按钮,我在日志中有以下内容:

    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

0 个答案:

没有答案