1. table을 하나 만듭니다. 저는 scott 계정으로 만들었습니다.
Table name : T_ACCOUNT
columns : "CASHBALANCE" NUMBER
"NAME" VARCHAR2(25 BYTE) NOT NULL
sample 데이터를 등록합니다.
500 John Smith
1000 Bernard Dupont
1500 Bart Simpson
2. Spring Source Tool 에서 Simple Hibernate Utility Project를 만듭니다.
File -> new -> Spring template project -> Simple Hibernate Utility Project
package name : com.inho.hibernate
3. pom.xml 에 dependency를 추가 합니다.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples.spring</groupId>
<artifactId>spring-hibernate-utility</artifactId>
<version>1.0.0.CI-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring Hibernate Utility</name>
<url>http://www.springframework.org</url>
<description>
<![CDATA[This project is a minimal jar utility with Spring configuration for Hibernate.]]>
</description>
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<spring.framework.version>3.1.1.RELEASE</spring.framework.version>
<sl4j.version>1.5.6</sl4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.6.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.156</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<!--
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
</dependency>
-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- ORACLE JDBC driver-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.6.Final</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jcl</artifactId>
<version>${sl4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${sl4j.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.0alpha</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>org.jboss.repository.releases</id>
<name>JBoss Maven Release Repository</name>
<url>https://repository.jboss.org/nexus/content/repositories/releases</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
4. Account.java 파일을 생성합니다.
package com.inho.hibernate;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="T_ACCOUNT")
public class Account {
@Id
private String name;
private double cashBalance;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getCashBalance() {
return cashBalance;
}
public void setCashBalance(double cashBalance) {
this.cashBalance = cashBalance;
}
@Override
public String toString() {
return "balance: " + cashBalance + ", name: " + name;
}
}
5. Oracle 연결을 위한 Configuration
HibernateConfiguration.java 파일을 아래와 같이 수정합니다.
package com.inho.hibernate;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.hibernate.dialect.H2Dialect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
public class HibernateConfiguration {
/*
@SuppressWarnings("deprecation")
@Bean
public DataSource dataSource() {
return new DriverManagerDataSource(
"oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@127.0.0.1:1521:ORCL",
"scott",
"tiger"
);
}
@Bean
public SessionFactory sessionFactory() {
Properties props = new Properties();
// props.put("hibernate.dialect", H2Dialect.class.getName());
props.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
props.put("hibernate.format_sql", "true");
props.put("hibernate.show_sql", "true");
AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
// bean.setAnnotatedClasses(new Class[]{Item.class, Order.class});
bean.setAnnotatedClasses(new Class[]{Emp.class});
bean.setHibernateProperties(props);
bean.setDataSource(this.dataSource());
bean.setSchemaUpdate(true);
return (SessionFactory) bean;
}
@Bean
public PlatformTransactionManager transactionManager() {
return new HibernateTransactionManager( sessionFactory());
}
*/
@Bean
public SessionFactory sessionFactory() {
return new LocalSessionFactoryBuilder(datasource()).addAnnotatedClass(Account.class).buildSessionFactory();
}
@Bean
public PlatformTransactionManager transactionManager() {
return new HibernateTransactionManager(sessionFactory());
}
@SuppressWarnings("deprecation")
@Bean
public DataSource datasource() {
return new DriverManagerDataSource(
"oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@127.0.0.1:1521:ORCL",
"scott",
"tiger"
);
}
/* @Bean
public DataSource datasource() {
EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean();
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
bean.setDatabasePopulator(databasePopulator);
bean.afterPropertiesSet();
return bean.getObject();
}*/
}
7. Test
Project Explorer 에서 OrderPersistenceTest.java 파일을 확장하여 retrieveAccount() 에 마우스 우클릭하여 JUnit 테스트를 진행합니다.
* JUnit 테스트 방법 : Run as -> JUnit test
2012-09-04 10:46:39,997 INFO [org.springframework.test.context.support.AbstractContextLoader] - <Detected default resource location "classpath:/com/inho/hibernate/OrderPersistenceTests-context.xml" for test class [com.inho.hibernate.OrderPersistenceTests].>
2012-09-04 10:46:40,011 INFO [org.springframework.test.context.support.DelegatingSmartContextLoader] - <GenericXmlContextLoader detected default locations for context configuration [ContextConfigurationAttributes@5d173 declaringClass = 'com.inho.hibernate.OrderPersistenceTests', locations = '{classpath:/com/inho/hibernate/OrderPersistenceTests-context.xml}', classes = '{}', inheritLocations = true, contextLoaderClass = 'org.springframework.test.context.ContextLoader'].>
2012-09-04 10:46:40,011 INFO [org.springframework.test.context.support.AnnotationConfigContextLoader] - <Could not detect default configuration classes for test class [com.inho.hibernate.OrderPersistenceTests]: OrderPersistenceTests does not declare any static, non-private, non-final, inner classes annotated with @Configuration.>
2012-09-04 10:46:40,015 INFO [org.springframework.test.context.TestContextManager] - <@TestExecutionListeners is not present for class [class com.inho.hibernate.OrderPersistenceTests]: using defaults.>
2012-09-04 10:46:40,869 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Loading XML bean definitions from class path resource [com/inho/hibernate/OrderPersistenceTests-context.xml]>
2012-09-04 10:46:43,250 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Loading XML bean definitions from class path resource [META-INF/spring/app-context.xml]>
2012-09-04 10:46:44,596 INFO [org.springframework.context.support.GenericApplicationContext] - <Refreshing org.springframework.context.support.GenericApplicationContext@95fd19: startup date [Tue Sep 04 10:46:44 KST 2012]; root of context hierarchy>
2012-09-04 10:46:47,577 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18bf072: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,hibernateConfiguration,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0,proxyTransactionManagementConfiguration,transactionAttributeSource,transactionInterceptor,sessionFactory,transactionManager,datasource]; root of factory hierarchy>
2012-09-04 10:46:49,890 INFO [org.springframework.jdbc.datasource.DriverManagerDataSource] - <Loaded JDBC driver: oracle.jdbc.driver.OracleDriver>
2012-09-04 10:46:50,275 INFO [org.hibernate.annotations.common.Version] - <HCANN000001: Hibernate Commons Annotations {4.0.1.Final}>
2012-09-04 10:46:50,356 INFO [org.hibernate.Version] - <HHH000412: Hibernate Core {4.1.6.Final}>
2012-09-04 10:46:50,360 INFO [org.hibernate.cfg.Environment] - <HHH000206: hibernate.properties not found>
2012-09-04 10:46:50,362 INFO [org.hibernate.cfg.Environment] - <HHH000021: Bytecode provider name : javassist>
2012-09-04 10:46:54,445 INFO [org.hibernate.dialect.Dialect] - <HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect>
2012-09-04 10:46:54,683 INFO [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] - <HHH000399: Using default transaction strategy (direct JDBC transactions)>
2012-09-04 10:46:54,708 INFO [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] - <HHH000397: Using ASTQueryTranslatorFactory>
2012-09-04 10:47:00,556 DEBUG [org.hibernate.SQL] - <select account0_.name as name0_, account0_.cashBalance as cashBala2_0_ from T_ACCOUNT account0_ where account0_.name=?>
balance: 1000.0, name: Bernard Dupont
2012-09-04 10:47:01,124 INFO [org.springframework.context.support.GenericApplicationContext] - <Closing org.springframework.context.support.GenericApplicationContext@95fd19: startup date [Tue Sep 04 10:46:44 KST 2012]; root of context hierarchy>
2012-09-04 10:47:01,125 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18bf072: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,hibernateConfiguration,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0,proxyTransactionManagementConfiguration,transactionAttributeSource,transactionInterceptor,sessionFactory,transactionManager,datasource]; root of factory hierarchy>
SpringBatch 에서 외부 parameter 값을 사용할 때. (0) | 2012.09.18 |
---|---|
[Spring batch] File to DB (0) | 2012.09.04 |
Spring Batch Example 2 - DB to DB (1) (5) | 2012.07.24 |
Spring batch example 1 (0) | 2012.07.17 |
Spring 외부 properties 설정 (0) | 2012.07.03 |
댓글 영역