IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Persistance des données Java Discussion :

Problème persistence Hibernate/JPA


Sujet :

Persistance des données Java

  1. #1
    Membre régulier
    Inscrit en
    Janvier 2011
    Messages
    10
    Détails du profil
    Informations forums :
    Inscription : Janvier 2011
    Messages : 10
    Par défaut 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 : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    [DEBUG,AddressDAOImpl,http-8080-exec-6] persisting Address instance
    [DEBUG,AddressDAOImpl,http-8080-exec-6] persist successful
    Context
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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

  2. #2
    oum
    oum est déconnecté
    Membre confirmé
    Profil pro
    Développeur Java
    Inscrit en
    Avril 2006
    Messages
    56
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2006
    Messages : 56
    Par défaut 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

  3. #3
    Membre régulier
    Inscrit en
    Janvier 2011
    Messages
    10
    Détails du profil
    Informations forums :
    Inscription : Janvier 2011
    Messages : 10
    Par défaut
    Oui j'ai résolu mon problème. Le seul hic c'est que je sais pas trop comment j'y suis arrivé... C'est en modifiant un peu mes fichiers de confs !

    La seule aide que je peux te donner c'est mon lien bitbucket avec le projet. Vu qu'il s'agissait d'un projet scolaire je n'ai pas eu le temps de trop chercher la raison de pourquoi cela fonctionne maintenant mais je m'y pencherais un jour !

    https://bitbucket.org/kakawait/photoshop/src

    Tu y trouveras les fichiers web.xml, app-servlet.xml et app-security.xml.

    Attention il existe de fichier app-security.xml c'est celui dans le package resources qui est la dernière version.

Discussions similaires

  1. Problème persistance Hibernate
    Par maxime8n dans le forum Hibernate
    Réponses: 9
    Dernier message: 10/04/2012, 14h21
  2. [débutant]problème avec Hibernate/JPA
    Par amnass dans le forum JPA
    Réponses: 5
    Dernier message: 19/05/2009, 15h02
  3. Problème d'utilisation JPA+Hibernate+Spring + DB2
    Par menzlitsh dans le forum JPA
    Réponses: 9
    Dernier message: 27/02/2009, 11h19
  4. Réponses: 2
    Dernier message: 11/07/2008, 14h44
  5. Réponses: 7
    Dernier message: 07/01/2008, 15h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo