Donc j'ai une classe Document abstraite et une classe héritant de Document, DocumentHTML qui elle n'est pas abstraite.
J'aimerai faire persister toutes mes instances de Document dans une même table.
Je fais des tests unitaires dans mon projet Maven, mais je ne passe pas un test simple d'insertion d'un document.
Je ne comprends pas l'erreur que me renvoie maven.
Erreur :
Code de Document :-------------------------------------------------------------------------------
Test set: fr.natoine.annotations_persistence.document.DocumentTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.778 sec <<< FAILURE!
testInsertDocument(fr.natoine.annotations_persistence.document.DocumentTest) Time elapsed: 0.767 sec <<< FAILURE!
java.lang.AssertionError
at org.hibernate.cfg.AnnotationBinder.getElementsToProcess(AnnotationBinder.java:817)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:667)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1162)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1226)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:173)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
at fr.natoine.annotations_persistence.document.DocumentTest.testInsertDocument(DocumentTest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:228)
at junit.framework.TestSuite.run(TestSuite.java:223)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
Code de DocumentHTML :
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 package fr.natoine.annotations_persistence.document; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.MappedSuperclass; import javax.persistence.Table; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement @Entity @MappedSuperclass @Table(name = "DOCUMENT") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public abstract class Document implements Serializable { @Id @GeneratedValue @Column(name = "DOCUMENT_ID") private Long id; @Column(name = "DOCUMENT_TITLE") private String title; @Column(name = "DOCUMENT_URL") private String url; @Column(name = "DOCUMENT_DATE_CREATION") private Date creation_in_context; @Column(name = "DOCUMENT_CONTEXT") private String context; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Date getCreation_in_context() { return creation_in_context; } public void setCreation_in_context(Date creationInContext) { creation_in_context = creationInContext; } public String getContext() { return context; } public void setContext(String context) { this.context = context; } public Long getId() { return id; } }
Code de Test :
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 package fr.natoine.annotations_persistence.document; import javax.persistence.Column; import javax.persistence.Entity; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement @Entity public class DocumentHTML extends Document { @Column(name = "DOCUMENTHTML_CONTENT") private String content_html; public String getContent_html() { return content_html; } public void setContent_html(String contentHtml) { content_html = contentHtml; } }
persistence.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 package fr.natoine.annotations_persistence.document; import java.util.Date; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import junit.framework.TestCase; public class DocumentTest extends TestCase { public DocumentTest(String name) { super(name); } public void testInsertDocument() { //Créer un document Document _doc = new DocumentHTML(); _doc.setContext("test"); _doc.setCreation_in_context(new Date()); _doc.setTitle("doc de test"); _doc.setUrl("url de test"); //le rendre persistant EntityManagerFactory emf = Persistence.createEntityManagerFactory("annotation"); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); em.persist(_doc); tx.commit(); em.close(); } }
et hibernate.cfg.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <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_1_0.xsd" version="1.0"> <persistence-unit name="annotation"> <properties> <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/> </properties> </persistence-unit> </persistence>
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 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- PostGresql connection settings --> <!-- <property name="connection.url">jdbc:postgresql://localhost/firsthibernate</property> <property name="connection.username">postgres</property> <property name="connection.driver_class">org.postgresql.Driver</property> <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> <property name="connection.password">p</property> --> <!-- MySQL connection settings --> <property name="connection.url">jdbc:mysql://localhost/hibSample</property> <property name="connection.username">hibernate_user</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.password">hibernate_pwd</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- HSQL connection settings --> <!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.url">jdbc:hsqldb:hsql://localhost</property> <property name="connection.username">sa</property> <property name="connection.password"></property> <property name="dialect">org.hibernate.dialect.HSQLDialect</property> --> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <!-- mapping resource --> <!-- <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> --> <!-- List of annotated Class --> <mapping class="fr.natoine.annotations_persistence.selection.Selection"/> <mapping class="fr.natoine.annotations_persistence.selection.SelectionHTML"/> <mapping class="fr.natoine.annotations_persistence.annotation.Annotation"/> <mapping class="fr.natoine.annotations_persistence.annotation.AnnotationDocument"/> <mapping class="fr.natoine.annotations_persistence.annotation.AnnotationSelection"/> <mapping class="fr.natoine.annotations_persistence.document.Document"/> <mapping class="fr.natoine.annotations_persistence.document.DocumentHTML"/> <mapping class="fr.natoine.annotations_persistence.document.post.Post"/> <mapping class="fr.natoine.annotations_persistence.document.post.FreeText"/> <mapping class="fr.natoine.annotations_persistence.document.post.SimpleLabel"/> <mapping class="fr.natoine.user_persistence.User"/> </session-factory> </hibernate-configuration>
Partager