Skip to content
Permalink
Browse files
spring data added
  • Loading branch information
fiammara committed Oct 12, 2019
1 parent e4129a9 commit d40d8996a926c682f4368954b6b9e06288717e56
Showing 7 changed files with 70 additions and 16 deletions.
@@ -95,7 +95,11 @@
<artifactId>xml-apis</artifactId>
<version>${xml.apis.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${springframework.version}</version>
</dependency>
</dependencies>

<build>
@@ -15,11 +15,13 @@
import org.apache.juneau.rest.springboot.JuneauRestInitializer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

/**
* Entry point for PetStore application.
*/
@SpringBootApplication
@EnableJpaRepositories(basePackages = "org.apache.juneau.petstore")
@SuppressWarnings("javadoc")
public class App {

@@ -0,0 +1,10 @@
package org.apache.juneau.petstore.repository;

import org.apache.juneau.petstore.dto.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface OrderRepository extends JpaRepository <Order, Long>{

}
@@ -0,0 +1,10 @@
package org.apache.juneau.petstore.repository;

import org.apache.juneau.petstore.dto.Pet;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PetRepository extends JpaRepository <Pet, Long> {

}
@@ -0,0 +1,14 @@
package org.apache.juneau.petstore.repository;



import org.apache.juneau.petstore.dto.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository <User, Long>{

User findByUsername(String username);
Long deleteByUsername(String username);
}
@@ -22,8 +22,15 @@
import org.apache.juneau.json.*;
import org.apache.juneau.parser.*;
import org.apache.juneau.petstore.dto.*;
import org.apache.juneau.pojotools.*;
import org.apache.juneau.pojotools.SearchArgs;
import org.apache.juneau.petstore.repository.OrderRepository;
import org.apache.juneau.petstore.repository.PetRepository;
import org.apache.juneau.petstore.repository.UserRepository;
//import org.apache.juneau.pojotools.*;
//import org.apache.juneau.pojotools.SearchArgs;
import org.springframework.beans.factory.annotation.Autowired;




/**
* Pet store database application.
@@ -37,6 +44,15 @@
*/
public class PetStoreService extends AbstractPersistenceService {


@Autowired
private PetRepository petRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private OrderRepository orderRepository;
//-----------------------------------------------------------------------------------------------------------------
// Initialization methods.
//-----------------------------------------------------------------------------------------------------------------
@@ -104,7 +120,7 @@ public PetStoreService initDirect(PrintWriter w) throws ParseException, IOExcept
* @throws IdNotFound If pet was not found.
*/
public Pet getPet(long id) throws IdNotFound {
return find(Pet.class, id);
return petRepository.getOne(id);
}

/**
@@ -115,7 +131,7 @@ public Pet getPet(long id) throws IdNotFound {
* @throws IdNotFound If order was not found.
*/
public Order getOrder(long id) throws IdNotFound {
return find(Order.class, id);
return orderRepository.getOne(id);
}

/**
@@ -128,7 +144,7 @@ public Order getOrder(long id) throws IdNotFound {
*/
public User getUser(String username) throws InvalidUsername, IdNotFound {
assertValidUsername(username);
return find(User.class, username);
return userRepository.findByUsername(username);
}

/**
@@ -137,7 +153,7 @@ public User getUser(String username) throws InvalidUsername, IdNotFound {
* @return All pets in the database.
*/
public List<Pet> getPets() {
return query("select X from PetstorePet X", Pet.class, (SearchArgs)null, (PageArgs)null);
return petRepository.findAll();
}

/**
@@ -146,7 +162,7 @@ public List<Pet> getPets() {
* @return All orders in the database.
*/
public List<Order> getOrders() {
return query("select X from PetstoreOrder X", Order.class, (SearchArgs)null, (PageArgs)null);
return orderRepository.findAll();
}

/**
@@ -155,7 +171,7 @@ public List<Order> getOrders() {
* @return All users in the database.
*/
public List<User> getUsers() {
return query("select X from PetstoreUser X", User.class, (SearchArgs)null, (PageArgs)null);
return userRepository.findAll();
}

/**
@@ -165,6 +181,7 @@ public List<User> getUsers() {
* @return a new {@link Pet} object.
*/
public Pet create(CreatePet c) {

return merge(new Pet().status(PetStatus.AVAILABLE).apply(c));
}

@@ -233,8 +250,7 @@ public User update(User u) throws IdNotFound, InvalidUsername {
* @throws IdNotFound Pet was not found.
*/
public void removePet(long id) throws IdNotFound {
EntityManager em = getEntityManager();
remove(em, find(em, Pet.class, id));
petRepository.deleteById(id);
}

/**
@@ -244,8 +260,7 @@ public void removePet(long id) throws IdNotFound {
* @throws IdNotFound Order was not found.
*/
public void removeOrder(long id) throws IdNotFound {
EntityManager em = getEntityManager();
remove(em, find(em, Order.class, id));
orderRepository.deleteById(id);
}

/**
@@ -255,8 +270,7 @@ public void removeOrder(long id) throws IdNotFound {
* @throws IdNotFound User was not found.
*/
public void removeUser(String username) throws IdNotFound {
EntityManager em = getEntityManager();
remove(em, find(em, User.class, username));
userRepository.deleteByUsername(username);
}

/**
@@ -26,7 +26,7 @@
<property name="javax.persistence.jdbc.user" value="" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
</properties>

0 comments on commit d40d899

Please sign in to comment.