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

@@ -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
}
}