Key / Id Property ermitteln#
Session session = (Session)entityManager.getDelegate();
ClassMetadata metadata = session.getSessionFactory().getClassMetadata(type);
String keyName = metadata.getIdentifierPropertyName();
Class<?> keyType = metadata.getIdentifierType().getReturnedClass();
Spring JPA#
applicationContext.xml:
<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/>
Spring Open EntityManager once in View Pattern#
web.xml:
<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>
Spring Open EntityManager in Session#
Nicht getestet ob das praktikabel ist.
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 Query#
In Hibernate sind die Criteria Querys eine der mächstigsten Instrumente, da sie rein durch ein Programm erzeugt werden können.
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();