@Repository @Transactional(readOnly = true) class AccountServiceImpl implements AccountService { @PersistenceContext private EntityManager em;
public PersistenceElement(Member member, AnnotatedElement ae, @Nullable PropertyDescriptor pd) { super(member, pd); PersistenceContext pc = ae.getAnnotation(PersistenceContext.class); PersistenceUnit pu = ae.getAnnotation(PersistenceUnit.class); Class<?> resourceType = EntityManager.class; if (pc != null) { if (pu != null) { throw new IllegalStateException("Member may only be annotated with either " + "@PersistenceContext or @PersistenceUnit, not both: " + member); } Properties properties = null; PersistenceProperty[] pps = pc.properties(); if (!ObjectUtils.isEmpty(pps)) { properties = new Properties(); for (PersistenceProperty pp : pps) { properties.setProperty(pp.name(), pp.value()); } } this.unitName = pc.unitName(); this.type = pc.type(); this.synchronizedWithTransaction = SynchronizationType.SYNCHRONIZED.equals(pc.synchronization()); this.properties = properties; } else { resourceType = EntityManagerFactory.class; this.unitName = pu.unitName(); } checkResourceType(resourceType); }
@ContextConfiguration(classes = GenericJpaRepositoryIntegrationTest.TestContext.class) @TestPropertySource("classpath:hsqldb.database.properties") @Transactional public class GenericJpaRepositoryIntegrationTest implements EventMessageHandler { @Autowired @Qualifier("simpleRepository") private GenericJpaRepository<JpaAggregate> repository; @Autowired private EventBus eventBus; @PersistenceContext private EntityManager entityManager; private SubscribingEventProcessor eventProcessor;
@Transactional(readOnly = true) @Repository public class IndexRepositoryImpl implements IndexRepository{ @PersistenceContext private EntityManager em; @Autowired private IndexRepositoryJpa repo;
@Stateless @Named public class TemplateServiceBean { @EJB IndexServiceBean indexService; @PersistenceContext(unitName = "VDCNet-ejbPU") private EntityManager em;
@Repository("blPhoneDao") public class PhoneDaoImpl implements PhoneDao { @PersistenceContext(unitName="blPU") protected EntityManager em; @Resource(name="blEntityConfiguration") protected EntityConfiguration entityConfiguration; public Phone save(Phone phone) { return em.merge(phone); } public Phone readPhoneById(Long phoneId) { return (Phone) em.find(PhoneImpl.class, phoneId); } public Phone create() { return (Phone) entityConfiguration.createEntityInstance(Phone.class.getName()); } }
/** * * @author skraffmiller */ @Stateless @Named public class ControlledVocabularyValueServiceBean implements java.io.Serializable { @PersistenceContext(unitName = "VDCNet-ejbPU") private EntityManager em; public List<ControlledVocabularyValue> findByDatasetFieldTypeId(Long dsftId) { String queryString = "select o from ControlledVocabularyValue as o where o.datasetFieldType.id = " + dsftId + " "; TypedQuery<ControlledVocabularyValue> query = em.createQuery(queryString, ControlledVocabularyValue.class); return query.getResultList(); } }
@Stateless public class MeasurementProblemManagerBean implements MeasurementProblemManagerLocal, MeasurementProblemManagerRemote { @SuppressWarnings("unused") private final Log log = LogFactory.getLog(MeasurementProblemManagerBean.class); @PersistenceContext(unitName = RHQConstants.PERSISTENCE_UNIT_NAME) private EntityManager entityManager; @EJB private AuthorizationManagerLocal authorizationManager;
/** * @author Arun Gupta */ @Path("employee") @Stateless public class EmployeeResource { @PersistenceContext EntityManager em; @GET @Produces("application/xml") public Employee[] get() { return em.createNamedQuery("Employee.findAll", Employee.class).getResultList().toArray(new Employee[0]); } }
/** * JPA Address repository implementation * * @author Michael Joseph Walsh * */ @Repository public class JpaAddressRepository implements AddressRepository { @PersistenceContext(unitName="defaultPersistenceUnit") private EntityManager manager; @Override @Transactional(value="defaultTransactionManager") public Address getById(Long id) { return manager.find(Address.class, id); } }
public static class DefaultVendorSpecificPrivatePersistenceContextField { @PersistenceContext @SuppressWarnings("deprecation") private org.hibernate.ejb.HibernateEntityManager em; }
@EJB protected TestEntityDao dao; @PersistenceContext protected EntityManager em;
private String getLookupName( Class annotatedClass, AnnotatedField annotatedField, List<InjectionCapable> injectionResources ) { String lookupName = null; if ( annotatedField.isAnnotationPresent( Resource.class ) ) { Resource resource = annotatedField.getAnnotation( Resource.class ); lookupName = getJndiName( resource.lookup(), resource.mappedName(), resource.name() ); } else if ( annotatedField.isAnnotationPresent( EJB.class ) ) { EJB ejb = annotatedField.getAnnotation( EJB.class ); lookupName = getJndiName(ejb.lookup(), ejb.mappedName(), ejb.name()); } else if ( annotatedField.isAnnotationPresent( WebServiceRef.class ) ) { WebServiceRef webServiceRef = annotatedField.getAnnotation( WebServiceRef.class ); lookupName = getJndiName(webServiceRef.lookup(), webServiceRef.mappedName(), webServiceRef.name()); } else if ( annotatedField.isAnnotationPresent( PersistenceUnit.class ) ) { PersistenceUnit persistenceUnit = annotatedField.getAnnotation( PersistenceUnit.class ); lookupName = getJndiName( persistenceUnit.unitName(), null, persistenceUnit.name() ); } else if ( annotatedField.isAnnotationPresent( PersistenceContext.class ) ) { PersistenceContext persistenceContext = annotatedField.getAnnotation( PersistenceContext.class ); lookupName = getJndiName( persistenceContext.unitName(), null, persistenceContext.name() ); } if ( lookupName == null || lookupName.trim().length() == 0 ) { lookupName = getComponentEnvName( annotatedClass, annotatedField.getJavaMember().getName(), injectionResources ); } return lookupName; }
private String getNameFromAnnotation(Annotation annotation) { String name = null; if (annotation instanceof Resource) { name = ((Resource) annotation).name(); } else if (annotation instanceof EJB) { name = ((EJB) annotation).name(); } else if (annotation instanceof PersistenceUnit) { name = ((PersistenceUnit) annotation).name(); } else if (annotation instanceof PersistenceContext) { name = ((PersistenceContext) annotation).name(); } return name; } }
@Override protected void checkFieldAnnotation(Field field, Object instance) throws NamingException, IllegalAccessException { super.checkFieldAnnotation(field, instance); if (field.isAnnotationPresent(EJB.class)) { EJB annotation = field.getAnnotation(EJB.class); lookupFieldResource(context, instance, field, annotation.name()); } /*if (field.isAnnotationPresent(WebServiceRef.class)) { WebServiceRef annotation = (WebServiceRef) field.getAnnotation(WebServiceRef.class); lookupFieldResource(context, instance, field, annotation.name()); }*/ if (field.isAnnotationPresent(PersistenceContext.class)) { PersistenceContext annotation = field.getAnnotation(PersistenceContext.class); lookupFieldResource(context, instance, field, annotation.name()); } if (field.isAnnotationPresent(PersistenceUnit.class)) { PersistenceUnit annotation = field.getAnnotation(PersistenceUnit.class); lookupFieldResource(context, instance, field, annotation.name()); } } }
PersistenceContextGenerator(String location, PersistenceContext pc) { _location = location; _jndiName = pc.name(); _unitName = pc.unitName(); _type = pc.type(); _properties = new HashMap(); PersistenceProperty[] propertyList = pc.properties(); for (int i = 0; propertyList != null && i < propertyList.length; i++) { PersistenceProperty prop = propertyList[i]; _properties.put(prop.name(), prop.value()); } }
private PersistenceContextResource createDefinition(PersistenceContext annotation, InjectingComponentType componentType) { String name = annotation.name(); String unitName = annotation.unitName(); PersistenceContextType type = annotation.type(); boolean multiThreaded = Scope.COMPOSITE.getScope().equals(componentType.getScope()); return new PersistenceContextResource(name, unitName, type, factoryServiceContract, multiThreaded); } }
public <X> X getEntityManager(PersistenceContext persistenceContext, Class<X> clazz) { EntityManager obj = getPersistenceContext(persistenceContext.unitName()); if (obj == null) { logger.log(Level.WARNING, WebBeansLoggerFacade.constructMessage(OWBLogConst.WARN_0014, "@PersistenceContext", persistenceContext.unitName())); } return clazz.cast(obj); }
private void applyToMethod(FacesContext facesContext, Method method, PersistenceContext unit, Object instance) { if (method.getName().startsWith("set")) { Object value = null; if (unit.name() != null && !"".equals(unit.name().trim())) { value = lookup(facesContext, JAVA_COMP_ENV + unit.name()); } invokeMethod(facesContext, method, instance, value); } }
private PersistenceContextResourceReference createReference(String name, Member member, PersistenceContext annotation, InjectingComponentType componentType, IntrospectionContext context) { String unitName = annotation.unitName(); PersistenceContextType type = annotation.type(); if (PersistenceContextType.EXTENDED == type) { InvalidPersistenceContextType error = new InvalidPersistenceContextType("Extended persistence contexts not supported: " + unitName, member, componentType); context.addError(error); } boolean multiThreaded = Scope.COMPOSITE == componentType.getScope(); return new PersistenceContextResourceReference(name, unitName, factoryServiceContract, multiThreaded); }