Spring + Hibernate + ZK 프레임 워크에서 샘플 응용 프로그램을 만들었습니다. Hibernate 구성 파일에서 hbm2ddl.auto 속성을 "update"모드로 유지했습니다. 아직도 응용 프로그램을 다시 실행할 때마다 이전에 값을 유지하지 않습니다. 다음응용 프로그램을 다시 실행할 때 값이 데이터베이스에서 삭제됩니다.
은 참조를 위해 몇 가지 구성 파일입니다
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>
컨트롤러/뷰 모델 클래스
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"로 설정했지만 해고되는 삭제 쿼리는 없습니다. 여기에서 누락 된 다른 중요한 것이 있습니까?
구성 파일에서 hbm2ddl.auto 옵션을 주석 처리했지만 응용 프로그램을 다시 실행하면 값이 데이터베이스에서 제거됩니다. –
이상합니다. 서버에서 실행중인 주석 코드가 있고 이전 코드 또는 캐시 된 코드가 없습니까? –
당신은 맞습니다 @ 진짜. 고마워. –