Ich denke wenn ihr kurz mal in den Code schaut könnt ihr mir nach kurzem Sagen was ich falsch mache aber irgend wie find ich es selber auch nach Tagen nicht raus.
Ich habe ein Session Bean mit laufender CMT Transaktion in dem ich ein Entity Bean lade und verändere. Beim Commit der Transaktion schreibt mir Hibernate die Änderung jedoch nicht ohne expliziten Aufruf von Flush auf die DB. Warum???
Soweit ich das verstehe müsste doch das nun Hibernate mit JPA im EE automatisch machen?
Problem liegt im PatientRepositoryImpl.approvePatient wo ich das EntityBean über die Basisklasse lade, dann verändere und zurückgebe. Ich meinte eigentlich, dass beim Commit der Transaktion (Transaktion wird commited) die Änderungen gegen die DB geschrieben werden müssten. Liege ich da falsch?
/**
* @author Copyright (c) 2007 by BEA Systems. All Rights Reserved.
* @author <a href="mailto:
[email protected]">Li Shen</a>
*/
public abstract class BaseUserRepositoryImpl<T extends User> extends EntityRepositorySupport<T, Long> implements
UserRepository<T> {
public int countByUsername(String username) {
return countByProperty(getQueryForCountByUsername(), new Property("username", username));
}
public int countByUsernameAndPassword(String username, String password) {
return countByProperties(getQueryForCountByUsernameAndPassword(), new Property("username", username),
new Property("password", password));
}
public T findByUsernameAndPassword(String username, String password) {
return findByUniqueProperties(getQueryForFindByUsernameAndPassword(), new Property("username", username),
new Property("password", password));
}
protected abstract String getQueryForCountByUsername();
protected abstract String getQueryForCountByUsernameAndPassword();
protected abstract String getQueryForFindByUsernameAndPassword();
}
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public abstract class EntityRepositorySupport<T, ID extends Serializable> implements EntityRepository<T, ID> {
@SuppressWarnings("unchecked")
private final Class<T> entityClass = ClassUtils.getGenericArgumentType(getClass());
// TODO customize persistence unit?
@PersistenceContext
private EntityManager entityManager;
protected int countByProperties(String namedQuery, Object... propertyValues) {
Long count = (Long) createNamedQuery(namedQuery, propertyValues).getSingleResult();
return count.intValue();
}
protected int countByProperties(String namedQuery, Property... properties) {
return countByProperties(namedQuery, (Object[]) properties);
}
protected int countByProperty(String namedQuery, Object propertyValue) {
return countByProperties(namedQuery, propertyValue);
}
protected int countByProperty(String namedQuery, String propertyName, Object propertyValue) {
return countByProperties(namedQuery, new Property(propertyName, propertyValue));
}
private Query createNamedQuery(String namedQuery, Object... propertyValues) {
Query query = getEntityManager().createNamedQuery(namedQuery);
int i = 1;
for (Object propertyValue : propertyValues) {
if (propertyValue instanceof Property) {
Property property = (Property) propertyValue;
query.setParameter(property.name, property.value);
}
else {
query.setParameter(i, propertyValue);
}
i++;
}
return query;
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void delete(ID id) {
// TODO maybe use a query instead
entityManager.remove(find(id));
}
public T find(ID id) {
return entityManager.find(entityClass, id);
}
@SuppressWarnings("unchecked")
protected List<T> findByProperties(String namedQuery, Object... propertyValues) {
return createNamedQuery(namedQuery, propertyValues).getResultList();
}
protected List<T> findByProperties(String namedQuery, Property... properties) {
return findByProperties(namedQuery, (Object[]) properties);
}
protected List<T> findByProperty(String namedQuery, Object propertyValue) {
return findByProperties(namedQuery, propertyValue);
}
protected List<T> findByProperty(String namedQuery, String propertyName, Object propertyValue) {
return findByProperties(namedQuery, new Property(propertyName, propertyValue));
}
@SuppressWarnings("unchecked")
protected T findByUniqueProperties(String namedQuery, Object... propertyValues) {
try {
return (T) createNamedQuery(namedQuery, propertyValues).getSingleResult();
}
catch (NoResultException e) {
return null;
}
}
protected T findByUniqueProperties(String namedQuery, Property... properties) {
return findByUniqueProperties(namedQuery, (Object[]) properties);
}
protected T findByUniqueProperty(String namedQuery, Object propertyValue) {
return findByUniqueProperties(namedQuery, propertyValue);
}
protected T findByUniqueProperty(String namedQuery, String propertyName, Object propertyValue) {
return findByUniqueProperties(namedQuery, new Property(propertyName, propertyValue));
}
protected EntityManager getEntityManager() {
return entityManager;
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void save(T entity) {
entityManager.persist(entity);
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public T update(T entity) {
T retVal = entityManager.merge(entity);
//entityManager.flush();
return retVal;
}
}