You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2021/01/27 09:47:24 UTC

[isis-app-helloworld] 02/02: updates to JPA

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch jpa
in repository https://gitbox.apache.org/repos/asf/isis-app-helloworld.git

commit de70f007364c40b5be5849270ab1680319c0138d
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Wed Jan 27 09:46:53 2021 +0000

    updates to JPA
---
 enhance-all.sh                                     |  2 --
 logging-dn-enhance.properties                      | 24 -------------
 pom.xml                                            |  2 +-
 .../domainapp/modules/hello/HelloWorldModule.java  |  6 ++++
 .../modules/hello/dom/hwo/HelloWorldObject.java    | 39 +++++++++++++++-------
 .../modules/hello/dom/hwo/HelloWorldObjects.java   | 20 +++--------
 .../hello/dom/hwo/HelloWorldRepository.java        | 13 ++++++++
 .../java/domainapp/modules/hello/types/Name.java   |  3 --
 .../java/domainapp/modules/hello/types/Notes.java  |  8 ++---
 src/main/java/domainapp/webapp/AppManifest.java    |  4 +--
 src/main/resources/application.yml                 |  4 ++-
 ...jects_Test.java => HelloWorldObjects_Test.java} | 11 +++---
 12 files changed, 65 insertions(+), 71 deletions(-)

diff --git a/enhance-all.sh b/enhance-all.sh
deleted file mode 100644
index 2da7b0f..0000000
--- a/enhance-all.sh
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/usr/bin/env bash
-mvn datanucleus:enhance -o
\ No newline at end of file
diff --git a/logging-dn-enhance.properties b/logging-dn-enhance.properties
deleted file mode 100644
index 281bfb6..0000000
--- a/logging-dn-enhance.properties
+++ /dev/null
@@ -1,24 +0,0 @@
-# LOG4J Configuration
-# ===================
-
-# Basic logging goes to "datanucleus.log"
-log4j.appender.A1=org.apache.log4j.FileAppender
-log4j.appender.A1.File=datanucleus.log
-log4j.appender.A1.layout=org.apache.log4j.PatternLayout
-log4j.appender.A1.layout.ConversionPattern=%d{HH:mm:ss,SSS} (%t) %-5p [%c] - %m%n
-#log4j.appender.A1.Threshold=INFO
-
-# Categories
-# Each category can be set to a "level", and to direct to an appender
-
-# Default to DEBUG level for all DataNucleus categories
-log4j.logger.DataNucleus = DEBUG, A1
-
-log4j.category.com.mchange.v2.c3p0=INFO, A1
-log4j.category.com.mchange.v2.resourcepool=INFO, A1
-log4j.category.org.logicalcobwebs.proxool=INFO,A1
-
-
-# Hbase libs logging
-log4j.category.org.apache.hadoop=INFO,A1
-log4j.category.org.apache.zookeeper=INFO,A1
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index c9c788e..7ab9822 100644
--- a/pom.xml
+++ b/pom.xml
@@ -55,7 +55,7 @@
 
 		<dependency>
 			<groupId>org.apache.isis.mavendeps</groupId>
-			<artifactId>isis-mavendeps-jdo</artifactId>
+			<artifactId>isis-mavendeps-jpa</artifactId>
 			<type>pom</type>
 		</dependency>
 
diff --git a/src/main/java/domainapp/modules/hello/HelloWorldModule.java b/src/main/java/domainapp/modules/hello/HelloWorldModule.java
index df8f52f..b3427c4 100644
--- a/src/main/java/domainapp/modules/hello/HelloWorldModule.java
+++ b/src/main/java/domainapp/modules/hello/HelloWorldModule.java
@@ -1,12 +1,18 @@
 package domainapp.modules.hello;
 
+import org.springframework.boot.autoconfigure.domain.EntityScan;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Import;
+import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
+
+import domainapp.modules.hello.dom.hwo.HelloWorldObject;
 
 @Configuration
 @Import({})
 @ComponentScan
+@EnableJpaRepositories
+@EntityScan(basePackageClasses = {HelloWorldModule.class})
 public class HelloWorldModule {
 
 }
diff --git a/src/main/java/domainapp/modules/hello/dom/hwo/HelloWorldObject.java b/src/main/java/domainapp/modules/hello/dom/hwo/HelloWorldObject.java
index 0207c88..94766d1 100644
--- a/src/main/java/domainapp/modules/hello/dom/hwo/HelloWorldObject.java
+++ b/src/main/java/domainapp/modules/hello/dom/hwo/HelloWorldObject.java
@@ -3,33 +3,46 @@ package domainapp.modules.hello.dom.hwo;
 import java.util.Comparator;
 
 import javax.inject.Inject;
-import javax.jdo.annotations.IdGeneratorStrategy;
-import javax.jdo.annotations.IdentityType;
-import javax.jdo.annotations.VersionStrategy;
 
 import org.apache.isis.applib.annotation.Action;
 import org.apache.isis.applib.annotation.ActionLayout;
 import org.apache.isis.applib.annotation.DomainObject;
 import org.apache.isis.applib.annotation.DomainObjectLayout;
 import org.apache.isis.applib.annotation.MemberOrder;
+import org.apache.isis.applib.annotation.Programmatic;
 import org.apache.isis.applib.annotation.Publishing;
 import org.apache.isis.applib.annotation.SemanticsOf;
 import org.apache.isis.applib.services.message.MessageService;
 import org.apache.isis.applib.services.repository.RepositoryService;
 import org.apache.isis.applib.services.title.TitleService;
+import org.apache.isis.persistence.jpa.applib.integration.JpaEntityInjectionPointResolver;
 
 import domainapp.modules.hello.types.Name;
 import domainapp.modules.hello.types.Notes;
 
-@javax.jdo.annotations.PersistenceCapable(identityType = IdentityType.DATASTORE, schema = "hello" )
-@javax.jdo.annotations.DatastoreIdentity(strategy = IdGeneratorStrategy.IDENTITY, column = "id")
-@javax.jdo.annotations.Version(strategy= VersionStrategy.DATE_TIME, column ="version")
-@javax.jdo.annotations.Unique(name="HelloWorldObject_name_UNQ", members = {"name"})
-@DomainObject(entityChangePublishing = Publishing.ENABLED)
+@javax.persistence.Entity
+@javax.persistence.Table(
+        schema="hello",
+        uniqueConstraints = {
+                @javax.persistence.UniqueConstraint(name = "HelloWorldObject_name_UNQ", columnNames = {"name"})
+        }
+)
+@javax.persistence.EntityListeners(JpaEntityInjectionPointResolver.class) // injection support
+@DomainObject(objectType = "hello.HelloWorldObject", entityChangePublishing = Publishing.ENABLED)
 @DomainObjectLayout()  // causes UI events to be triggered
 public class HelloWorldObject implements Comparable<HelloWorldObject> {
 
-    private HelloWorldObject(){}
+    protected HelloWorldObject(){}
+
+    @javax.persistence.Id
+    @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
+    @javax.persistence.Column(nullable = false)
+    private Long id;
+
+    @javax.persistence.Version
+    @javax.persistence.Column(name = "OPTLOCK")
+    private int version;
+
 
     public HelloWorldObject(final String name) {
         this.name = name;
@@ -40,6 +53,7 @@ public class HelloWorldObject implements Comparable<HelloWorldObject> {
     }
 
     @Name
+    @javax.persistence.Column(length = Name.MAX_LEN, nullable = false)
     @MemberOrder(name = "identity", sequence = "1")
     private String name;
     public String getName() {
@@ -50,6 +64,7 @@ public class HelloWorldObject implements Comparable<HelloWorldObject> {
     }
 
     @Notes
+    @javax.persistence.Column(length = Notes.MAX_LEN, nullable = true)
     @MemberOrder(name = "details", sequence = "1")
     private String notes;
     public String getNotes() {
@@ -93,8 +108,8 @@ public class HelloWorldObject implements Comparable<HelloWorldObject> {
     }
 
 
-    @Inject RepositoryService repositoryService;
-    @Inject TitleService titleService;
-    @Inject MessageService messageService;
+    @Inject @javax.persistence.Transient RepositoryService repositoryService;
+    @Inject @javax.persistence.Transient TitleService titleService;
+    @Inject @javax.persistence.Transient MessageService messageService;
 
 }
diff --git a/src/main/java/domainapp/modules/hello/dom/hwo/HelloWorldObjects.java b/src/main/java/domainapp/modules/hello/dom/hwo/HelloWorldObjects.java
index d92dd8d..c0dd034 100644
--- a/src/main/java/domainapp/modules/hello/dom/hwo/HelloWorldObjects.java
+++ b/src/main/java/domainapp/modules/hello/dom/hwo/HelloWorldObjects.java
@@ -2,8 +2,6 @@ package domainapp.modules.hello.dom.hwo;
 
 import java.util.List;
 
-import javax.jdo.JDOQLTypedQuery;
-
 import org.apache.isis.applib.annotation.Action;
 import org.apache.isis.applib.annotation.ActionLayout;
 import org.apache.isis.applib.annotation.DomainService;
@@ -12,9 +10,7 @@ import org.apache.isis.applib.annotation.PromptStyle;
 import org.apache.isis.applib.annotation.RestrictTo;
 import org.apache.isis.applib.annotation.SemanticsOf;
 import org.apache.isis.applib.services.repository.RepositoryService;
-import org.apache.isis.persistence.jdo.applib.integration.JdoSupportService;
 
-import domainapp.modules.hello.dom.hwo.QHelloWorldObject;
 import domainapp.modules.hello.types.Name;
 
 @DomainService(
@@ -24,13 +20,13 @@ import domainapp.modules.hello.types.Name;
 public class HelloWorldObjects {
 
     private final RepositoryService repositoryService;
-    private final JdoSupportService jdoSupportService;
+    private final HelloWorldRepository helloWorldRepository;
 
     public HelloWorldObjects(
             final RepositoryService repositoryService,
-            final JdoSupportService jdoSupportService) {
+            final HelloWorldRepository helloWorldRepository) {
         this.repositoryService = repositoryService;
-        this.jdoSupportService = jdoSupportService;
+        this.helloWorldRepository = helloWorldRepository;
     }
 
     @Action(semantics = SemanticsOf.NON_IDEMPOTENT)
@@ -48,18 +44,12 @@ public class HelloWorldObjects {
     @ActionLayout(promptStyle = PromptStyle.DIALOG_SIDEBAR)
     public List<HelloWorldObject> findByName(
             @Name final String name) {
-        JDOQLTypedQuery<HelloWorldObject> q = jdoSupportService.newTypesafeQuery(HelloWorldObject.class);
-        final QHelloWorldObject cand = QHelloWorldObject.candidate();
-        q = q.filter(
-                cand.name.indexOf(q.stringParameter("name")).ne(-1)
-                );
-        return q.setParameter("name", name)
-                .executeList();
+        return helloWorldRepository.findByNameContaining(name);
     }
 
     @Action(semantics = SemanticsOf.SAFE, restrictTo = RestrictTo.PROTOTYPING)
     public List<HelloWorldObject> listAll() {
-        return repositoryService.allInstances(HelloWorldObject.class);
+        return helloWorldRepository.findAll();
     }
 
 
diff --git a/src/main/java/domainapp/modules/hello/dom/hwo/HelloWorldRepository.java b/src/main/java/domainapp/modules/hello/dom/hwo/HelloWorldRepository.java
new file mode 100644
index 0000000..c3e40fa
--- /dev/null
+++ b/src/main/java/domainapp/modules/hello/dom/hwo/HelloWorldRepository.java
@@ -0,0 +1,13 @@
+package domainapp.modules.hello.dom.hwo;
+
+import java.util.List;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+
+public interface HelloWorldRepository extends JpaRepository<HelloWorldObject, Long> {
+
+    List<HelloWorldObject> findByNameContaining(final String name);
+
+    HelloWorldObject findByName(final String name);
+
+}
diff --git a/src/main/java/domainapp/modules/hello/types/Name.java b/src/main/java/domainapp/modules/hello/types/Name.java
index f3457b9..553813e 100644
--- a/src/main/java/domainapp/modules/hello/types/Name.java
+++ b/src/main/java/domainapp/modules/hello/types/Name.java
@@ -5,14 +5,11 @@ import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
-import javax.jdo.annotations.Column;
-
 import org.apache.isis.applib.annotation.Editing;
 import org.apache.isis.applib.annotation.Parameter;
 import org.apache.isis.applib.annotation.ParameterLayout;
 import org.apache.isis.applib.annotation.Property;
 
-@Column(length = Name.MAX_LEN, allowsNull = "false")
 @Property(maxLength = Name.MAX_LEN)
 @Parameter(maxLength = Name.MAX_LEN)
 @ParameterLayout(named = "Name")
diff --git a/src/main/java/domainapp/modules/hello/types/Notes.java b/src/main/java/domainapp/modules/hello/types/Notes.java
index 0effb61..e68f694 100644
--- a/src/main/java/domainapp/modules/hello/types/Notes.java
+++ b/src/main/java/domainapp/modules/hello/types/Notes.java
@@ -5,18 +5,16 @@ import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
-import javax.jdo.annotations.Column;
-
 import org.apache.isis.applib.annotation.Editing;
+import org.apache.isis.applib.annotation.Optionality;
 import org.apache.isis.applib.annotation.Parameter;
 import org.apache.isis.applib.annotation.ParameterLayout;
 import org.apache.isis.applib.annotation.Property;
 import org.apache.isis.applib.annotation.PropertyLayout;
 
-@Column(length = Notes.MAX_LEN, allowsNull = "true")
-@Property(editing = Editing.ENABLED, maxLength = Notes.MAX_LEN)
+@Property(editing = Editing.ENABLED, maxLength = Notes.MAX_LEN, optionality = Optionality.OPTIONAL)
 @PropertyLayout(multiLine = 5)
-@Parameter(maxLength = Notes.MAX_LEN)
+@Parameter(maxLength = Notes.MAX_LEN, optionality = Optionality.OPTIONAL)
 @ParameterLayout(multiLine = 5)
 @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE })
 @Retention(RetentionPolicy.RUNTIME)
diff --git a/src/main/java/domainapp/webapp/AppManifest.java b/src/main/java/domainapp/webapp/AppManifest.java
index e99ca85..1baba37 100644
--- a/src/main/java/domainapp/webapp/AppManifest.java
+++ b/src/main/java/domainapp/webapp/AppManifest.java
@@ -7,7 +7,7 @@ import org.springframework.context.annotation.PropertySources;
 
 import org.apache.isis.core.config.presets.IsisPresets;
 import org.apache.isis.core.runtimeservices.IsisModuleCoreRuntimeServices;
-import org.apache.isis.persistence.jdo.datanucleus.IsisModuleJdoDatanucleus;
+import org.apache.isis.persistence.jpa.eclipselink.IsisModuleJpaEclipselink;
 import org.apache.isis.security.shiro.IsisModuleSecurityShiro;
 import org.apache.isis.testing.h2console.ui.IsisModuleTestingH2ConsoleUi;
 import org.apache.isis.viewer.restfulobjects.jaxrsresteasy4.IsisModuleViewerRestfulObjectsJaxrsResteasy4;
@@ -19,7 +19,7 @@ import domainapp.modules.hello.HelloWorldModule;
 @Import({
         IsisModuleCoreRuntimeServices.class,
         IsisModuleSecurityShiro.class,
-        IsisModuleJdoDatanucleus.class,
+        IsisModuleJpaEclipselink.class,
         IsisModuleViewerRestfulObjectsJaxrsResteasy4.class,
         IsisModuleViewerWicketViewer.class,
 
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 82ff9da..4686c08 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -61,6 +61,9 @@ isis:
             level2:
               type: none
               mode: ENABLE_SELECTIVE
+    jpa:
+      auto-create-schemas: hello
+      create-schema-sql-template: "CREATE SCHEMA IF NOT EXISTS %S"
 
 resteasy:
   jaxrs:
@@ -74,4 +77,3 @@ server:
 spring:
   banner:
     location: banner.txt
-
diff --git a/src/test/java/domainapp/modules/hello/dom/hwo/HellWorldObjects_Test.java b/src/test/java/domainapp/modules/hello/dom/hwo/HelloWorldObjects_Test.java
similarity index 85%
rename from src/test/java/domainapp/modules/hello/dom/hwo/HellWorldObjects_Test.java
rename to src/test/java/domainapp/modules/hello/dom/hwo/HelloWorldObjects_Test.java
index 2858841..8bd1a66 100644
--- a/src/test/java/domainapp/modules/hello/dom/hwo/HellWorldObjects_Test.java
+++ b/src/test/java/domainapp/modules/hello/dom/hwo/HelloWorldObjects_Test.java
@@ -18,19 +18,18 @@ import static org.mockito.ArgumentMatchers.argThat;
 import static org.mockito.Mockito.when;
 
 import org.apache.isis.applib.services.repository.RepositoryService;
-import org.apache.isis.persistence.jdo.applib.integration.JdoSupportService;
 
 @ExtendWith(MockitoExtension.class)
-class HellWorldObjects_Test {
+class HelloWorldObjects_Test {
 
     @Mock RepositoryService mockRepositoryService;
-    @Mock JdoSupportService mockJdoSupportService;
+    @Mock HelloWorldRepository mockHelloWorldRepository;
 
     HelloWorldObjects objects;
 
     @BeforeEach
     public void setUp() {
-        objects = new HelloWorldObjects(mockRepositoryService, mockJdoSupportService);
+        objects = new HelloWorldObjects(mockRepositoryService, mockHelloWorldRepository);
     }
 
     @Nested
@@ -44,7 +43,7 @@ class HellWorldObjects_Test {
 
             // expect
             when(mockRepositoryService.persist(
-                    argThat((ArgumentMatcher<HelloWorldObject>) simpleObject -> Objects.equals(simpleObject.getName(), someName)))
+                    argThat((ArgumentMatcher<HelloWorldObject>) helloWorldObject -> Objects.equals(helloWorldObject.getName(), someName)))
             ).then((Answer<HelloWorldObject>) invocation -> invocation.getArgument(0));
 
             // when
@@ -66,7 +65,7 @@ class HellWorldObjects_Test {
             final List<HelloWorldObject> all = new ArrayList<>();
 
             // expecting
-            when(mockRepositoryService.allInstances(HelloWorldObject.class))
+            when(mockHelloWorldRepository.findAll())
                 .thenReturn(all);
 
             // when