Java Reference
In-Depth Information
Now you can begin updating
formatJob
. Let's begin with the only code you need to write: the
annotations you add to the
Customer
class to map it to the database. Listing 9-32 shows the
Customer
class updated.
Listing 9-32.
Customer.java
Mapped to the Customer Table
package com.apress.springbatch.chapter9;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="customer")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String firstName;
private String middleInitial;
private String lastName;
private String address;
private String city;
private String state;
private String zip;
// Accessors go here
....
}
The annotations you use here are the same as the ones you used in the ItemReader example in
Chapter 7. The mapping for the
Customer
class is pretty straightforward because the column names of
the Customer table match those of the
Customer
class. The other thing to notice is that you aren't using
any Hibernate-specific annotations. All the annotations used here are JPA-supported annotations, which
allows you to switch from Hibernate to any JPA-supported implementation if you choose with no code
changes required.
Next, you can move on to configuring the
SessionFactory
. Again, the configuration here is the same
that you used in Chapter 7 for Hibernate's ItemReader implementations. You configure the
SessionFactory
and the Hibernate-supported transaction manager both in the
launch-context.xml
file.
In addition, you add a
hibernate.cfg.xml
file to the root of the
resources
directory. Listing 9-33 shows
the configuration updates you need to make to the
launch-context.xml
file.