Bonjour,
Existe t'il un mécanisme avec Spring & Hibernate pour crées le schéma de mes tables dans ma BD (la 1er fois quand elle est vide) de la même façon que c'est fait dans les EJB avec leur fichier persistance.xml
Merci
Bonjour,
Existe t'il un mécanisme avec Spring & Hibernate pour crées le schéma de mes tables dans ma BD (la 1er fois quand elle est vide) de la même façon que c'est fait dans les EJB avec leur fichier persistance.xml
Merci
Oui c'est tout à fait possible.
Il faut gérer la propriété hibernate.hbm2ddl.auto dans ton fichier de configuration si tu utilises Hibernate.
ok je suis ce tuto http://blog.netapsys.fr/index.php/po...iers-d-imports
aussi voilà mon fichier persistance.xml (qui contient mes config hbm2ddl.auto)
et ma config spring-config-dao.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 <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" 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"> <persistence-unit name="e-ticket-jpa-hibernate-postgresPU" transaction-type="RESOURCE_LOCAL"> <props> <!-- Génère le schema de base --> <prop key="hibernate.hbm2ddl.auto">create</prop> <!-- Fichiers d'alimentation de données --> <prop key="hibernate.hbm2ddl.import_files">/import.sql</prop> </props> <class>ca.etsmtl.gti525.entity.presentation.Artiste</class> <class>ca.etsmtl.gti525.entity.presentation.Representation</class> <class>ca.etsmtl.gti525.entity.presentation.Spectacle</class> <class>ca.etsmtl.gti525.entity.presentation.Billet</class> <class>ca.etsmtl.gti525.entity.presentation.Salle</class> <class>ca.etsmtl.gti525.entity.vente.Adresse</class> <class>ca.etsmtl.gti525.entity.vente.CarteCredit</class> <class>ca.etsmtl.gti525.entity.vente.Client</class> <class>ca.etsmtl.gti525.entity.vente.Reservation</class> </persistence-unit> </persistence>
aussi j'ai mis le fichier import.sql dans le racine elle fait juste un crée table d'une seul table "Artiste" )
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 <?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="daoVente" class="ca.etsmtl.gti525.dao.vente.DaoJpaVente" /> <bean id="daoPresentation" class="ca.etsmtl.gti525.dao.presentation.DaoJpaPresentation" /> <!-- EntityManagerFactory --> <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="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" /> <!-- <property name="showSql" value="true" /> <property name="generateDdl" value="true" /> --> </bean> </property> </bean> <!-- la source de donnéees DBCP --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.postgresql.Driver"/> <property name="url" value="jdbc:postgresql://localhost:5432/e-ticket"/> <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> <!-- traduction des exceptions --> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> <!-- persistence --> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> </beans>
Merci de me dire si je fait bien les chose parce-que ma table n'est pas crées.
Je ne configure pas mes projets comme cela.
Voilà le contenu de mon persistence.xml
Puis la conf de mon entity manager dans mon fichier de conf spring (je fais référence au persistence unit de mon 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 <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="sample"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> <property name="hibernate.show_sql" value="true" /> </properties> </persistence-unit> </persistence>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="sample"/> <property name="dataSource" ref="dataSource"/> </bean>
Merci sa marcher très bien.
La prochaine étape c'est d'intégré JavaDB (derby) en embraqué dans mon application.
je cherche quelque chose qui est équivalent a ça
merci de me dire si sa existe (j'ai pas trouvés des ref sur le net)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 <property hibernate org.apache.derby.jdbc.EmbeddedDriver Auto-start
A toi de bien paramétrer ton datasource avec le bon driver et la bonne url.
Essaie ces valeurs :
Driver embedded Derby: org.apache.derby.jdbc.EmbeddedDriver
Url de connexion: jdbc:derby:test;create=true
Le create sert à créer la base nommée test si elle n'existe pas.
A tester...
Oui c'est exactement ce que j'ai fait : (je les mis dans autre post) ici : http://www.developpez.net/forums/d13...ernate-tomcat/
Mais j'ai besoin que la JavaDB (Derby) se lancer au déploiement ou au build de l'application par exemple. (bien sûr en utilisant Derby que j'ai mis dans mon POM.xml)
Partager