Add support for marking a repository with @Region rather than the entity
This commit is contained in:
@@ -102,10 +102,18 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
private GemfireTemplate getTemplate(RepositoryMetadata metadata) {
|
||||
|
||||
Class<?> repositoryClass = metadata.getRepositoryInterface();
|
||||
Class<?> domainClass = metadata.getDomainType();
|
||||
GemfirePersistentEntity<?> entity = context.getPersistentEntity(domainClass);
|
||||
|
||||
GemfirePersistentEntity<?> entity;
|
||||
if (repositoryClass.isAnnotationPresent(org.springframework.data.gemfire.mapping.Region.class)) {
|
||||
entity = context.getPersistentEntity(repositoryClass);
|
||||
}
|
||||
else {
|
||||
entity = context.getPersistentEntity(domainClass);
|
||||
}
|
||||
|
||||
Region<?, ?> region = regions.getRegion(domainClass);
|
||||
Region<?, ?> region = regions.getRegion(entity.getRegionName());
|
||||
|
||||
if (region == null) {
|
||||
throw new IllegalStateException(String.format(
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.springframework.data.gemfire.repository.sample;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
public class Animal {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Foo [id=" + id + ", name=" + name + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.springframework.data.gemfire.repository.sample;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
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;
|
||||
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class AnimalRepositoryTest {
|
||||
|
||||
@Autowired
|
||||
CatRepository catRepo;
|
||||
|
||||
@Autowired
|
||||
DogRepository dogRepo;
|
||||
|
||||
@Test
|
||||
public void foo() {
|
||||
|
||||
Animal felix = new Animal();
|
||||
felix.setId(1);
|
||||
felix.setName("Felix");
|
||||
|
||||
Animal leo = new Animal();
|
||||
leo.setId(2);
|
||||
leo.setName("Leo");
|
||||
|
||||
Animal fido = new Animal();
|
||||
fido.setId(1);
|
||||
fido.setName("Fido");
|
||||
|
||||
Animal leo_ = catRepo.save(leo);
|
||||
assertNotNull(leo_);
|
||||
|
||||
Animal felix_ = catRepo.save(felix);
|
||||
assertNotNull(felix_);
|
||||
|
||||
Animal fido_ = dogRepo.save(fido);
|
||||
assertNotNull(fido_);
|
||||
|
||||
assertEquals(2L, catRepo.count());
|
||||
assertEquals(1L, dogRepo.count());
|
||||
|
||||
Animal foundFelix = catRepo.findOne(1L);
|
||||
assertEquals(felix, foundFelix);
|
||||
|
||||
Animal findOther = catRepo.findByName("Leo");
|
||||
assertEquals(leo, findOther);
|
||||
|
||||
Animal foundFido = dogRepo.findBy("Fido");
|
||||
assertEquals(fido, foundFido);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.springframework.data.gemfire.repository.sample;
|
||||
|
||||
import org.springframework.data.gemfire.mapping.Region;
|
||||
import org.springframework.data.gemfire.repository.GemfireRepository;
|
||||
import org.springframework.data.gemfire.repository.Query;
|
||||
|
||||
@Region("Cats")
|
||||
public interface CatRepository extends GemfireRepository<Animal, Long> {
|
||||
|
||||
Animal findByName(String name);
|
||||
|
||||
@Query("SELECT * FROM /Foo x WHERE x.name = $1")
|
||||
Animal findBy(String name);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.springframework.data.gemfire.repository.sample;
|
||||
|
||||
import org.springframework.data.gemfire.mapping.Region;
|
||||
import org.springframework.data.gemfire.repository.GemfireRepository;
|
||||
import org.springframework.data.gemfire.repository.Query;
|
||||
|
||||
@Region("Dogs")
|
||||
public interface DogRepository extends GemfireRepository<Animal, Long> {
|
||||
|
||||
Animal findByName(String name);
|
||||
|
||||
@Query("SELECT * FROM /FooTwo x WHERE x.name = $1")
|
||||
Animal findBy(String name);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:gfe="http://www.springframework.org/schema/gemfire"
|
||||
xmlns:gfe-data="http://www.springframework.org/schema/data/gemfire"
|
||||
xmlns:util="http://www.springframework.org/schema/util" 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
|
||||
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
|
||||
http://www.springframework.org/schema/data/gemfire http://www.springframework.org/schema/data/gemfire/spring-data-gemfire.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<gfe:annotation-driven/>
|
||||
|
||||
<util:properties id="peerCacheConfigurationSettings">
|
||||
<prop key="name">fooRepositoryTest</prop>
|
||||
<prop key="locators">localhost[11236]</prop>
|
||||
<prop key="log-level">config</prop>
|
||||
<prop key="mcast-port">0</prop>
|
||||
<prop key="start-locator">localhost[11236]</prop>
|
||||
</util:properties>
|
||||
|
||||
<gfe:cache />
|
||||
|
||||
<gfe:replicated-region
|
||||
id="Cats"
|
||||
key-constraint="java.lang.Long"
|
||||
value-constraint="org.springframework.data.gemfire.repository.sample.Animal"
|
||||
persistent="false" />
|
||||
|
||||
<gfe:replicated-region
|
||||
id="Dogs"
|
||||
key-constraint="java.lang.Long"
|
||||
value-constraint="org.springframework.data.gemfire.repository.sample.Animal"
|
||||
persistent="false" />
|
||||
|
||||
<gfe-data:repositories
|
||||
base-package="org.springframework.data.gemfire.repository.sample" />
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user