Bonjour,
J'utilise pour mon projet Spring MVC, JPA (EclipseLink) connecté à une base MySQL.
Lorsque je tente un persist() sur mon objet, il refuse de me l'enregistrer en base avec l'erreur suivante :
J'ai détecté que l'erreur venait de la ligne @GeneratedValue(strategy = GenerationType.IDENTITY) de mon entité car quand je change la valeur de la strategy (par exemple sur SEQUENCE), le message d'erreur change en me disant qu'il n'a pas réussi à trouver une table nommé 'sequence'.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'SEQ_GEN_IDENTITY' in field list Error Code: 1109 Call: SELECT SEQ_GEN_IDENTITY.NEXTVAL FROM DUAL Query: ValueReadQuery(sql="SELECT SEQ_GEN_IDENTITY.NEXTVAL FROM DUAL") org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) javax.servlet.http.HttpServlet.service(HttpServlet.java:641) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
Dans ma base de donnée MySQL, le champ id qui a cete annotation est une simple clé primaire de type int et en auto-increment, tout ce qu'il y a de plus basique.
Pouvez-vous m'aider à trouver le soucis ?
Voici mes sources :
Génération de mon objet
Plus loindans mon DAO
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 Utilisateurs u = new Utilisateurs(); u.setEmail("aa@aa.fr"); u.setIsValide(true); u.setPassword("aa"); Utilisateurs createUser = this.utilisateursService.createUser(u);
Ma configuration dans applicationContext.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 @Override public Utilisateurs createUser(Utilisateurs u) { em.persist(u); return u; }
et enfin le bout de code de mon entité JPA où le problème survient
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 <?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="classpath:properties/jdbc.properties" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> <!--<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"></bean> </property> <property name="persistenceXmlLocation" value="/META-INF/persistence.xml" /> </bean>--> <!-- couche de persistance JPA --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"> <property name="databasePlatform" value="org.eclipse.persistence.platform.database.OraclePlatform" /> <property name="showSql" value="true" /> </bean> </property> <property name="jpaPropertyMap"> <props> <prop key="eclipselink.weaving">false</prop> </props> </property> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"> </bean> </property> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> <!--<property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver " /> </property>--> </bean> <!-- DAO EntityManager injection (@PersistenceContext) --> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <!-- Transactions manager (@Transactionnal) --> <tx:annotation-driven transaction-manager="txManager" /> <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- JDBC exceptions translation (@Repository) --> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> </beans>
Merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @NotNull @Column(name = "id") private Integer id;
Résolu :
En cherchant sur l'erreur, je suis tombé sur des forums Oracle. Ça m'a sauté aux yeux. Quand on fait attention, on ne laisse pas trainer des bouts de code partout, il fallait retirer les lignes suivantes du fichier applicationContext.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 <property name="databasePlatform" value="org.eclipse.persistence.platform.database.OraclePlatform" /> <property name="showSql" value="true" />
Partager