Problème persistence Hibernate/JPA
Bonjour,
Je vous sollicite afin de trouver de l'aide. J'ai passé des heures a chercher sur internet et essayé diverses techniques ...
J'expose mon problème :
Création d'un site en Java sous Tomcat 7 utilisant Spring 3 hibernate 3.5 et JPA.
Je me suis très fortement inspiré du Tutoriel Hibernate/JPA - Spring2.5 - Tapestry5.
Le problème est dès que j'essaye de persist un objet ... La persist s'effectue (d'après les logs) mais aucun résultat sur la base de donnée ! Et aucune erreur dans les logs ...
Je vous joins le maximum d'information, dont les logs en question :
Log
Code:
1 2
| [DEBUG,AddressDAOImpl,http-8080-exec-6] persisting Address instance
[DEBUG,AddressDAOImpl,http-8080-exec-6] persist successful |
Context
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="project-properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
<property name="locations">
<list>
<value>classpath:database.properties</value>
</list>
</property>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${hibernate.connection.driver_class}" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect">
<bean class="${jpa.dialect}" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.${jpa.vendor.adapter}">
<property name="showSql" value="${hibernate.show_sql}" />
<property name="databasePlatform" value="${hibernate.dialect}" />
<property name="generateDdl" value="false" />
</bean>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven/>
</beans> |
persistence.xml
Code:
1 2 3 4 5 6 7 8 9
|
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="manager" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>
</persistence> |
AddressServiceImpl.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| package com.photoshop.service.impl;
import java.util.List;
import com.photoshop.domain.Address;
import com.photoshop.domain.dao.AddressDAO;
import com.photoshop.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@Service("AddressService")
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class AddressServiceImpl implements AddressService {
private final Log log = LogFactory.getLog(this.getClass());
@Autowired
private AddressDAO AddressDao;
public List<Address> findAll() {
return AddressDao.findAll();
}
public Address findById(Integer id) {
return AddressDao.findById(id);
}
@Transactional(readOnly = false)
public Address merge(Address detachedAddress) {
return AddressDao.merge(detachedAddress);
}
@Transactional(readOnly = false)
public void persist(Address transientAddress) {
AddressDao.persist(transientAddress);
}
@Transactional(readOnly = false)
public void remove(Address persistentAddress) {
AddressDao.remove(persistentAddress);
}
@Transactional(readOnly = false)
public void remove(Integer AddressId) {
AddressDao.remove(AddressId);
}
} |
AddressDAOImpl.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| package com.photoshop.domain.dao.impl;
import java.util.List;
import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.photoshop.domain.Address;
import com.photoshop.domain.dao.AddressDAO;
@Repository("AddressDAO")
public class AddressDAOImpl implements AddressDAO {
private static final Log log = LogFactory.getLog(AddressDAOImpl.class);
@PersistenceContext
private EntityManager entityManager;
@Override
public void persist(Address transientAddress) {
log.debug("persisting Address instance");
try {
entityManager.persist(transientAddress);
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
}
}
@Override
public void remove(Address persistentAddress) {
log.debug("removing Address instance");
try {
entityManager.remove(persistentAddress);
entityManager.close();
log.debug("remove successful");
} catch (RuntimeException re) {
log.error("remove failed", re);
}
}
@Override
public void remove(Integer AddressId) {
this.remove(this.findById(AddressId));
}
@Override
public Address merge(Address detachedAddress) {
log.debug("merging Address instance");
try {
Address result = entityManager.merge(detachedAddress);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
return null;
}
}
@Override
public Address findById(Integer id) {
log.debug("getting Address instance with id: " + id);
try {
Address instance = entityManager.find(Address.class, id);
log.debug("findById successful");
return instance;
} catch (RuntimeException re) {
log.error("findById failed", re);
return null;
}
}
@Override
public List<Address> findAll() {
log.debug("getting all Address instances");
try {
Query query = entityManager.createQuery(
"SELECT a " +
"FROM Address a");
List<Address> AddressList = (List<Address>) query.getResultList();
log.debug("findAll successful");
return AddressList;
} catch (RuntimeException re) {
log.error("findAll failed", re);
return new ArrayList<Address>();
}
}
} |
Le bout de code du controller
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Controller
public class UserController {
@Autowired
private AddressService addressService;
....
Address address = new Address(countryService.findById(registerForm.getCountry()), registerForm.getStreet(),
registerForm.getPostcode(), registerForm.getCity(),
registerForm.getUser().getFirstname() + registerForm.getUser().getLastname());
addressService.persist(address);
...
} |
Voila j'espère donner toutes les informations possible. Si besoin je peux joindre bien évidemment d'autre informations.
Je sais que l'on peut gérer soit même les transactions. Mais j'aimerais fortement utiliser les annotations.
Merci d'avance
as-tu trouvé la réponse ?
Bonjour kakawait,
as-tu reçu une réponse depuis que tu as posté ?
J'ai un problème identique, si tu as trouvé la solution cela m'intéresse de savoir comment .
merci beaucoup
oum