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

Spring Java Discussion :

jpaTemplate.persist() ne sinsere pas en base de donnée


Sujet :

Spring Java

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 13
    Par défaut jpaTemplate.persist() ne sinsere pas en base de donnée
    je tente d'insérer un objet en base de donnée mais celui ci ne sinsere pas en base de donnée
    J'ai réalise un test unitaire pour vérifier mon code Java et celui insere bien les données Je ne comprends pas dou peux venir mon erreur

    Par elimination je pense que ca doit venir dun probleme de chargement des context spring ou bien de jpaTemplate.persist y a til une option speciale ?



    Quelqu'un aurait il une idée ?

    merci pour votre aide

    TEST Junit [OK]
    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
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations ={ "classpath*:/META-INF/spring/myshop-data.xml"})
    public class Account2ControllerTest {
        protected final Log logger = LogFactory.getLog(getClass());
        @Autowired
        @Qualifier("platService")
        private IPlateService mplathome ; 
     
        @Test
        public void test2(){
        Plat plat1 = new Plat("Carpacio XL"," Carpacio et pates a volonte!! Delichieux filet de ","heyehey ");
     
            Plat plat2 = new Plat("vChoco","Deliciosooo !!Delichieux filet de ","heyehey ");
     
            mplathome.persist(plat1);
            mplathome.persist(plat2);
        }
    }

    Ma couche DAO

    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
    
    public abstract class AbstractDAO<T>  {
            
            private Class<T> entityClass;
            
            protected JpaTemplate jpaTemplate;
    
            protected Class<T> getEntityClass() {
                    return entityClass;
            }
            
            @PersistenceUnit
            public void setEntityManagerFactory(EntityManagerFactory emf) {
                    this.jpaTemplate = new JpaTemplate(emf);
            }
    
            @SuppressWarnings("unchecked")
            public AbstractDAO(Class<T> entityClass) {
                    this.entityClass = entityClass;
            }
            
         
    
            @SuppressWarnings("unchecked")
            public Collection<T> getAll() {
                    return jpaTemplate.find("from " + entityClass.getSimpleName());
            }
    
           //@Transactional marche pas mieux avec
    
            public void persist(T t) {
            	System.out.println(">>>>>>>>> aVANT"+t.toString());
        		jpaTemplate.persist(t);
        	
        		
        		System.out.println("Fin ");
        	
        	}
            
            public T getById(Long id) {
                    return jpaTemplate.find(entityClass, id);
            }
            
            public void remove(T t) {
                    jpaTemplate.remove(t);          
            }
    
    }

    MON Dao

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
     
    @Repository
    public class PlatHome extends AbstractDAO<Plat> implements IPlatHome {
     
        private static final Log log = LogFactory.getLog(PlatHome.class);
     
     
        public PlatHome(){
            super(Plat.class);
        }
    }

    Mes fichiers de config SPRING

    myshop-data.xml

    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
     
    <?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:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
     
     
    	<!-- Déclaration du PropertyPlaceholderConfigurer -->
    	<bean id="propertyConfigurer"
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<value>classpath*:db.properties</value>
    			</list>
    		</property>
    	</bean>
     
    	<!-- traduction des exceptions -->
    	<bean
    		class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
     
     
    	<bean
    		class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
     
    	<bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
    		<property name="persistenceUnitName" value="jpaTutos" />
    		<property name="dataSource" ref="dataSource" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="database" value="MYSQL" />
    				<property name="showSql" value="true" />
    			</bean>
    		</property>
    	</bean>
     
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost/restaurant" />
    		<property name="username" value="root" />
    		<property name="password" value="" />
    	</bean>
     
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
     
    	<!--
    		Indique a Spring que les transactions sont geree par @Transactional
    	-->
    	<tx:annotation-driven transaction-manager="transactionManager" />
     
    	<!--
    		Indique a Spring que la configuration des beans se fait par
    		annotations
    	-->
    	<context:component-scan base-package="com.myshop">
    		<context:exclude-filter type="annotation"
    			expression="org.springframework.stereotype.Controller" />
    	</context:component-scan>
    	<context:annotation-config />
     
    </beans>


    et mon application context cote servlet MVC

    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
    <?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:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<!--
    		Scans the classpath of this application for @Components to deploy as beans
    	-->
    
    	<context:component-scan base-package="com.myshop">
    		<context:include-filter type="annotation"
    			expression="org.springframework.stereotype.Controller" />
    	</context:component-scan>
    
    
    
    	<!-- Application Message Bundle -->
    	<bean id="messageSource"
    		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    		<property name="basename" value="/WEB-INF/messages/messages" />
    		<property name="cacheSeconds" value="0" />
    	</bean>
    
    	<!-- Configures Spring MVC -->
    	<import resource="mvc-config.xml" />
    
    </beans>

    Mon fichier web.xml

    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
     
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     
    	<!-- Reads request input using UTF-8 encoding -->
    	<filter>
    		<filter-name>characterEncodingFilter</filter-name>
    		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    		<init-param>
    			<param-name>encoding</param-name>
    			<param-value>UTF-8</param-value>
    		</init-param>
    		<init-param>
    			<param-name>forceEncoding</param-name>
    			<param-value>true</param-value>
    		</init-param>
    	</filter>
     
    	<filter-mapping>
    		<filter-name>characterEncodingFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
     
    	<!--
    		Enables clean URLs with JSP views e.g. /welcome instead of
    		/app/welcome
    	-->
    	<filter>
    		<filter-name>UrlRewriteFilter</filter-name>
    		<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    	</filter>
     
    	<filter-mapping>
    		<filter-name>UrlRewriteFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
     
    	<!-- Handles all requests into the application -->
    	<servlet>
    		<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>
    				/WEB-INF/spring/app-config.xml
     
    			</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
     
     
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
     
    			/WEB-INF/spring/myshop-data.xml
    		</param-value>
     
    	</context-param>
     
    	<servlet-mapping>
    		<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    		<url-pattern>/app/*</url-pattern>
    	</servlet-mapping>
     
     
    	<!-- ajout -->
    	 <listener>
            <listener-class>
                org.springframework.web.context.ContextLoaderListener
            </listener-class>
        </listener>
     
     
     	
    	<filter>
    		<filter-name>JpaFilter</filter-name>
    		<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    	</filter>
    </web-app>

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 13
    Par défaut
    J'ai trouvé cette solution
    http://forum.springsource.org/showpo...93&postcount=8

    depuis le site
    http://forum.springsource.org/showthread.php?t=73688

    Quelqu'un aurait il une autre solution à me proposer ?
    Comment cela se fait til que JPA Hibernate ne commit pas objet nativement


    Le code en question
    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
     
    public abstract class AbstractDAO<T>  {
     
            private Class<T> entityClass;
     
            protected JpaTemplate jpaTemplate;
     
            protected Class<T> getEntityClass() {
                    return entityClass;
            }
     
            @Autowired
            JpaTransactionManager transactionManager;
     
       @Transactional
            public void persist(T t) {
            	TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
            	transactionManager.commit(status);
        		jpaTemplate.persist(t);
     
    }

Discussions similaires

  1. le programme ne trouve pas la base de données
    Par frederix quest dans le forum Windows Forms
    Réponses: 4
    Dernier message: 03/08/2007, 14h42
  2. Réponses: 2
    Dernier message: 08/06/2007, 15h38
  3. [phpMyAdmin] phpMyAdmin n'affiche pas les bases de données ?
    Par delassuss dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 4
    Dernier message: 12/04/2007, 08h44
  4. MySQL ne trouve pas la base de données
    Par alfigor dans le forum Outils
    Réponses: 3
    Dernier message: 26/06/2006, 13h42
  5. DBListBox n'affiche pas ma base de données !
    Par vins91 dans le forum C++Builder
    Réponses: 3
    Dernier message: 15/03/2005, 22h35

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