1

我在 Spring + Hibernate + ZK 框架中制作了一个示例应用程序。在 Hibernate 配置文件中,我将属性 hbm2ddl.auto 保持在“更新”模式。仍然每当我再次运行应用程序时,我都没有得到更早保留的值。

以下是几个配置文件供参考:

BeanLocations.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!-- Database Configuration -->
    <import resource="../database/DataSource.xml"/>
    <import resource="../database/Hibernate.xml"/>

    <!-- Auto scan the components -->
    <context:component-scan 
        base-package="com.nagarro" />

</beans>

数据源.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>properties/database.properties</value>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- <property name="hibernate.current_session_context_class" value = "${hibernate.current_session_context_class}" /> -->
    </bean>

</beans>

休眠.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
    xmlns:tx="http://www.springframework.org/schema/tx">

    <!-- Hibernate session factory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>

            </props>
        </property>

        <property name="annotatedClasses">
            <list>

                <value>com.nagarro.model.ItemAttribute</value>
                <value>com.nagarro.model.ItemAttributeGroup</value>
            </list>
        </property>
    </bean>

</beans>   

控制器/视图模型类

public class ItemAttributeRenderer {

    /*
     * The Logger reference variable used for Logging.
     */
    static private final Logger LOG = LoggerFactory.getLogger(ItemAttributeRenderer.class);

    @Autowired
    private ItemAttributeService itemAttrService;

    private List<ItemAttribute> itemList = new ArrayList<ItemAttribute>();

    private void prepareAndSaveItemAttributeData(){
        ItemAttribute item = null;
        int counter;
        for(int index = 0; index < 5; index++){
            counter = index+1;
            item = new ItemAttribute("Item "+counter, "Attirbute Value"+counter, "Qualifier Value"+counter);
            itemAttrService.save(item);
        }
    }
    /**
     * @return the itemList
     */
    public List<ItemAttribute> getItemList() {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring/config/BeanLocations.xml");
        itemAttrService = (ItemAttributeService)appContext.getBean("itemAttrService");
        prepareAndSaveItemAttributeData();
        itemList = itemAttrService.getItemAttributeList();
        return itemList;
    }

    @Command
    public void save(){
        for(ItemAttribute item : itemList){
            itemAttrService.update(item);
        }

    }

    /**
     * @param itemList the itemList to set
     */
    public void setItemList(List<ItemAttribute> itemList) {
        this.itemList = itemList;
    }
    /**
     * @return the itemAttrService
     */
    public ItemAttributeService getItemAttrService() {
        return itemAttrService;
    }
    /**
     * @param itemAttrService the itemAttrService to set
     */

    public void setItemAttrService(ItemAttributeService itemAttrService) {
        this.itemAttrService = itemAttrService;
    }



}

查看文件:

<?page title="Result"?>
<zk>
    <custom-attributes center="${arg.center }" />
    <window apply="org.zkoss.bind.BindComposer"
        viewModel="@id('vm') @init('com.nagarro.viewmodel.ItemAttributeRenderer')"
        title="Result Window" border="normal">
        <div>
            <grid>
                <rows>
                    <row>
                        <listbox model="@bind(vm.itemList)">
                            <listhead>
                                <listheader label="Item Name"
                                    style="text-align:center;">
                                </listheader>
                                <listheader label="Attribute Value"
                                    style="text-align:center;">
                                </listheader>
                                <listheader label="Qualifier Value"
                                    style="text-align:center;">
                                </listheader>
                            </listhead>
                            <template name="model" var="item">
                                <listitem value="${item }">
                                    <listcell label="@load(item.name)"
                                        style="text-align:center;">

                                    </listcell>
                                    <listcell style="text-align:center;">
                                    <textbox value="@bind(item.attributeValue)"
                                        style="text-align:center;"/>

                                    </listcell>
                                    <listcell
                                        label="@load(item.qualifierValue)"
                                        style="text-align:center;">
                                    </listcell>
                                </listitem>
                            </template>
                        </listbox>
                    </row>
                </rows>
            </grid>
        </div>
        <div style="text-align:right; padding:10px;">
            <button label="Save" mold="trendy"
                onClick="@command('save')">
            </button>

        </div>
    </window>
</zk>

服务等级

@Service("itemAttrService")
public class ItemAttributeServiceImpl implements ItemAttributeService{

    @Autowired
    ItemAttributeDao itemAttrDao;

    public void setItemAttrDao(ItemAttributeDao itemAttrDao) {
        this.itemAttrDao = itemAttrDao;
    }


    public void save(ItemAttribute item){
        itemAttrDao.save(item);

    }

    public void update(ItemAttribute item){
        itemAttrDao.update(item);

    }

    public void delete(ItemAttribute item){
        itemAttrDao.delete(item);
    }

    public ItemAttribute findByItemAttributeName(String name){
        return itemAttrDao.findByItemAttributeName(name);
    }

    public List<ItemAttribute> getItemAttributeList(){
        return itemAttrDao.getItemAttributeList();
    }

}

DAO 类

@Repository("itemAttrDao")
public class ItemAttributeDaoImpl extends CustomHibernateDaoSupport  implements ItemAttributeDao {


    public void save(ItemAttribute itemAttribute) {
        getHibernateTemplate().save(itemAttribute);
        //getHibernateTemplate().getSessionFactory().getCurrentSession().getTransaction().commit();
    }


    public void update(ItemAttribute itemAttribute) {
        getHibernateTemplate().update(itemAttribute);
        //getHibernateTemplate().getSessionFactory().getCurrentSession().getTransaction().commit();
    }


    public void delete(ItemAttribute itemAttribute) {
        getHibernateTemplate().delete(itemAttribute);
        //getHibernateTemplate().getSessionFactory().getCurrentSession().getTransaction().commit();
    }


    public ItemAttribute findByItemAttributeName(String name) {
        List list = getHibernateTemplate().find("from ItemAttribute where name=?",name);
        if(list == null){

        }else if(list.isEmpty()){

        }
        return (ItemAttribute)list.get(0);
    }


    public List<ItemAttribute> getItemAttributeList() {
        List<ItemAttribute> itemList = getHibernateTemplate().find("from ItemAttribute ");
        return itemList;
    }

}

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.abc</groupId>
    <artifactId>ZKSpringHibernateExample</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SpringHibernateExample</name>
    <url>http://maven.apache.org</url>

    <repositories>
        <repository>
            <id>java.net</id>
            <url>http://download.java.net/maven/2/</url>
        </repository>
    </repositories>
    <properties>
        <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring framework -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>



                    <!-- MySQL database driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>

        <!-- Hibernate framework -->


        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.3.Final</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>


        <!-- Hibernate library dependecy start -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
        </dependency>
        <!-- Hibernate library dependecy end -->

        <!-- ZK Dependency start -->
        <dependency>
            <groupId>org.zkoss.zk</groupId>
            <artifactId>zkplus</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.zkoss.zk</groupId>
            <artifactId>zhtml</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.zkoss.zk</groupId>
            <artifactId>zkbind</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.zkoss.zk</groupId>
            <artifactId>zul</artifactId>
            <version>6.0.0</version>
        </dependency>
        <!-- ZK Dependency Ends -->

    </dependencies>
</project>

我的问题是当我再次运行应用程序时,即使我已将 hbm2ddl.auto 属性设置为更新模式,之前保存的所有数据都会消失。我已将 show_sql 属性设置为“true”以检查被触发的查询,但没有被触发的删除查询。我在这里还缺少其他重要的东西吗?

4

3 回答 3

0

在此处查看评论并阅读 Hibernate 手册

<property name="hbm2ddl.auto">update</property>

在启动时设置以上属性删除并重新创建数据库架构

考虑以下来自休眠手册的评论。

Hibernate 的持久性上下文的自动会话管理在这种情况下特别有用。hbm2ddl.auto 选项打开直接将数据库模式自动生成到数据库中。这也可以通过删除配置选项来关闭,或者在 SchemaExport Ant 任务的帮助下重定向到文件。

于 2013-02-19T16:53:12.303 回答
0

hbm2ddl.auto用于验证数据库模式,这里没有任何意义。

您必须提交您的值,否则它们不会永久存储在数据库中。

于 2013-02-19T16:53:14.777 回答
0

我找到了解决方案。我的 Tomcat 中有很多混乱的项目依赖项,所以我删除了那个 Tomcat 实例并为我的项目重新配置了它。然后我清理了我的项目,现在一切正常。感谢您花时间发布答案。

于 2013-02-22T09:49:33.243 回答