Skip to content

Commit

Permalink
Merge pull request wildfly#18411 from scottmarlow/WFLY-19973_Persiste…
Browse files Browse the repository at this point in the history
…nceContext_properties

[WFLY-19973] @PersistenceContext properties attribute should be processed
  • Loading branch information
bstansberry authored Nov 26, 2024
2 parents dfc2e0f + 2a0a771 commit 2179dfc
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ protected boolean skipQueryDetach() {
return skipQueryDetach.booleanValue();
}

@Override
public void setProperty(String propertyName, Object value) {
properties.put(propertyName, value);
super.setProperty(propertyName, value);
}


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,13 @@ private InjectionSource getBindingSource(final DeploymentUnit deploymentUnit, fi
(stType == null || SynchronizationType.SYNCHRONIZED.name().equals(stType.asString()))?
SynchronizationType.SYNCHRONIZED: SynchronizationType.UNSYNCHRONIZED;

Map<String, String> properties;
final Map<String, String> properties = new HashMap<>();
AnnotationValue value = annotation.value("properties");
AnnotationInstance[] props = value != null ? value.asNestedArray() : null;
if (props != null) {
properties = new HashMap<>();
for (int source = 0; source < props.length; source++) {
properties.put(props[source].value("name").asString(), props[source].value("value").asString());
}
} else {
properties = null;
}
// get deployment settings from top level du (jboss-all.xml is only parsed at the top level).
final JPADeploymentSettings jpaDeploymentSettings = DeploymentUtils.getTopDeploymentUnit(deploymentUnit).getAttachment(JpaAttachments.DEPLOYMENT_SETTINGS_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -718,7 +717,7 @@ public void inject(final PersistenceUnitServiceImpl value) throws
binderService.getManagedObjectInjector().inject(new ValueManagedReferenceFactory(
new TransactionScopedEntityManager(
pu.getScopedPersistenceUnitName(),
Collections.emptyMap(),
new HashMap(), // WFLY-19973: pass empty HashMap that can be modified by application code.
value.getEntityManagerFactory(),
SynchronizationType.SYNCHRONIZED, transactionSynchronizationRegistry, transactionManager)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,38 @@

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.PersistenceProperty;
import jakarta.persistence.Query;


public class CdiJpaInjectingBean {
@PersistenceContext(unitName = "cdiPu")

@PersistenceContext(unitName = "cdiPu", properties = { @PersistenceProperty(name = "CdiJpaInjectingBean", value = "true")})
EntityManager em;

@PersistenceContext(unitName = "cdiPu")
EntityManager emEmptyProperties;

public Employee queryEmployeeName(int id) {
Query q = em.createQuery("SELECT e FROM Employee e where e.id=:employeeId");
q.setParameter("employeeId", id);
return (Employee) q.getSingleResult();
}

public String getInitialPropertyValue() {
return (String) em.getProperties().get("CdiJpaInjectingBean");
}

public void setAdditionalPropertyValue(String value) {
em.setProperty("CdiJpaInjectingBean.additional", value);
}

public String getAdditionalPropertyValue() {
return (String) em.getProperties().get("CdiJpaInjectingBean.additional");
}

public String addPropertyToEmptyPropertyMap(String value) {
emEmptyProperties.setProperty("CdiJpaInjectingBean.addToEmptyPropertyMap", value);
return (String) emEmptyProperties.getProperties().get("CdiJpaInjectingBean.addToEmptyPropertyMap");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,25 @@ public void testOrmXmlDefinedEmployeeEntity() throws Exception {
}
Assert.fail("NoResultException should occur but didn't!");
}

// [WFLY-19973] (Weld injected) @PersistenceContext (initial) properties attribute should be processed
@Test
public void testInitialPersistenceContextPropertiesAreSet() throws Exception {
Assert.assertEquals("true",bean.getInitialPropertyValue());
}

// [WFLY-19973] (Weld injected) @PersistenceContext (later set) properties attribute should be available
@Test
public void testEntityManagerPropertiesAreSaved() throws Exception {
bean.setAdditionalPropertyValue("WeldJpaInjectionScopeTestCase.testproperty");
Assert.assertEquals("WeldJpaInjectionScopeTestCase.testproperty",bean.getAdditionalPropertyValue());
}

// [WFLY-19973] (Weld injected) @PersistenceContext (later set) ensure that property can be added to empty property map
@Test
public void testEntityManagerPropertiesEmptyCase() throws Exception {
Assert.assertEquals("AddedToEmptyHashMap", bean.addPropertyToEmptyPropertyMap("AddedToEmptyHashMap"));
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

import jakarta.enterprise.inject.spi.InjectionPoint;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.PersistenceProperty;
import jakarta.persistence.PersistenceUnit;
import jakarta.transaction.TransactionManager;
import jakarta.transaction.TransactionSynchronizationRegistry;
Expand Down Expand Up @@ -71,7 +73,7 @@ public ResourceReferenceFactory<EntityManager> registerPersistenceContextInjecti
public EntityManager call() throws Exception {
return new TransactionScopedEntityManager(
scopedPuName,
new HashMap<>(),
getProperties(context),
persistenceUnitService.getEntityManagerFactory(),
context.synchronization(),
deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_SYNCHRONIZATION_REGISTRY),
Expand Down Expand Up @@ -122,6 +124,15 @@ private String getScopedPUName(final DeploymentUnit deploymentUnit, final String
return scopedPu.getScopedPersistenceUnitName();
}

private static Map getProperties(PersistenceContext context) {
HashMap map = new HashMap();
for (PersistenceProperty property : context.properties()) {
map.put(property.name(), property.value());
}
return map;
}


private static class EntityManagerResourceReferenceFactory implements ResourceReferenceFactory<EntityManager> {
private final String scopedPuName;
private final EntityManagerFactory entityManagerFactory;
Expand All @@ -139,9 +150,10 @@ public EntityManagerResourceReferenceFactory(String scopedPuName, EntityManagerF

@Override
public ResourceReference<EntityManager> createResource() {
final TransactionScopedEntityManager result = new TransactionScopedEntityManager(scopedPuName, new HashMap<>(), entityManagerFactory, context.synchronization(), transactionSynchronizationRegistry, transactionManager);
final TransactionScopedEntityManager result = new TransactionScopedEntityManager(scopedPuName, getProperties(context), entityManagerFactory, context.synchronization(), transactionSynchronizationRegistry, transactionManager);
return new SimpleResourceReference<EntityManager>(result);
}

}

private static class LazyFactory<T> implements ResourceReferenceFactory<T> {
Expand Down

0 comments on commit 2179dfc

Please sign in to comment.