Added multi-store example to Spring Data REST examples.

This app demonstrates how to mix Spring Data JPA and Spring Data MongoDB together (with Spring Data REST on top) to demonstrate how to split them up properly.
This commit is contained in:
Greg Turnquist
2014-07-30 11:35:14 -05:00
committed by Oliver Gierke
parent 56ab6b4705
commit 401e141012
8 changed files with 447 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import example.person.Person;
import example.person.PersonRepository;
import example.treasure.Treasure;
import example.treasure.TreasureRepository;
/**
* Application configuration file. Used for bootstrap and data setup.
*
* @author Greg Turnquist
* @author Oliver Gierke
*/
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories
@EnableMongoRepositories
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired PersonRepository personRepository;
@Autowired TreasureRepository treasureRepository;
@PostConstruct
void checkitOut() {
personRepository.save(new Person("Frodo", "Baggins"));
personRepository.save(new Person("Bilbo", "Baggins"));
for (Person person : personRepository.findAll()) {
log.info("Hello " + person.toString());
}
treasureRepository.deleteAll();
treasureRepository.save(new Treasure("Sting", "Made by the Elves"));
treasureRepository.save(new Treasure("Sauron's ring", "One ring to rule them all"));
for (Treasure treasure : treasureRepository.findAll()) {
log.info("Found treasure " + treasure.toString());
}
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.person;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import lombok.Data;
import lombok.RequiredArgsConstructor;
/**
* A person.
*
* @author Greg Turnquist
* @author Oliver Gierke
*/
@Entity
@Data
@RequiredArgsConstructor
public class Person {
private @Id @GeneratedValue Long id;
private final String firstName;
private final String lastName;
protected Person() {
this(null, null);
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.person;
import org.springframework.data.repository.CrudRepository;
/**
* A repository to manage {@link Person} instances.
*
* @author Greg Turnquist
* @author Oliver Gierke
*/
public interface PersonRepository extends CrudRepository<Person, Long> {}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.treasure;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* A treasure.
*
* @author Greg Turnquist
* @author Oliver Gierke
*/
@Data
@Document
@RequiredArgsConstructor
public class Treasure {
private @Id String id;
private final String name;
private final String description;
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.treasure;
import org.springframework.data.repository.CrudRepository;
/**
* A repository to manage {@link Treasure}s.
*
* @author Greg Turnquist
* @author Oliver Gierke
*/
public interface TreasureRepository extends CrudRepository<Treasure, String> {}