RESOLVED - BATCH-971: add database writers to iosample

JPA iosample
This commit is contained in:
robokaso
2008-12-18 10:42:46 +00:00
parent 194cc1db69
commit dcb5111b70
6 changed files with 147 additions and 33 deletions

View File

@@ -46,6 +46,7 @@
<config>src/main/resources/jobs/iosample/xml.xml</config>
<config>src/main/resources/jobs/iosample/hibernate.xml</config>
<config>src/main/resources/jobs/hibernateJob.xml</config>
<config>src/main/resources/jobs/iosample/jpa.xml</config>
</configs>
<configSets>
<configSet>
@@ -428,5 +429,18 @@
<config>src/main/resources/simple-job-launcher-context.xml</config>
</configs>
</configSet>
<configSet>
<name><![CDATA[iosample-jpa]]></name>
<allowBeanDefinitionOverriding>true</allowBeanDefinitionOverriding>
<incomplete>false</incomplete>
<configs>
<config>src/main/resources/data-source-context.xml</config>
<config>src/main/resources/data-source-context-init.xml</config>
<config>src/main/resources/jobs/iosample/jpa.xml</config>
<config>src/main/resources/jobs/ioSampleJob.xml</config>
<config>src/main/resources/org/springframework/batch/sample/config/common-context.xml</config>
<config>src/main/resources/simple-job-launcher-context.xml</config>
</configs>
</configSet>
</configSets>
</beansProjectDescription>

View File

@@ -125,6 +125,21 @@
<groupId>org.hibernate</groupId>
<artifactId>com.springsource.org.hibernate</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>com.springsource.org.hibernate.ejb</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>com.springsource.org.hibernate.annotations</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>com.springsource.javax.persistence</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>com.springsource.javax.transaction</artifactId>

View File

@@ -18,30 +18,39 @@ package org.springframework.batch.sample.domain.trade;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "CUSTOMER")
public class CustomerCredit {
@Id
private int id;
private String name;
private BigDecimal credit;
public CustomerCredit(){
}
public CustomerCredit(int id, String name, BigDecimal credit){
private String name;
private BigDecimal credit;
public CustomerCredit() {
}
public CustomerCredit(int id, String name, BigDecimal credit) {
this.id = id;
this.name = name;
this.credit = credit;
}
public String toString() {
return "CustomerCredit [id=" + id +",name=" + name + ", credit=" + credit + "]";
}
return "CustomerCredit [id=" + id + ",name=" + name + ", credit=" + credit + "]";
}
public BigDecimal getCredit() {
return credit;
}
public BigDecimal getCredit() {
return credit;
}
public int getId() {
public int getId() {
return id;
}
@@ -50,31 +59,31 @@ public class CustomerCredit {
}
public void setCredit(BigDecimal credit) {
this.credit = credit;
}
this.credit = credit;
}
public String getName() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public CustomerCredit increaseCreditBy(BigDecimal sum) {
CustomerCredit newCredit = new CustomerCredit();
newCredit.credit = this.credit.add(sum);
newCredit.name = this.name;
newCredit.id = this.id;
return newCredit;
}
public boolean equals(Object o) {
return (o instanceof CustomerCredit) && ((CustomerCredit)o).id==id;
public void setName(String name) {
this.name = name;
}
public CustomerCredit increaseCreditBy(BigDecimal sum) {
CustomerCredit newCredit = new CustomerCredit();
newCredit.credit = this.credit.add(sum);
newCredit.name = this.name;
newCredit.id = this.id;
return newCredit;
}
public boolean equals(Object o) {
return (o instanceof CustomerCredit) && ((CustomerCredit) o).id == id;
}
public int hashCode() {
return id;
}
}

View File

@@ -0,0 +1,13 @@
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="customerCredit" transaction-type="RESOURCE_LOCAL">
<class>org.springframework.batch.sample.domain.trade.CustomerCredit</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="itemReader"
class="org.springframework.batch.item.database.JpaPagingItemReader">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="queryString" value="from CustomerCredit" />
</bean>
<bean id="itemWriter" class="org.springframework.batch.item.database.JpaItemWriter">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="customerCredit" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
</bean>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<!--
Hibernate JPA dialect does not support custom tx isolation levels =>
overwrite with ISOLATION_DEFAULT
-->
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="isolationLevelForCreate" value="ISOLATION_DEFAULT" />
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
</bean>
</beans>

View File

@@ -0,0 +1,19 @@
package org.springframework.batch.sample.iosample;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
"/jobs/iosample/jpa.xml" })
public class JpaFunctionalTests extends AbstractIoSampleTests {
@Override
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
// no-op
}
}