SpringData/JPA 2.0.0.M4 basics work.

This commit is contained in:
Greg Burd 2017-06-20 16:23:18 -04:00
parent dae5301201
commit 0446fdeb30
6 changed files with 95 additions and 3 deletions

View file

@ -12,6 +12,9 @@
<deploymentDescriptor name="persistence.xml" url="file://$MODULE_DIR$/src/main/resources/META-INF/persistence.xml" /> <deploymentDescriptor name="persistence.xml" url="file://$MODULE_DIR$/src/main/resources/META-INF/persistence.xml" />
</configuration> </configuration>
</facet> </facet>
<facet type="Spring" name="Spring">
<configuration />
</facet>
</component> </component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" /> <output url="file://$MODULE_DIR$/target/classes" />
@ -54,6 +57,18 @@
<orderEntry type="library" name="Maven: com.github.jnr:jnr-x86asm:1.0.2" level="project" /> <orderEntry type="library" name="Maven: com.github.jnr:jnr-x86asm:1.0.2" level="project" />
<orderEntry type="library" name="Maven: com.github.jnr:jnr-posix:3.0.27" level="project" /> <orderEntry type="library" name="Maven: com.github.jnr:jnr-posix:3.0.27" level="project" />
<orderEntry type="library" name="Maven: com.github.jnr:jnr-constants:0.9.0" level="project" /> <orderEntry type="library" name="Maven: com.github.jnr:jnr-constants:0.9.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-jpa:2.0.0.M4" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.0.0.M4" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-orm:5.0.0.RC2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.0.0.RC2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.0.0.RC2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.0.0.RC2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.0.0.RC2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.0.0.RC2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.0.0.RC2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.0.0.RC2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.0.0.RC2" level="project" />
<orderEntry type="library" name="Maven: org.aspectj:aspectjrt:1.8.10" level="project" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.16.16" level="project" /> <orderEntry type="library" name="Maven: org.projectlombok:lombok:1.16.16" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" /> <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" /> <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />

17
pom.xml
View file

@ -15,6 +15,17 @@
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
</properties> </properties>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.datanucleus</groupId> <groupId>org.datanucleus</groupId>
@ -188,6 +199,12 @@
</dependency> </dependency>
--> -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.0.0.M4</version>
</dependency>
<!-- ... --> <!-- ... -->
<dependency> <dependency>

View file

@ -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;
}
}

View file

@ -14,8 +14,10 @@ import javax.persistence.Query;
import com.example.crud.entities.*; import com.example.crud.entities.*;
import com.example.crud.repositories.InventoryRepository;
import org.datanucleus.enhancer.DataNucleusEnhancer; import org.datanucleus.enhancer.DataNucleusEnhancer;
import org.datanucleus.util.NucleusLogger; import org.datanucleus.util.NucleusLogger;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
/** /**
* Controlling application for the DataNucleus Tutorial using JPA. * Controlling application for the DataNucleus Tutorial using JPA.
@ -158,6 +160,13 @@ public class Main {
} }
System.out.println(""); 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 // Clean out the database
em = emf.createEntityManager(); em = emf.createEntityManager();
tx = em.getTransaction(); tx = em.getTransaction();

View file

@ -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, String> {
Inventory findByName(String name);
@Query(value = "select * from inventory where product_id_eid contains :productId allow filtering",
nativeQuery = true)
List<Inventory> findByProduct(@Param("productId") String productId);
}

View file

@ -69,11 +69,11 @@
<logger name="com.datastax.driver.core.QueryLogger.SLOW" level="INFO"> <logger name="com.datastax.driver.core.QueryLogger.SLOW" level="INFO">
<appender-ref ref="console"/> <appender-ref ref="console"/>
</logger> </logger>
<!--
<logger name="com.datastax.driver.core.QueryLogger.NORMAL" level="TRACE"> <logger name="com.datastax.driver.core.QueryLogger.NORMAL" level="INFO">
<appender-ref ref="console"/> <appender-ref ref="console"/>
</logger> </logger>
-->
<logger name="com.datastax.driver.core.QueryLogger.FAST" level="INFO"> <logger name="com.datastax.driver.core.QueryLogger.FAST" level="INFO">
<appender-ref ref="console"/> <appender-ref ref="console"/>
</logger> </logger>