diff --git a/cnf/central.maven b/cnf/central.maven index 2ccb0c2..28361b9 100644 --- a/cnf/central.maven +++ b/cnf/central.maven @@ -213,4 +213,14 @@ jakarta.enterprise:jakarta.enterprise.cdi-api:2.0.2 jakarta.interceptor:jakarta.interceptor-api:1.2.5 org.apache.felix:org.apache.felix.http.whiteboard:2.3.2 - +# Derby +org.apache.derby:derby:10.14.2.0 +org.ops4j.pax.jdbc:pax-jdbc-derby:1.5.5 + +#Eclipselink +org.eclipse.persistence:org.eclipse.persistence.moxy:4.0.0 +org.eclipse.persistence:org.eclipse.persistence.jpa:4.0.0 +org.eclipse.persistence:org.eclipse.persistence.jpa.jpql:4.0.0 +org.eclipse.persistence:org.eclipse.persistence.core:4.0.0 +org.eclipse.persistence:org.eclipse.persistence.asm:9.4.0 +jakarta.persistence:jakarta.persistence-api:3.1.0 diff --git a/cnf/local/index.xml b/cnf/local/index.xml index cce4a67..bb822ec 100644 --- a/cnf/local/index.xml +++ b/cnf/local/index.xml @@ -1,5 +1,5 @@ - + @@ -58,388 +58,481 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + + - + - + - + - + - + - - + + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - + - + - + - - - + + - - - - - - - - - - - - - + + - + - - + + + + + - + - + - + - - - + + - - - - - - - - + + - - - - + + + - + - - + + - - + + + + + - + - - - - + + + - + + + + + + + + - - + + - - + + - + - - + + - - + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + - - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - + + + + + + - - - - - - - + + + + + + - - + - + - + - + - + - + - - + + - - - - - - - - - - + + @@ -449,82 +542,129 @@ - + - + + + + + + + - + - + - + - - - - - - - - - - - - - - - - + + - - - + + + - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + - + - + + + + + - + - - + + + + + + @@ -537,41 +677,49 @@ + + + + - - + + - - + + - - + + + + + + - - + + - - + + - - + + - - + + @@ -579,39 +727,53 @@ - + - + + + + + + + + + - + - + - + - - + + - - + + + - + + - - + + + + + + - + @@ -619,7 +781,15 @@ - + + + + + + + + + @@ -650,10 +820,6 @@ - - - - @@ -663,12 +829,12 @@ - - + + - - + + @@ -680,8 +846,18 @@ + + + + - + + + + + + + @@ -693,408 +869,333 @@ - - - - - + + + - - - - - - - - - - - - - - - - + + - - + + - + - - + + + + + + + + + + - - + + - - + + - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + - - - - - - - - - - + + + - + - - + + - - + + - + - - + + - - + + - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + - + - - - - - - - - - + + - - - + + + + + - - - - - - - - - - - - - - - - - - - - - - - + - - + + + - - + + + + + + + + + + + + + - + - - + + - - - - - - - - + - - + + - + - + - + - + - + - - + + - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + - - + + - - + + - - + + + + + + @@ -1103,525 +1204,781 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - - - + - + - + - - - - - - - - - + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + - - + + + - - - + + + - - - + + + + + + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - + - - + + - - - + + + - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + - + - + - + - - - - - + + + + + - - - - - - + + + + + - - + + - - - - - - + + - + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + + - + - + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + - - + - + - + - - - - - - - - - - - + + - - - + + + - - + - - + + + - - + + + - - + + + - - + + + - - + + + - - + + + - - + + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1630,31 +1987,16 @@ - - - - - - - - - - - - - - - + + + - - - - - + + + - - - + + @@ -1686,83 +2028,4 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/cnf/local/org.eclipse.gemini.jpa/org.eclipse.gemini.jpa-1.0.0.jar b/cnf/local/org.eclipse.gemini.jpa/org.eclipse.gemini.jpa-1.0.0.jar new file mode 100644 index 0000000..5e9d6bf Binary files /dev/null and b/cnf/local/org.eclipse.gemini.jpa/org.eclipse.gemini.jpa-1.0.0.jar differ diff --git a/cnf/local/org.gecko.persistence.derby/org.gecko.persistence.derby-1.0.0.jar b/cnf/local/org.gecko.persistence.derby/org.gecko.persistence.derby-1.0.0.jar new file mode 100644 index 0000000..c4589eb Binary files /dev/null and b/cnf/local/org.gecko.persistence.derby/org.gecko.persistence.derby-1.0.0.jar differ diff --git a/cnf/local/org.osgi.service.jpa/org.osgi.service.jpa-3.1.1.jar b/cnf/local/org.osgi.service.jpa/org.osgi.service.jpa-3.1.1.jar new file mode 100644 index 0000000..516b4d8 Binary files /dev/null and b/cnf/local/org.osgi.service.jpa/org.osgi.service.jpa-3.1.1.jar differ diff --git a/org.gecko.playground.jpa/.classpath b/org.gecko.playground.jpa/.classpath new file mode 100644 index 0000000..a3296b8 --- /dev/null +++ b/org.gecko.playground.jpa/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/org.gecko.playground.jpa/.gitignore b/org.gecko.playground.jpa/.gitignore new file mode 100644 index 0000000..d37c7e7 --- /dev/null +++ b/org.gecko.playground.jpa/.gitignore @@ -0,0 +1,2 @@ +/derby.log +/Database/ diff --git a/org.gecko.playground.jpa/.project b/org.gecko.playground.jpa/.project new file mode 100644 index 0000000..69e93ae --- /dev/null +++ b/org.gecko.playground.jpa/.project @@ -0,0 +1,23 @@ + + + org.gecko.playground.jpa + + + + + + org.eclipse.jdt.core.javabuilder + + + + + bndtools.core.bndbuilder + + + + + + org.eclipse.jdt.core.javanature + bndtools.core.bndnature + + diff --git a/org.gecko.playground.jpa/.settings/org.eclipse.core.resources.prefs b/org.gecko.playground.jpa/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..9c75668 --- /dev/null +++ b/org.gecko.playground.jpa/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 +encoding/bnd.bnd=UTF-8 diff --git a/org.gecko.playground.jpa/.settings/org.eclipse.jdt.core.prefs b/org.gecko.playground.jpa/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..d4540a5 --- /dev/null +++ b/org.gecko.playground.jpa/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,10 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/org.gecko.playground.jpa/bnd.bnd b/org.gecko.playground.jpa/bnd.bnd new file mode 100644 index 0000000..95e8b1b --- /dev/null +++ b/org.gecko.playground.jpa/bnd.bnd @@ -0,0 +1,24 @@ +-buildpath: \ + jakarta.persistence-api;version=latest,\ + org.osgi.framework;version=latest,\ + org.osgi.util.tracker;version=latest,\ + org.osgi.service.jpa;version=latest,\ + org.eclipse.persistence.jpa;version=latest,\ + org.eclipse.persistence.core;version=latest,\ + org.eclipse.emf.ecore;version=latest + +-includeresource: \ + META-INF/persistence.xml=persistence.xml +Meta-Persistence: META-INF/persistence.xml + +-privatepackage: \ + org.gecko.playground.jpa,\ + org.gecko.playground.jpa.classloader + +Require-Capability: \ + osgi.extender;filter:='(osgi.extender=osgi.configurator)' + +Export-Package: org.gecko.playground.jpa.entities +Import-Package: \ + org.eclipse.persistence.internal.dynamic;version='[4.0.0,5.0.0)',\ + * \ No newline at end of file diff --git a/org.gecko.playground.jpa/launch.bndrun b/org.gecko.playground.jpa/launch.bndrun new file mode 100644 index 0000000..fd61a93 --- /dev/null +++ b/org.gecko.playground.jpa/launch.bndrun @@ -0,0 +1,37 @@ +-runrequires: \ + bnd.identity;id='org.apache.felix.gogo.command',\ + bnd.identity;id='org.apache.felix.gogo.shell',\ + bnd.identity;id='org.eclipse.gemini.jpa',\ + bnd.identity;id='org.eclipse.persistence.jpa.jpql',\ + bnd.identity;id='org.gecko.persistence.derby',\ + bnd.identity;id='org.apache.aries.spifly.dynamic.framework.extension',\ + bnd.identity;id='org.gecko.playground.jpa',\ + bnd.identity;id=derby +-runfw: org.apache.felix.framework;version='[7.0.5,7.0.5]' +-runee: JavaSE-17 +-runbundles: \ + org.apache.felix.gogo.command;version='[1.1.2,1.1.3)',\ + org.apache.felix.gogo.shell;version='[1.1.4,1.1.5)',\ + jakarta.persistence-api;version='[3.1.0,3.1.1)',\ + org.eclipse.persistence.asm;version='[9.4.0,9.4.1)',\ + org.eclipse.persistence.core;version='[4.0.0,4.0.1)',\ + org.eclipse.persistence.jpa;version='[4.0.0,4.0.1)',\ + org.eclipse.persistence.jpa.jpql;version='[4.0.0,4.0.1)',\ + org.osgi.service.jdbc;version='[1.1.0,1.1.1)',\ + org.osgi.service.jpa;version='[3.1.1,3.1.2)',\ + derby;version='[10.14.2000000,10.14.2000001)',\ + org.apache.aries.spifly.dynamic.framework.extension;version='[1.3.6,1.3.7)',\ + org.apache.felix.cm.json;version='[2.0.0,2.0.1)',\ + org.apache.felix.configurator;version='[1.0.18,1.0.19)',\ + org.apache.felix.gogo.runtime;version='[1.1.6,1.1.7)',\ + org.apache.felix.scr;version='[2.2.6,2.2.7)',\ + org.eclipse.gemini.jpa;version='[1.0.0,1.0.1)',\ + org.eclipse.parsson.jakarta.json;version='[1.1.2,1.1.3)',\ + org.gecko.persistence.derby;version='[1.0.0,1.0.1)',\ + org.gecko.playground.jpa;version=snapshot,\ + org.osgi.service.component;version='[1.5.1,1.5.2)',\ + org.osgi.util.converter;version='[1.0.9,1.0.10)',\ + org.osgi.util.function;version='[1.2.0,1.2.1)',\ + org.osgi.util.promise;version='[1.3.0,1.3.1)',\ + org.apache.felix.configadmin;version='[1.9.26,1.9.27)' +-runblacklist: bnd.identity;id='org.osgi.service.cm' \ No newline at end of file diff --git a/org.gecko.playground.jpa/persistence.xml b/org.gecko.playground.jpa/persistence.xml new file mode 100644 index 0000000..9be940c --- /dev/null +++ b/org.gecko.playground.jpa/persistence.xml @@ -0,0 +1,36 @@ + + + + + org.eclipse.persistence.jpa.PersistenceProvider + + org.gecko.playground.jpa.entities.Account + org.gecko.playground.jpa.entities.Customer + org.gecko.playground.jpa.entities.Transaction + org.gecko.playground.jpa.entities.TxOperation + + true + + + + + + + + + + + + + + + + + + + + + diff --git a/org.gecko.playground.jpa/src/org/gecko/playground/jpa/AccountClient.java b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/AccountClient.java new file mode 100644 index 0000000..bc57af7 --- /dev/null +++ b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/AccountClient.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at + * http://www.opensource.org/licenses/apache2.0.php. + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * mkeith - Gemini JPA sample + ******************************************************************************/ +package org.gecko.playground.jpa; + +import java.util.Date; +import java.util.List; + +import org.eclipse.persistence.dynamic.DynamicClassLoader; +import org.eclipse.persistence.dynamic.DynamicEntity; +import org.eclipse.persistence.dynamic.DynamicTypeBuilder; +import org.eclipse.persistence.jpa.dynamic.JPADynamicHelper; +import org.eclipse.persistence.queries.ReadAllQuery; +import org.gecko.playground.jpa.classloader.OSGiJPADynamicHelper; +import org.gecko.playground.jpa.entities.Account; +import org.gecko.playground.jpa.entities.Customer; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.TypedQuery; + +/** + * Gemini JPA sample client class + * + * @author mkeith + */ +public class AccountClient { + + public void run(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + + Customer c = new Customer("Chan", "Jackie", "1034 KingFu Lane, Los Angeles, CA"); + em.persist(c); + Account a = new Account(c); + a.setBalance(100.0); + em.persist(a); + + em.getTransaction().commit(); + + TypedQuery q = em.createQuery("SELECT a FROM Account a", Account.class); + List results = q.getResultList(); + System.out.println("\n*** Account Report ***"); + for (Account acct : results) { + System.out.println("Account: " + acct); + } + em.close(); + } + @SuppressWarnings("unchecked") + public void runDynamic(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + JPADynamicHelper helper = new OSGiJPADynamicHelper(em); + DynamicClassLoader dcl = helper.getDynamicClassLoader(); + Class employeeClass= dcl.createDynamicClass("scott.Emp"); + Class deptClass= dcl.createDynamicClass("scott.Dept"); + DynamicTypeBuilder employeeBuilder = new DynamicTypeBuilder(employeeClass, + null /*no parent type*/, "EMP"); + DynamicTypeBuilder deptBuilder = new DynamicTypeBuilder(deptClass, + null /*no parent type*/, "DEPT"); + employeeBuilder.setPrimaryKeyFields("EMPNO"); + employeeBuilder.addDirectMapping("employeeNumber", int.class, "EMPNO"); + employeeBuilder.addDirectMapping("name", String.class, "ENAME"); + employeeBuilder.addDirectMapping("job", String.class, "JOB"); + employeeBuilder.addDirectMapping("hiredOn", Date.class, "HIREDATE"); + employeeBuilder.addDirectMapping("salaryPerWeek", int.class, "SAL"); + employeeBuilder.addDirectMapping("commision", int.class, "COMM"); + employeeBuilder.addOneToOneMapping("manager", employeeBuilder.getType(), "MGR"); + deptBuilder.setPrimaryKeyFields("DEPTNO"); + deptBuilder.addDirectMapping("deptNumber", int.class, "DEPTNO"); + deptBuilder.addDirectMapping("name", String.class, "DNAME"); + deptBuilder.addDirectMapping("location", String.class, "LOC"); + employeeBuilder.addOneToOneMapping("department", deptBuilder.getType(), "DEPTNO"); + + + helper.addTypes(true /*create tables*/, true /*create constraints*/, + employeeBuilder.getType(), deptBuilder.getType()); + + DynamicEntity deptEntity = deptBuilder.getType().newDynamicEntity(); + deptEntity.set("deptNumber", 12); + deptEntity.set("name", "digitalization"); + deptEntity.set("location", "Jena DIMC Office"); + + DynamicEntity employeeEntity = employeeBuilder.getType().newDynamicEntity(); + employeeEntity.set("employeeNumber", 1); + employeeEntity.set("name", "Mark"); + employeeEntity.set("job", "CTO"); +// employeeEntity.set("hiredOn", new Date()); + employeeEntity.set("salaryPerWeek", 1750); + employeeEntity.set("commision", 42); + employeeEntity.set("department", deptEntity); + + ReadAllQuery query = helper.newReadAllQuery("Emp"); + List employees = (List)helper.getSession().executeQuery(query); + if (employees.isEmpty()) { + em.getTransaction().begin(); + em.persist(deptEntity); + em.persist(employeeEntity); + em.getTransaction().commit(); + } + + //query.addAscendingOrdering("employeeNumber"); + query.addAscendingOrdering("name"); + System.out.println("Employees-Size " + employees.size()); + employees.forEach(e->{ + System.out.println("Employee " + e.get("name")); + DynamicEntity dept = e.get("department"); + System.out.println("Employee Department " + dept.get("name")); + + }); + em.close(); + } +} \ No newline at end of file diff --git a/org.gecko.playground.jpa/src/org/gecko/playground/jpa/TestComponent.java b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/TestComponent.java new file mode 100644 index 0000000..f6fdc3c --- /dev/null +++ b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/TestComponent.java @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2012 - 2023 Data In Motion and others. + * All rights reserved. + * + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Data In Motion - initial API and implementation + */ +package org.gecko.playground.jpa; + +import org.osgi.framework.BundleContext; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.jpa.EntityManagerFactoryBuilder; + +import jakarta.persistence.EntityManagerFactory; + +/** + * https://wiki.eclipse.org/EclipseLink/Examples/JPA/Dynamic + * https://wiki.eclipse.org/EclipseLink/Examples/JPA/Dynamic/CustomizeAttributes + * https://wiki.eclipse.org/EclipseLink/Examples/Foundation/DynamicPersistence + * + * @author mark + * @since 13.01.2023 + */ +@Component +public class TestComponent { + + @Reference(target = "(" + EntityManagerFactoryBuilder.JPA_UNIT_NAME + "=Accounts)") + private EntityManagerFactory emf; + + + @Activate + public void activate(BundleContext ctx) { + System.out.println("Got basic model"); + System.out.println("Gemini JPA Account Sample started"); + new AccountClient().run(emf); + } + +} diff --git a/org.gecko.playground.jpa/src/org/gecko/playground/jpa/classloader/OSGiJPADynamicHelper.java b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/classloader/OSGiJPADynamicHelper.java new file mode 100644 index 0000000..dff6759 --- /dev/null +++ b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/classloader/OSGiJPADynamicHelper.java @@ -0,0 +1,72 @@ +/** + * Copyright (c) 2012 - 2023 Data In Motion and others. + * All rights reserved. + * + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Data In Motion - initial API and implementation + */ +package org.gecko.playground.jpa.classloader; + +import org.eclipse.persistence.dynamic.DynamicClassLoader; +import org.eclipse.persistence.internal.helper.ConversionManager; +import org.eclipse.persistence.jpa.dynamic.JPADynamicHelper; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; + +/** + * + * @author mark + * @since 13.01.2023 + */ +public class OSGiJPADynamicHelper extends JPADynamicHelper { + + /** + * Creates a new instance. + * @param emf + */ + public OSGiJPADynamicHelper(EntityManagerFactory emf) { + super(emf); + } + + /** + * Creates a new instance. + * @param em + */ + public OSGiJPADynamicHelper(EntityManager em) { + super(em); + } + + /* + * (non-Javadoc) + * @see org.eclipse.persistence.dynamic.DynamicHelper#getDynamicClassLoader() + */ + @Override + public DynamicClassLoader getDynamicClassLoader() { + ConversionManager cm = null; + + if (session == null) { + cm = ConversionManager.getDefaultManager(); + } else { + cm = session.getPlatform().getConversionManager(); + } + + if (cm.getLoader() instanceof DynamicClassLoader) { + return (DynamicClassLoader) cm.getLoader(); + } + + DynamicClassLoader dcl = new DynamicClassLoader(getClass().getClassLoader()); + cm.setLoader(dcl); + + if (session == null) { + ConversionManager.setDefaultLoader(dcl); + } + + return dcl; + } + +} diff --git a/org.gecko.playground.jpa/src/org/gecko/playground/jpa/classloader/package-info.java b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/classloader/package-info.java new file mode 100644 index 0000000..33badbe --- /dev/null +++ b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/classloader/package-info.java @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2012 - 2023 Data In Motion and others. + * All rights reserved. + * + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Data In Motion - initial API and implementation + */ +/** + * + * @author mark + * @since 13.01.2023 + */ +package org.gecko.playground.jpa.classloader; \ No newline at end of file diff --git a/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/Account.java b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/Account.java new file mode 100644 index 0000000..e47dc7a --- /dev/null +++ b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/Account.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at + * http://www.opensource.org/licenses/apache2.0.php. + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * mkeith - Gemini JPA Sample + ******************************************************************************/ +package org.gecko.playground.jpa.entities; + +import jakarta.persistence.*; + +import java.util.Date; +import java.util.List; +import java.util.ArrayList; + +/** + * Sample JPA model class + * + * @author mkeith + */ +@Entity +public class Account { + + @Id @GeneratedValue + int id; + + double balance; + + @OneToOne(mappedBy="account") + Customer customer; + + @OneToMany(mappedBy="account") + @OrderBy("txTime") + List txns; + + @Temporal(TemporalType.DATE) + public Date dateCreated; + + /* Constructors */ + public Account() { + dateCreated = new Date(System.currentTimeMillis()); + } + public Account(Customer customer) { + this(); + this.balance = 0; + this.customer = customer; + customer.setAccount(this); + this.txns = new ArrayList(); + } + + /* Getters and setters */ + public int getId() { return id; } + public void setId(int id) { this.id = id; } + + public double getBalance() { return balance; } + public void setBalance(double balance) { this.balance = balance; } + + public Customer getCustomer() { return customer; } + public void setCustomer(Customer customer) { this.customer = customer; } + + public List getTxns() { return txns; } + public void setTxns(List txns) { this.txns = txns; } + + public String toString() { + return "Account(" + id + ", " + ((customer!=null)?customer.getLastName():"null") + ", Balance: $" + balance + ")"; + } +} diff --git a/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/Customer.java b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/Customer.java new file mode 100644 index 0000000..333a1ba --- /dev/null +++ b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/Customer.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at + * http://www.opensource.org/licenses/apache2.0.php. + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * mkeith - Gemini JPA Sample + ******************************************************************************/ +package org.gecko.playground.jpa.entities; + +import jakarta.persistence.*; + +/** + * Gemini JPA Sample class + * + * @author mkeith + */ +@Entity +public class Customer { + @Id @GeneratedValue + int id; + + @Column(name="LNAME") + String lastName; + + @Column(name="FNAME") + String firstName; + + @Column(name="ADDR") + String address; + + @OneToOne + Account account; + + /* Constructors */ + public Customer() { super(); } + public Customer(String lastName, String firstName, String address) { + super(); + this.lastName = lastName; + this.firstName = firstName; + this.address = address; + } + + /* Getters and setters */ + public int getId() { return id; } + + public String getLastName() { return lastName; } + public void setLastName(String lastName) { this.lastName = lastName; } + + public String getFirstName() { return firstName; } + public void setFirstName(String firstName) { this.firstName = firstName; } + + public String getAddress() { return address; } + public void setAddress(String address) { this.address = address; } + + public Account getAccount() { return account; } + public void setAccount(Account account) { this.account = account; } + + public String toString() { + return "Customer(" + firstName + " " + lastName + ", " + address + ")"; + } +} diff --git a/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/Transaction.java b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/Transaction.java new file mode 100644 index 0000000..110abd3 --- /dev/null +++ b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/Transaction.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at + * http://www.opensource.org/licenses/apache2.0.php. + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * mkeith - Gemini JPA Sample + ******************************************************************************/ +package org.gecko.playground.jpa.entities; + +import jakarta.persistence.*; +import java.util.Date; +import java.util.Calendar; + + +/** + * Gemini JPA Sample class + * + * @author mkeith + */ +@Entity +@Table(name="ACCT_TXN") +@NamedQuery(name="Transaction.findAllSince", + query="SELECT t FROM Transaction t WHERE t.account = :account AND t.txTime >= :dateArg") +public class Transaction { + + @Id @GeneratedValue + int id; + + @ManyToOne + Account account; + + @Column(name="OP") + TxOperation operation; + + double amount; + + @Temporal(TemporalType.TIME) + Date txTime; + + /* Constructors */ + public Transaction() { super(); } + public Transaction(Account account, TxOperation operation, double amount) { + super(); + this.account = account; + account.getTxns().add(this); + this.operation = operation; + this.amount = amount; + this.txTime = Calendar.getInstance().getTime(); + } + + /* Getters and setters */ + public int getId() { return id; } + + public Account getAccount() { return account; } + public void setAccount(Account account) { this.account = account; } + + public TxOperation getOperation() { return operation; } + public void setOperation(TxOperation operation) { this.operation = operation; } + + public Date getTxTime() { return txTime; } + public void setTxTime(Date txTime) { this.txTime = txTime; } + + public double getAmount() { return amount; } + public void setAmount(double amount) { this.amount = amount; } + + public String toString() { + return "("+ txTime + " - " + "Acct#: " + account.getId() + " " + operation.toString() + ": " + amount + ")"; + } +} diff --git a/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/TxOperation.java b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/TxOperation.java new file mode 100644 index 0000000..c26b0c8 --- /dev/null +++ b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/TxOperation.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at + * http://www.opensource.org/licenses/apache2.0.php. + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * mkeith - Gemini JPA Sample + ******************************************************************************/ +package org.gecko.playground.jpa.entities; + +/** + * Gemini JPA Sample class + * + * @author mkeith + */ +public enum TxOperation { + + DEPOSIT, + WITHDRAW +} diff --git a/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/package-info.java b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/package-info.java new file mode 100644 index 0000000..2b4623a --- /dev/null +++ b/org.gecko.playground.jpa/src/org/gecko/playground/jpa/entities/package-info.java @@ -0,0 +1,2 @@ +@org.osgi.annotation.versioning.Version("1.0.0") +package org.gecko.playground.jpa.entities;