JPA + Spring com configuração Hibernate

2 respostas
R

Estou com um problema e não consigo resolver.
Quando persisto , não salva no banco de dados.
Observação: tenho que usar o transactionManager.
Espero que possam me ajudar.

Dao
public abstract class BasicJpaDao extends JpaDaoSupport implements
AbstractBasicDao {

public BasicJpaDao(Class persistentClass) throws DAOException {

this.entityManagerFactory = Persistence

.createEntityManagerFactory(persistenceUnit);

this.entityManager = this.entityManagerFactory.createEntityManager();

this.persistentClass = persistentClass;

}
public T save(T t) {

try {

Long idLong = (Long) MethodUtilities.getAttribute(t, id);

if (idLong == 0) {

this.getJpaTemplate().persist(t);

} else {

this.getJpaTemplate().merge(t);

}

return t;

} catch (Exception e) {

throw new DAOException(e);

}

}

}

// configuração

<?xml version="1.0" encoding="UTF-8"?>

//



hibernate/xxx.hbm.xml
hibernate/xxxx.hbm.xml
hibernate/xxxxxx.hbm.xml
hibernate/xxxxx.hbm.xml

<property name="dataSource">
		<ref local="dataSource"/>
	</property>
	
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">${hibernate.dialect}</prop>
			<prop key="show_sql">true</prop>
			<prop key="use_outer_join">true</prop>
			<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
		</props>
	</property>
</bean>

<!-- DataSource configuration  -->
<bean id="dataSource"
	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName">
		<value>org.postgresql.Driver</value>
	</property>
	<property name="url">
		<value>jdbc:postgresql://xxx.x.xxx.xx/xxx</value>
	</property>
	<property name="username">
		<value>xxxxx</value>
	</property>
	<property name="password">
		<value>xxxxx</value>
	</property>
</bean>

<bean id="entityManagerFactory"
	class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
	<property name="persistenceUnitName" value="persistenceUnit"/>
	<property name="jpaDialect">
		<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
		</bean>
	</property>
	<property name="jpaVendorAdapter">
		<bean
			class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
			<property name="database" value="POSTGRESQL"/>
			<property name="databasePlatform"
				value="org.hibernate.dialect.PostgreSQLDialect"/>
			<property name="showSql" value="false"/>
			<property name="generateDdl" value="false"/>
		</bean>
	</property>
	
	
	
</bean>


<bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
          ref="entityManagerFactory" />
<property name="jpaDialect" ref="jpaDialect"/>
<bean id="jpaDialect"
	class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />






2 Respostas

andreiribas

hm… nunca mexi com jpa so com o hibernate mesmo… mas quando acontece isso é porque você não abriu uma transação e fez commit da transação depois de persistir o objeto

glilco

nesse link explica como

http://forum.springframework.org/showthread.php?t=28395

tem que criar um TransactionProxy para envolver seus DAOs para ele realizar o commit.

Criado 22 de outubro de 2007
Ultima resposta 11 de abr. de 2008
Respostas 2
Participantes 3