-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: Jan Vissers <[email protected]>
- Loading branch information
Showing
7 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
...ipselink.jpa.test.jse/src/org/eclipse/persistence/jpa/test/canonical/BasePersistable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.eclipse.persistence.jpa.test.canonical; | ||
|
||
public interface BasePersistable { | ||
|
||
Long getId(); | ||
|
||
String getName(); | ||
|
||
void setName(String name); | ||
} |
17 changes: 17 additions & 0 deletions
17
jpa/eclipselink.jpa.test.jse/src/org/eclipse/persistence/jpa/test/canonical/DomainClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.eclipse.persistence.jpa.test.canonical; | ||
|
||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
public class DomainClass extends DomainPersistable { | ||
|
||
// Holds additional features that one can specify on the DomainInterface | ||
// Essentially this is an enrichment of the persistable | ||
|
||
@Override | ||
public Long doSomeBusinessOperation() { | ||
|
||
return null; | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
...ipselink.jpa.test.jse/src/org/eclipse/persistence/jpa/test/canonical/DomainInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.eclipse.persistence.jpa.test.canonical; | ||
|
||
public interface DomainInterface extends BasePersistable { | ||
|
||
Long doSomeBusinessOperation(); | ||
} |
11 changes: 11 additions & 0 deletions
11
...pselink.jpa.test.jse/src/org/eclipse/persistence/jpa/test/canonical/DomainInterface_.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.eclipse.persistence.jpa.test.canonical; | ||
|
||
import javax.persistence.metamodel.SingularAttribute; | ||
import javax.persistence.metamodel.StaticMetamodel; | ||
|
||
@StaticMetamodel(DomainPersistable.class) | ||
public class DomainInterface_ { | ||
|
||
public static volatile SingularAttribute<DomainPersistable, Long> id; | ||
public static volatile SingularAttribute<DomainPersistable, String> name; | ||
} |
32 changes: 32 additions & 0 deletions
32
...selink.jpa.test.jse/src/org/eclipse/persistence/jpa/test/canonical/DomainPersistable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.eclipse.persistence.jpa.test.canonical; | ||
|
||
import javax.persistence.Id; | ||
import javax.persistence.MappedSuperclass; | ||
|
||
@MappedSuperclass | ||
public abstract class DomainPersistable implements DomainInterface { | ||
|
||
// Holds all standard fields, mapped to something that is going to be persisted in the database | ||
|
||
@Id | ||
private Long id; | ||
private String name; | ||
|
||
@Override | ||
public Long getId() { | ||
|
||
return id; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
|
||
return name; | ||
} | ||
|
||
@Override | ||
public void setName(String name) { | ||
|
||
this.name = name; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...k.jpa.test.jse/src/org/eclipse/persistence/jpa/test/canonical/TestCanonicalMetaModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package org.eclipse.persistence.jpa.test.canonical; | ||
|
||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import java.util.Arrays; | ||
import java.util.Properties; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
|
||
import org.eclipse.persistence.config.SessionCustomizer; | ||
import org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo; | ||
import org.eclipse.persistence.internal.sessions.AbstractSession; | ||
import org.eclipse.persistence.jpa.PersistenceProvider; | ||
import org.eclipse.persistence.sessions.Session; | ||
import org.junit.Test; | ||
|
||
public class TestCanonicalMetaModel { | ||
|
||
@Test | ||
public void testUseCanonicalModelFieldName() { | ||
|
||
PersistenceProvider persistenceProvider = new PersistenceProvider(); | ||
SEPersistenceUnitInfo persistenceUnitInfo = new SEPersistenceUnitInfo(); | ||
|
||
// For ASM to do its work creating metaclasses | ||
persistenceUnitInfo.setClassLoader(Thread.currentThread().getContextClassLoader()); | ||
|
||
// We only programmatically add 'leaf' domain classes to pui | ||
persistenceUnitInfo.setManagedClassNames(Arrays.asList(DomainClass.class.getName())); | ||
persistenceUnitInfo.setPersistenceUnitName("canonical-test"); | ||
persistenceUnitInfo.setPersistenceUnitRootUrl(DomainClass.class.getProtectionDomain() | ||
.getCodeSource().getLocation()); | ||
|
||
Properties properties = new Properties(); | ||
readProperty(properties, "javax.persistence.jdbc.driver"); | ||
readProperty(properties, "javax.persistence.jdbc.url"); | ||
readProperty(properties, "javax.persistence.jdbc.user"); | ||
readProperty(properties, "javax.persistence.jdbc.password"); | ||
|
||
// In our actual application we have a generic session customizer that binds a number | ||
// of delegate session customizers in a 'logical' order - for this example however we | ||
// only need to have this 'test' one - see below class | ||
properties.setProperty("eclipselink.session.customizer", CanonicalMetaModelCustomizer.class.getName()); | ||
|
||
try (AutoCloseableEntityManagerFactory acemf = new AutoCloseableEntityManagerFactory(persistenceProvider, | ||
persistenceUnitInfo, properties)) { | ||
assertThat("The \"name\" field is properly initialized", DomainInterface_.name, notNullValue()); | ||
} | ||
|
||
} | ||
|
||
private void readProperty(Properties properties, String key) { | ||
|
||
properties.setProperty(key, System.getProperty(key)); | ||
} | ||
|
||
/** | ||
* What it is doing is that it will be providing the link between an (impl) 'hidden' persistable and an | ||
* api (exposed) canonical metamodel class. | ||
*/ | ||
public static class CanonicalMetaModelCustomizer implements SessionCustomizer { | ||
|
||
@Override | ||
public void customize(Session session) throws Exception { | ||
|
||
AbstractSession.class.cast(session).addStaticMetamodelClass( | ||
DomainPersistable.class.getName(), DomainInterface_.class.getName()); | ||
} | ||
} | ||
|
||
/** | ||
* Provides some small cleanup test helper facility. | ||
*/ | ||
static class AutoCloseableEntityManagerFactory implements AutoCloseable { | ||
|
||
private final EntityManagerFactory entityManagerFactory; | ||
private final EntityManager firstEntityManager; | ||
|
||
AutoCloseableEntityManagerFactory(PersistenceProvider persistenceProvider, | ||
SEPersistenceUnitInfo persistenceUnitInfo, Properties properties) { | ||
|
||
entityManagerFactory = persistenceProvider.createContainerEntityManagerFactory(persistenceUnitInfo, properties); | ||
firstEntityManager = entityManagerFactory.createEntityManager(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
|
||
if (firstEntityManager != null) { | ||
firstEntityManager.close(); | ||
} | ||
|
||
if (entityManagerFactory != null) { | ||
entityManagerFactory.close(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters