SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-004JPA例子的代码

时间:2021-08-30 21:02:42

一、结构

SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-004JPA例子的代码

二、Repository层

1.

 package spittr.db;

 import java.util.List;

 import spittr.domain.Spitter;

 /**
* Repository interface with operations for {@link Spitter} persistence.
* @author habuma
*/
public interface SpitterRepository { long count(); Spitter save(Spitter spitter); Spitter findOne(long id); Spitter findByUsername(String username); List<Spitter> findAll(); }

2.

 package spittr.db;

 import java.util.List;

 import spittr.domain.Spittle;

 /**
* Repository interface with operations for {@link Spittle} persistence.
* @author habuma
*/
public interface SpittleRepository { long count(); List<Spittle> findRecent(); List<Spittle> findRecent(int count); Spittle findOne(long id); Spittle save(Spittle spittle); List<Spittle> findBySpitterId(long spitterId); void delete(long id); }

3.

 package spittr.db.jpa;

 import java.util.List;

 import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; import spittr.db.SpitterRepository;
import spittr.domain.Spitter; @Repository
public class JpaSpitterRepository implements SpitterRepository { @PersistenceContext
private EntityManager entityManager; public long count() {
return findAll().size();
} public Spitter save(Spitter spitter) {
entityManager.persist(spitter);
return spitter;
} public Spitter findOne(long id) {
return entityManager.find(Spitter.class, id);
} public Spitter findByUsername(String username) {
return (Spitter) entityManager.createQuery("select s from Spitter s where s.username=?").setParameter(1, username).getSingleResult();
} public List<Spitter> findAll() {
return (List<Spitter>) entityManager.createQuery("select s from Spitter s").getResultList();
} }

4.

 package spittr.db.jpa;

 import java.util.List;

 import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; import spittr.db.SpittleRepository;
import spittr.domain.Spittle; @Repository
public class JpaSpittleRepository implements SpittleRepository { @PersistenceContext
private EntityManager entityManager; public long count() {
return findAll().size();
} public List<Spittle> findRecent() {
return findRecent(10);
} public List<Spittle> findRecent(int count) {
return (List<Spittle>) entityManager.createQuery("select s from Spittle s order by s.postedTime desc")
.setMaxResults(count)
.getResultList();
} public Spittle findOne(long id) {
return entityManager.find(Spittle.class, id);
} public Spittle save(Spittle spittle) {
entityManager.persist(spittle);
return spittle;
} public List<Spittle> findBySpitterId(long spitterId) {
return (List<Spittle>) entityManager.createQuery("select s from Spittle s, Spitter sp where s.spitter = sp and sp.id=? order by s.postedTime desc")
.setParameter(1, spitterId)
.getResultList();
} public void delete(long id) {
entityManager.remove(findOne(id));
} public List<Spittle> findAll() {
return (List<Spittle>) entityManager.createQuery("select s from Spittle s").getResultList();
} }

三、domain层

1.

 package spittr.domain;

 import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; @Entity
public class Spitter { private Spitter() {} @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id; @Column(name="username")
private String username; @Column(name="password")
private String password; @Column(name="fullname")
private String fullName; @Column(name="email")
private String email; @Column(name="updateByEmail")
private boolean updateByEmail; public Spitter(Long id, String username, String password, String fullName, String email, boolean updateByEmail) {
this.id = id;
this.username = username;
this.password = password;
this.fullName = fullName;
this.email = email;
this.updateByEmail = updateByEmail;
} public Long getId() {
return id;
} public String getUsername() {
return username;
} public String getPassword() {
return password;
} public String getFullName() {
return fullName;
} public String getEmail() {
return email;
} public boolean isUpdateByEmail() {
return updateByEmail;
} }

2.

 package spittr.domain;

 import java.util.Date;

 import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; @Entity
public class Spittle { private Spittle() {} @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id; @ManyToOne
@JoinColumn(name="spitter")
private Spitter spitter; @Column
private String message; @Column
private Date postedTime; public Spittle(Long id, Spitter spitter, String message, Date postedTime) {
this.id = id;
this.spitter = spitter;
this.message = message;
this.postedTime = postedTime;
} public Long getId() {
return this.id;
} public String getMessage() {
return this.message;
} public Date getPostedTime() {
return this.postedTime;
} public Spitter getSpitter() {
return this.spitter;
} }

四、配置文件及数据库文件

1.

 package spittr.db.jpa;

 import javax.inject.Inject;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer; @Configuration
@ComponentScan
public class JpaConfig { @Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder edb = new EmbeddedDatabaseBuilder();
edb.setType(EmbeddedDatabaseType.H2);
edb.addScript("spittr/db/jpa/schema.sql");
edb.addScript("spittr/db/jpa/test-data.sql");
EmbeddedDatabase embeddedDatabase = edb.build();
return embeddedDatabase;
} @Bean
public LocalContainerEntityManagerFactoryBean emf(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource);
emf.setPersistenceUnitName("spittr");
emf.setJpaVendorAdapter(jpaVendorAdapter);
emf.setPackagesToScan("spittr.domain");
return emf;
} @Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.H2);
adapter.setShowSql(true);
adapter.setGenerateDdl(false);
adapter.setDatabasePlatform("org.hibernate.dialect.H2Dialect");
return adapter;
} @Configuration
@EnableTransactionManagement
public static class TransactionConfig implements TransactionManagementConfigurer {
@Inject
private EntityManagerFactory emf; public PlatformTransactionManager annotationDrivenTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}
}

2.RepositoryTest-context.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:component-scan base-package="spittr.db.jpa" /> <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:persistenceUnitName="spittr"
p:jpaVendorAdapter-ref="jpaVendorAdapter"
p:packagesToScan="spittr.domain" /> <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="H2" />
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
</bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="emf" /> <jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="spittr/db/jpa/schema.sql" />
<jdbc:script location="spittr/db/jpa/test-data.sql" />
</jdbc:embedded-database> </beans>

3.schema.sql

 drop table if exists spittle;
drop table if exists spitter; create table spitter (
id identity,
username varchar(25) not null,
password varchar(25) not null,
fullName varchar(100) not null,
email varchar(50) not null,
updateByEmail boolean not null
); create table spittle (
id integer identity primary key,
spitter integer not null,
message varchar(2000) not null,
postedTime datetime not null,
foreign key (spitter) references spitter(id)
);

4.test-data.sql

 insert into Spitter (username, password, fullname, email, updateByEmail) values ('habuma', 'password', 'Craig Walls', 'craig@habuma.com', false);
insert into Spitter (username, password, fullname, email, updateByEmail) values ('mwalls', 'password', 'Michael Walls', 'mwalls@habuma.com', true);
insert into Spitter (username, password, fullname, email, updateByEmail) values ('chuck', 'password', 'Chuck Wagon', 'chuck@habuma.com', false);
insert into Spitter (username, password, fullname, email, updateByEmail) values ('artnames', 'password', 'Art Names', 'art@habuma.com', true); insert into Spittle (spitter, message, postedTime) values (1, 'This is a test spittle message', '2012-06-09 22:00:00Z');
insert into Spittle (spitter, message, postedTime) values (1, 'This is another test spittle message', '2012-06-09 22:10:00Z');
insert into Spittle (spitter, message, postedTime) values (1, 'This is a third test spittle message', '2012-07-04 23:30:00Z');
insert into Spittle (spitter, message, postedTime) values (2, 'Hello from Chuck!', '2012-03-25 12:15:00Z');
insert into Spittle (spitter, message, postedTime) values (4, 'Hello from Art!', '2012-03-25 12:15:00Z');
insert into Spittle (spitter, message, postedTime) values (4, 'Hello again from Art!', '2012-03-25 12:25:00Z');
insert into Spittle (spitter, message, postedTime) values (4, 'Hola from Arthur!', '2012-03-25 12:35:00Z');
insert into Spittle (spitter, message, postedTime) values (4, 'Buenos Dias from Art!', '2012-03-25 12:45:00Z');
insert into Spittle (spitter, message, postedTime) values (4, 'Ni Hao from Art!', '2012-03-25 12:55:00Z');
insert into Spittle (spitter, message, postedTime) values (4, 'Guten Tag from Art!', '2012-03-25 13:05:00Z');
insert into Spittle (spitter, message, postedTime) values (4, 'Konnichi wa from Art!', '2012-03-25 13:15:00Z');
insert into Spittle (spitter, message, postedTime) values (4, 'Buon giorno from Art!', '2012-03-25 13:25:00Z');
insert into Spittle (spitter, message, postedTime) values (4, 'Bonjour from Art!', '2012-03-25 13:35:00Z');
insert into Spittle (spitter, message, postedTime) values (4, 'Aloha from Art!', '2012-03-25 13:45:00Z');
insert into Spittle (spitter, message, postedTime) values (4, 'God dag from Art!', '2012-03-25 13:55:00Z');

5.log4j.properties

 log4j.rootCategory=INFO, stdout

 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %40.40c:%4L - %m%n

五、测试文件

1.

 package spittr.db.jpa;

 import static org.junit.Assert.*;

 import java.util.List;

 import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional; import spittr.db.SpitterRepository;
import spittr.domain.Spitter; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=JpaConfig.class)
public class SpitterRepositoryTest { @Autowired
SpitterRepository spitterRepository; @Test
@Transactional
public void count() {
assertEquals(4, spitterRepository.count());
} @Test
@Transactional
public void findAll() {
List<Spitter> spitters = spitterRepository.findAll();
assertEquals(4, spitters.size());
assertSpitter(0, spitters.get(0));
assertSpitter(1, spitters.get(1));
assertSpitter(2, spitters.get(2));
assertSpitter(3, spitters.get(3));
} @Test
@Transactional
public void findByUsername() {
assertSpitter(0, spitterRepository.findByUsername("habuma"));
assertSpitter(1, spitterRepository.findByUsername("mwalls"));
assertSpitter(2, spitterRepository.findByUsername("chuck"));
assertSpitter(3, spitterRepository.findByUsername("artnames"));
} @Test
@Transactional
public void findOne() {
assertSpitter(0, spitterRepository.findOne(1L));
assertSpitter(1, spitterRepository.findOne(2L));
assertSpitter(2, spitterRepository.findOne(3L));
assertSpitter(3, spitterRepository.findOne(4L));
} @Test
@Transactional
public void save_newSpitter() {
assertEquals(4, spitterRepository.count());
Spitter spitter = new Spitter(null, "newbee", "letmein", "New Bee", "newbee@habuma.com", true);
Spitter saved = spitterRepository.save(spitter);
assertEquals(5, spitterRepository.count());
assertSpitter(4, saved);
assertSpitter(4, spitterRepository.findOne(5L));
} @Test
@Transactional
@Ignore
public void save_existingSpitter() {
assertEquals(4, spitterRepository.count());
Spitter spitter = new Spitter(4L, "arthur", "letmein", "Arthur Names", "arthur@habuma.com", false);
Spitter saved = spitterRepository.save(spitter);
assertSpitter(5, saved);
assertEquals(4, spitterRepository.count());
Spitter updated = spitterRepository.findOne(4L);
assertSpitter(5, updated);
} private static void assertSpitter(int expectedSpitterIndex, Spitter actual) {
assertSpitter(expectedSpitterIndex, actual, "Newbie");
} private static void assertSpitter(int expectedSpitterIndex, Spitter actual, String expectedStatus) {
Spitter expected = SPITTERS[expectedSpitterIndex];
assertEquals(expected.getId(), actual.getId());
assertEquals(expected.getUsername(), actual.getUsername());
assertEquals(expected.getPassword(), actual.getPassword());
assertEquals(expected.getFullName(), actual.getFullName());
assertEquals(expected.getEmail(), actual.getEmail());
assertEquals(expected.isUpdateByEmail(), actual.isUpdateByEmail());
} private static Spitter[] SPITTERS = new Spitter[6]; @BeforeClass
public static void before() {
SPITTERS[0] = new Spitter(1L, "habuma", "password", "Craig Walls", "craig@habuma.com", false);
SPITTERS[1] = new Spitter(2L, "mwalls", "password", "Michael Walls", "mwalls@habuma.com", true);
SPITTERS[2] = new Spitter(3L, "chuck", "password", "Chuck Wagon", "chuck@habuma.com", false);
SPITTERS[3] = new Spitter(4L, "artnames", "password", "Art Names", "art@habuma.com", true);
SPITTERS[4] = new Spitter(5L, "newbee", "letmein", "New Bee", "newbee@habuma.com", true);
SPITTERS[5] = new Spitter(4L, "arthur", "letmein", "Arthur Names", "arthur@habuma.com", false);
} }

2.

 package spittr.db.jpa;

 import static org.junit.Assert.*;

 import java.util.Date;
import java.util.List; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional; import spittr.db.SpittleRepository;
import spittr.domain.Spitter;
import spittr.domain.Spittle; @RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:spittr/db/jpa/RepositoryTest-context.xml")
@ContextConfiguration(classes=JpaConfig.class)
public class SpittleRepositoryTest { @Autowired
SpittleRepository spittleRepository; @Test
@Transactional
public void count() {
assertEquals(15, spittleRepository.count());
} @Test
@Transactional
public void findRecent() {
// default case
{
List<Spittle> recent = spittleRepository.findRecent();
assertRecent(recent, 10);
} // specific count case
{
List<Spittle> recent = spittleRepository.findRecent(5);
assertRecent(recent, 5);
}
} @Test
@Transactional
public void findOne() {
Spittle thirteen = spittleRepository.findOne(13);
assertEquals(13, thirteen.getId().longValue());
assertEquals("Bonjour from Art!", thirteen.getMessage());
assertEquals(1332682500000L, thirteen.getPostedTime().getTime());
assertEquals(4, thirteen.getSpitter().getId().longValue());
assertEquals("artnames", thirteen.getSpitter().getUsername());
assertEquals("password", thirteen.getSpitter().getPassword());
assertEquals("Art Names", thirteen.getSpitter().getFullName());
assertEquals("art@habuma.com", thirteen.getSpitter().getEmail());
assertTrue(thirteen.getSpitter().isUpdateByEmail());
} @Test
@Transactional
public void findBySpitter() {
List<Spittle> spittles = spittleRepository.findBySpitterId(4L);
assertEquals(11, spittles.size());
for (int i = 0; i < 11; i++) {
assertEquals(15-i, spittles.get(i).getId().longValue());
}
} @Test
@Transactional
public void save() {
assertEquals(15, spittleRepository.count());
Spitter spitter = spittleRepository.findOne(13).getSpitter();
Spittle spittle = new Spittle(null, spitter, "Un Nuevo Spittle from Art", new Date());
Spittle saved = spittleRepository.save(spittle);
assertEquals(16, spittleRepository.count());
assertNewSpittle(saved);
assertNewSpittle(spittleRepository.findOne(16L));
} @Test
@Transactional
public void delete() {
assertEquals(15, spittleRepository.count());
assertNotNull(spittleRepository.findOne(13));
spittleRepository.delete(13L);
assertEquals(14, spittleRepository.count());
assertNull(spittleRepository.findOne(13));
} private void assertRecent(List<Spittle> recent, int count) {
long[] recentIds = new long[] {3,2,1,15,14,13,12,11,10,9};
assertEquals(count, recent.size());
for (int i = 0; i < count; i++) {
assertEquals(recentIds[i], recent.get(i).getId().longValue());
}
} private void assertNewSpittle(Spittle spittle) {
assertEquals(16, spittle.getId().longValue());
} }