... | ... | @@ -48,13 +48,13 @@ Créez un fichier properties qui va reprendre l'ensemble de ses informations, pl |
|
|
Par exemple, `src\main\resources\spring\database.properties` :
|
|
|
|
|
|
```properties
|
|
|
# Exemple pour Hibernate 5
|
|
|
db.driver=com.mysql.cj.jdbc.Driver
|
|
|
db.url=jdbc:mysql://localhost/banque?useSSL=false&serverTimezone=Europe/Paris&allowPublicKeyRetrieval=true
|
|
|
db.login=root
|
|
|
db.password=
|
|
|
db.login=banque
|
|
|
db.password=banque
|
|
|
|
|
|
# https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#configurations
|
|
|
hibernate.dialect=org.hibernate.dialect.MySQLDialect
|
|
|
hibernate.dialect.storage_engine=innodb
|
|
|
hibernate.connection.pool_size=20
|
|
|
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
|
... | ... | @@ -155,6 +155,7 @@ En Hibernate 5, la [sessionFactory](https://docs.spring.io/spring-framework/docs |
|
|
</property>
|
|
|
<property name="hibernateProperties">
|
|
|
<props>
|
|
|
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
|
|
|
<prop key="hibernate.dialect.storage_engine">${hibernate.dialect.storage_engine}</prop>
|
|
|
<prop key="hibernate.connection.handling_mode">${hibernate.connection.handling_mode}</prop>
|
|
|
<prop key="hibernate.connection.pool_size">${hibernate.connection.pool_size}</prop>
|
... | ... | @@ -178,7 +179,7 @@ Vous ne devez pas déclarer vos beans DAOs, les annotations [@Repository](https: |
|
|
## Par classe de configuration - SpringConfigurationData
|
|
|
Créez une classe de configuration Spring `src\main\java\com\banque\spring\SpringConfigurationData.java`, inspirez vous des exercices précédents.
|
|
|
|
|
|
En Hibernate 5, la [sessionFactory](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/orm/hibernate5/LocalSessionFactoryBean.html) ressemblera à :
|
|
|
En Hibernate 5+, la [sessionFactory](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/orm/hibernate5/LocalSessionFactoryBean.html) ressemblera à :
|
|
|
|
|
|
```java
|
|
|
@Autowired
|
... | ... | @@ -187,15 +188,24 @@ En Hibernate 5, la [sessionFactory](https://docs.spring.io/spring-framework/docs |
|
|
@Bean
|
|
|
public org.springframework.orm.hibernate5.LocalSessionFactoryBean sessionFactory() {
|
|
|
// LocalSessionFactoryBean est un factoryBean qui produit des sessionFactory
|
|
|
org.springframework.orm.hibernate5.LocalSessionFactoryBean resu = new org.springframework.orm.hibernate5.LocalSessionFactoryBean();
|
|
|
var resu = new org.springframework.orm.hibernate5.LocalSessionFactoryBean();
|
|
|
resu.setDataSource(this.dataSource());
|
|
|
resu.setMappingResources("hibernate/compte.hbm.xml", "hibernate/operation.hbm.xml", "hibernate/utilisateur.hbm.xml");
|
|
|
Properties hibernateProperties = new Properties();
|
|
|
var hibernateProperties = new Properties();
|
|
|
hibernateProperties.put("hibernate.dialect", this.env.getProperty("hibernate.dialect"));
|
|
|
|
|
|
if (this.env.getProperty("hibernate.dialect.storage_engine") != null) {
|
|
|
// Pour MySQL seulement
|
|
|
hibernateProperties.put("hibernate.dialect.storage_engine", this.env.getProperty("hibernate.dialect.storage_engine"));
|
|
|
}
|
|
|
hibernateProperties.put("hibernate.connection.pool_size", this.env.getProperty("hibernate.connection.pool_size"));
|
|
|
hibernateProperties.put("hibernate.cache.provider_class", this.env.getProperty("hibernate.cache.provider_class"));
|
|
|
hibernateProperties.put("hibernate.cache.use_second_level_cache", this.env.getProperty("hibernate.cache.use_second_level_cache"));
|
|
|
if (this.env.getProperty("hibernate.connection.handling_mode") != null) {
|
|
|
// Pour MySQL seulement
|
|
|
hibernateProperties.put("hibernate.connection.handling_mode", this.env.getProperty("hibernate.connection.handling_mode"));
|
|
|
}
|
|
|
// Option qui ne sert à rien, gestion dans log4j
|
|
|
hibernateProperties.put("hibernate.show_sql", this.env.getProperty("hibernate.show_sql"));
|
|
|
resu.setHibernateProperties(hibernateProperties);
|
|
|
return resu;
|
... | ... | |