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

JPA Java Discussion :

Problème d'utilisation JPA+Hibernate+Spring + DB2


Sujet :

JPA Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Janvier 2006
    Messages
    194
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Janvier 2006
    Messages : 194
    Par défaut Problème d'utilisation JPA+Hibernate+Spring + DB2
    Bonjour tous le monde,
    Je travaille sur une application swing et j'utilise JPA,Hibernate et Spring avec une Base de données DB2 installée sur AS400.

    Mon fichier de configuration Spring est le suivant :

    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
    <?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"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     
    	<!-- couches applicatives -->
    	<!-- 
    	<bean id="dao" class="odas.toolbox.persistance.dao.Dao" />
    	 -->
     
    	<bean id="universalDao" class="odas.toolbox.persistance.dao.UniversalDao" />
     
    	<!-- 
    	<bean id="service" class="odas.toolbox.persistance.service.Service">
    		<property name="dao" ref="dao" />
    	</bean>
    	 -->
     
    	<bean id="universalManager" class="odas.toolbox.service.impl.UniversalManagerImpl">
    		<property name="universalDao" ref="universalDao" />
    	</bean>
     
    	<!-- la source de donnéees DBCP -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName" value="com.ibm.as400.access.AS400JDBCDriver" />
    		<property name="url" value="jdbc:as400://tersudm/decisiont" />
    		<property name="username" value="***" />
    		<property name="password" value="***" />
    	</bean>
     
    	<!-- le gestionnaire de transactions -->
    	<tx:annotation-driven transaction-manager="txManager" />
    	<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
     
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
     
    				<property name="showSql" value="true" />
     
    				<property name="databasePlatform" value="org.hibernate.dialect.DB2400Dialect" />
    				<property name="generateDdl" value="false" />
    			</bean>
    		</property>
    		<property name="loadTimeWeaver">
    			<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    		</property>
    	</bean>
     
     
     
     
     
    	<!-- traduction des exceptions -->
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
     
    	<!-- persistence -->
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
     
    </beans>
    L'entité à persister et la suivante :

    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
     
    @SuppressWarnings("unused")
    @Entity
    @Table(name="P08JOINTP")
    public class P08jointp extends BaseObject implements Serializable {
     
    	private static final long serialVersionUID = 3832626160073359411L;
     
    	@Id
    	@Column(name = "P08IDPKJT", nullable = false)	
    	@GeneratedValue(strategy = GenerationType.AUTO)
    	//@Column(name = "P08IDPKJT", nullable = false)	
    	private Integer p08idpkjt;
     
    	@Column(name = "P08LEFTE", length= 4 )
    	private String p08lefte;
     
    	@Column(name = "P08RIGHTE", length= 4)
    	private String p08righte;
     
    	@Column(name = "P08LEFTZ", length= 20)
    	private String p08leftz;
     
    	@Column(name = "P08RIGHTP", nullable = false)
    	private int p08rightp;
     
    	//	--- Constructeurs
     
    	public P08jointp() {}
     
    	public P08jointp(String p08lefte, String p08righte, String p08leftz, int p08rightp) {
    		setP08lefte(p08lefte);
    		setP08righte(p08righte);
    		setP08leftz(p08leftz);
    		setP08rightp(p08rightp);
    	}
     
    // getter and Setter 
    ......
    .....
    Je veux insérer cette entité dans ma DB en utilisant une simple classe main :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    //	configuration de l'application
    		System.out.println ("BEFORE ..") ;
    		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
    		// couche service
    		universalManager = (UniversalManager) ctx.getBean("universalManager");
     
    		// on vide la base
    		P08jointp p = new P08jointp("aaa","bbb","ccc",1);		
     
    		universalManager.save(p);
    mais j'ai toujours cette exception :
    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
     
    BEFORE ..
    14:48:32,850  INFO ClassPathXmlApplicationContext:305 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@82701e: display name [org.springframework.context.support.ClassPathXmlApplicationContext@82701e]; startup date [Mon Feb 23 14:48:32 CET 2009]; root of context hierarchy
    14:48:33,257  INFO ClassPathXmlApplicationContext:317 - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@82701e]: org.springframework.beans.factory.support.DefaultListableBeanFactory@19e15c
    14:48:33,335  INFO ClassPathXmlApplicationContext:976 - Bean 'org.springframework.aop.config.internalAutoProxyCreator' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    14:48:33,382  INFO ClassPathXmlApplicationContext:976 - Bean 'dataSource' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    14:48:33,414  INFO ClassPathXmlApplicationContext:976 - Bean 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#e5bbd6' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    14:48:33,429  INFO ClassPathXmlApplicationContext:976 - Bean 'org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver#8ee016' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    14:48:38,820  INFO ClassPathXmlApplicationContext:976 - Bean 'entityManagerFactory' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    14:48:38,820  INFO ClassPathXmlApplicationContext:976 - Bean 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    14:48:38,836  INFO ClassPathXmlApplicationContext:976 - Bean 'org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    14:48:38,883  INFO ClassPathXmlApplicationContext:976 - Bean 'entityManagerFactory' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    14:48:38,899  INFO ClassPathXmlApplicationContext:976 - Bean 'txManager' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    14:48:38,899  INFO ClassPathXmlApplicationContext:976 - Bean '(inner bean)' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    14:48:38,899  INFO ClassPathXmlApplicationContext:976 - Bean '(inner bean)' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    14:48:38,899  INFO ClassPathXmlApplicationContext:976 - Bean 'org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    Hibernate: insert into P08JOINTP (P08IDPKJT, P08LEFTE, P08RIGHTE, P08LEFTZ, P08RIGHTP) values (default, ?, ?, ?, ?)
    14:48:39,949  WARN JDBCExceptionReporter:71 - SQL Error: -407, SQLState: 23502
    14:48:39,949 ERROR JDBCExceptionReporter:72 - [SQL0407] Valeurs indéfinies non admises dans la colonne ou la variable P08IDPKJT.
    Exception in thread "main" javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not insert: [odas.toolbox.persistance.jpa.hibernate.model.P08jointp]
    	at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:622)
    	at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:585)
    	at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:180)
    	at $Proxy13.persist(Unknown Source)
    	at odas.toolbox.persistance.dao.UniversalDao.save(UniversalDao.java:34)
    	at odas.toolbox.service.impl.UniversalManagerImpl.save(UniversalManagerImpl.java:49)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:585)
    	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:203)
    	at $Proxy16.save(Unknown Source)
    	at odas.toolbox.rmi.test.TestPersistance.main(TestPersistance.java:28)
    Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [odas.toolbox.persistance.jpa.hibernate.model.P08jointp]
    	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
    	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2093)
    	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2573)
    	at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:47)
    	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
    	at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
    	at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
    	at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
    	at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
    	at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
    	at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
    	at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
    	at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
    	at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
    	at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:212)
    	... 20 more
    Caused by: java.sql.SQLException: [SQL0407] Valeurs indéfinies non admises dans la colonne ou la variable P08IDPKJT.
    	at com.ibm.as400.access.JDError.throwSQLException(JDError.java:650)
    	at com.ibm.as400.access.JDError.throwSQLException(JDError.java:621)
    	at com.ibm.as400.access.AS400JDBCStatement.commonPrepare(AS400JDBCStatement.java:1518)
    	at com.ibm.as400.access.AS400JDBCPreparedStatement.<init>(AS400JDBCPreparedStatement.java:185)
    	at com.ibm.as400.access.AS400JDBCConnection.prepareStatement(AS400JDBCConnection.java:1777)
    	at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:394)
    	at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:370)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:585)
    	at org.hibernate.util.GetGeneratedKeysHelper.prepareStatement(GetGeneratedKeysHelper.java:49)
    	at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:487)
    	at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:94)
    	at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.prepare(IdentityGenerator.java:69)
    	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:30)
    	... 34 more
    Merci d'avance pour votre aide

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Février 2009
    Messages
    43
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 43
    Par défaut Id auto
    Comment tu as géré l'ID automatique sous DB2 ?

  3. #3
    Membre confirmé
    Inscrit en
    Janvier 2006
    Messages
    194
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Janvier 2006
    Messages : 194
    Par défaut
    J'ai pas généré les ID sur DB2, le problème c que j'ai testé ce code sur Oracle et ça marche bien avec MySQL5 ça marche bien et même avec DB2 sur Gentoo ça marche bien, le problème ça viens uniquement lorsque j'attaque la base de données qui est sur AS400

  4. #4
    Membre confirmé
    Inscrit en
    Janvier 2006
    Messages
    194
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Janvier 2006
    Messages : 194
    Par défaut
    Y a pas de solution sur ce forum?

  5. #5
    Membre Expert
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 963
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 963
    Par défaut
    Citation Envoyé par menzlitsh Voir le message
    Y a pas de solution sur ce forum?
    si ça marche partout sauf dans un cas :

    cherchez toutes les différences de configuration…
    y compris dans des choses comme les droits d'accès à toutes les tables, index, …

  6. #6
    Membre éclairé
    Profil pro
    Inscrit en
    Février 2009
    Messages
    43
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 43
    Par défaut Suite
    S'il y a une solution sur le forum je vais bien sur te laisser la chercher

    Je pense sincèrement qu'il s'agit d'un problème au niveau de ton schéma DB2.
    Si tu indique AUTO, c'est que tu veut utiliser le système le plus adapté à ton type de base de données : Par exemple IDENTITY sous SQL Server ou le couple Trigger + sequence sous Oracle. Dans ton cas Auto est apparmment le bon choix vu que tu as l'air d'essayer sur plusieurs base.

    Mais ce n'est pas parce que tu choisis Auto qu'il n'y a rien a faire (enfin je pense que je ne t'apprends rien).

    Je n'ai que de vagues souvenirs de comment fonctionne les ID auto sur DB2, j'ai vu de tout et même bien souvent sous AS/400 des programmes RPG allant rechercher des ID séquantiels dans une table dédiée (eh oui...vive la modernité). En plus simple, je pense que le couple Trigger + Sequence comme oracle doit fonctionner.

    - Essaye ensuite tout simplement un insert dans la table à la main sans indiquer l'ID et dis moi si l'ID est généré.
    - Si Auto ne passe pas, essaye avec Sequence en indiquant ta sequence, juste pour avoir voir un peu mieux l'ampleur du problème.

    Ah et puis dernière chose, je pense que persist() serait mieux venu que save() pour récupérer ton ID en retour (si mes souvenirs sont bons encore une fois).

    A+

  7. #7
    Membre Expert
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 963
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 963
    Par défaut
    Citation Envoyé par JeanDavidKevin Voir le message

    Ah et puis dernière chose, je pense que persist() serait mieux venu que save() pour récupérer ton ID en retour (si mes souvenirs sont bons encore une fois).

    A+
    entitymanager.persist() ne renvoit rien du tout…
    et les IDs sont générés au moment du commit…

    session.save() renvoit l'ID
    (il y a aussi Session.getIdentifier())

    mais en principe si on utilise JPA c'est pour éviter d'accéder à Hibernate.Session…
    ou tout au moins éviter de mélanger des appels de niveau conceptuels différents dans le même bloc de code…

Discussions similaires

  1. JPA/Hibernate/Spring sans Java EE ?
    Par dingoth dans le forum JPA
    Réponses: 10
    Dernier message: 08/09/2011, 11h47
  2. Réponses: 0
    Dernier message: 19/11/2009, 14h54
  3. [Data] Comment utiliser JPA et Spring ?
    Par fredege dans le forum Spring
    Réponses: 3
    Dernier message: 11/06/2009, 09h37
  4. Utiliser JPA/Hibernate avec JBoss et Oracle
    Par GrooveRage dans le forum Wildfly/JBoss
    Réponses: 5
    Dernier message: 12/03/2009, 18h30

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