再次运行应用程序时,将从数据库中删除值

时间:2013-02-19 16:37:31

标签: java spring hibernate zk

我在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>

DataSource.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>

Hibernate.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>   

Controller / Viewmodel类

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”以检查被触发的查询,但没有被触发的删除查询。 我在这里还有其他重要的事情。

3 个答案:

答案 0 :(得分:0)

请参阅注释并阅读Hibernate手册here

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

设置上面的属性在启动时删除并重新创建数据库模式

请考虑以下来自hibernate手册的评论。

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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