Hibernate 4 et SessionFactory
Bonjour à tous et à toutes,
J'ai crée un test JUnit permettant de tester l'ajout en Base d'une entitée. J'utilise Hibernate-core en version 4 déclaré dans un fichier pom.xml.
Voici mon code :
Code:
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
|
public class TacheTest {
private final static Logger logger = LogManager.getLogger(MainTacheApplication.class.getName());
private static SessionFactory maSessionFactory;
private static ServiceRegistry serviceRegistry;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
maSessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch(Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return maSessionFactory.openSession();
}
@Test
public void testCreateEntity() throws SQLException {
Transaction transaction = getSession().beginTransaction();
// Instanciation de la tâche
Tache maTache = new Tache();
logger.info("Trying to create a test connection with the database.");
maTache.setDescription("maPremiereTache");
maTache.setDateDeCreation(new Date());
// l'objet est en session pour être manipulé
getSession().save(maTache);
transaction.commit();
Assert.assertNotNull(maTache.getIdTache()); // on vérifie l'assertion
}
} |
Lors de l'éxécution du test, j'ai le message suivant dans le Console.
Code:
1 2 3 4 5 6 7 8
|
2013-12-30 11:38:31,226 INFO [main] common.Version (Version.java:37) | Hibernate Commons Annotations 3.2.0.Final
déc. 30, 2013 11:38:31 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.3.Final}
déc. 30, 2013 11:38:31 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.username="postgres", hibernate.connection.password=****, hibernate.dialect="org.hibernate.dialect.PostgreSQLDialect", hibernate.show_sql=true, hibernate.connection.url="jdbc:postgresql://localhost:5432/tacheapp", hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.driver_class="org.postgresql.Driver", hibernate.format_sql=true}
déc. 30, 2013 11:38:31 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist |
Merci d'avance.
Transact.