Session session = (Session)entityManager.getDelegate(); ClassMetadata metadata = session.getSessionFactory().getClassMetadata(type); String keyName = metadata.getIdentifierPropertyName(); Class<?> keyType = metadata.getIdentifierType().getReturnedClass();
<context:component-scan base-package="com.intersult"/> <aop:config proxy-target-class="true"/> <aop:aspectj-autoproxy/> <bean id="dataSource" class="${dataSource.class}" destroy-method="close"> <property name="URL" value="${dataSource.url}"/> <property name="user" value="${dataSource.username}"/> <property name="password" value="${dataSource.password}"/> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <tx:annotation-driven/>
<filter> <filter-name>EntityManagerFilter</filter-name> <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>EntityManagerFilter</filter-name> <servlet-name>Faces Servlet</servlet-name> </filter-mapping>
Dennoch hier der Code:
public class OpenEntityManagerInSessionFilter extends OncePerRequestFilter { public static final String ENTITY_MANAGER_FACTORY_NAME = "entityManagerFactory"; public static final String ENTITY_MANAGER_NAME = "entityManager"; private String entityManagerFactoryBeanName = ENTITY_MANAGER_FACTORY_NAME; public void setEntityManagerFactoryBeanName(String entityManagerFactoryBeanName) { this.entityManagerFactoryBeanName = entityManagerFactoryBeanName; } public String getEntityManagerFactoryBeanName() { return entityManagerFactoryBeanName; } @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { EntityManagerFactory entityManagerFactory = lookupEntityManagerFactory(); boolean participate = false; try { if (TransactionSynchronizationManager.hasResource(entityManagerFactory)) { participate = true; } else { EntityManager entityManager = (EntityManager)request.getSession().getAttribute(ENTITY_MANAGER_NAME); if (entityManager == null) { entityManager = entityManagerFactory.createEntityManager(); request.getSession().setAttribute(ENTITY_MANAGER_NAME, entityManager); } TransactionSynchronizationManager.bindResource( entityManagerFactory, new EntityManagerHolder(entityManager)); } filterChain.doFilter(request, response); } finally { if (!participate) TransactionSynchronizationManager.unbindResource(entityManagerFactory); } } protected EntityManagerFactory lookupEntityManagerFactory() { WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); return (EntityManagerFactory) wac.getBean(getEntityManagerFactoryBeanName(), EntityManagerFactory.class); } }
Criteria criteria = ((Session)entityManager.getDelegate()).createCriteria(Rule.class); ProjectionList projections = Projections.projectionList(); projections.add(Projections.alias(Projections.groupProperty(selector.property()), "goal")); projections.add(Projections.alias(Projections.rowCount(), "count")); criteria.setProjection(projections); criteria.add(Restrictions.like(selector.property(), text, MatchMode.ANYWHERE)); criteria.addOrder(Order.desc("count")); criteria.setMaxResults(5); criteria.setResultTransformer(Transformers.aliasToBean(Suggestion.class)); return criteria.list();
Criteria criteria = session.createCriteria(Transition.class); DetachedCriteria subquery = DetachedCriteria.forClass(Participant.class); subquery.add(Restrictions.eq("user", user)); subquery.setProjection(Projections.property("role")); criteria.add(Property.forName("actor").in(subquery));
Ein UserType kann auch automatisch angewandt werden, indem er mit entsprechenden Annotationen versehen wird:
@MappedSuperclass @TypeDef(defaultForType = EnumSet.class, typeClass = EnumSetType.class) public class EnumSetType<Type extends Enum<Type>> implements UserType, ParameterizedType { ... }