diff --git a/crud.iml b/crud.iml index 9df0c4b..54c2435 100644 --- a/crud.iml +++ b/crud.iml @@ -12,6 +12,9 @@ + + + @@ -54,6 +57,18 @@ + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 7258689..79c53e9 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,17 @@ 1.8 + + + spring-milestones + Spring Milestones + https://repo.spring.io/libs-milestone + + false + + + + org.datanucleus @@ -188,6 +199,12 @@ --> + + org.springframework.data + spring-data-jpa + 2.0.0.M4 + + diff --git a/src/main/java/com/example/crud/ApplicationConfig.java b/src/main/java/com/example/crud/ApplicationConfig.java new file mode 100644 index 0000000..aa8c26e --- /dev/null +++ b/src/main/java/com/example/crud/ApplicationConfig.java @@ -0,0 +1,32 @@ +package com.example.crud; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +@Configuration +@EnableJpaRepositories +@EnableJpaAuditing +@EnableTransactionManagement +class ApplicationConfig { + + @Bean + public EntityManagerFactory entityManagerFactory() { + EntityManagerFactory emf = Persistence.createEntityManagerFactory("crud"); + return emf; + } + + @Bean + public PlatformTransactionManager transactionManager() { + JpaTransactionManager txManager = new JpaTransactionManager(); + txManager.setEntityManagerFactory(entityManagerFactory()); + return txManager; + } +} diff --git a/src/main/java/com/example/crud/Main.java b/src/main/java/com/example/crud/Main.java index 5b153c9..d93d1a7 100644 --- a/src/main/java/com/example/crud/Main.java +++ b/src/main/java/com/example/crud/Main.java @@ -14,8 +14,10 @@ import javax.persistence.Query; import com.example.crud.entities.*; +import com.example.crud.repositories.InventoryRepository; import org.datanucleus.enhancer.DataNucleusEnhancer; import org.datanucleus.util.NucleusLogger; +import org.springframework.data.jpa.repository.support.JpaRepositoryFactory; /** * Controlling application for the DataNucleus Tutorial using JPA. @@ -158,6 +160,13 @@ public class Main { } System.out.println(""); + em = emf.createEntityManager(); + JpaRepositoryFactory factory = new JpaRepositoryFactory(em); + InventoryRepository repository = factory.getRepository(InventoryRepository.class); + Inventory inventory = repository.findByName("My Inventory"); + System.out.println("SpringData/JPA: " + inventory.toString()); + em.close(); + // Clean out the database em = emf.createEntityManager(); tx = em.getTransaction(); diff --git a/src/main/java/com/example/crud/repositories/InventoryRepository.java b/src/main/java/com/example/crud/repositories/InventoryRepository.java new file mode 100644 index 0000000..678a1bd --- /dev/null +++ b/src/main/java/com/example/crud/repositories/InventoryRepository.java @@ -0,0 +1,19 @@ +package com.example.crud.repositories; + +import com.example.crud.entities.Inventory; +import com.example.crud.entities.Product; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; + +public interface InventoryRepository extends JpaRepository { + + Inventory findByName(String name); + + @Query(value = "select * from inventory where product_id_eid contains :productId allow filtering", + nativeQuery = true) + List findByProduct(@Param("productId") String productId); + +} diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 47e29e0..865c000 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -69,11 +69,11 @@ - +