[ Hibernate ] Question de débutant
Salut à tous,
voilà j'essaye de me mettre à hibernate et du coup, aprés un peu de lecture, j'ai pris un petit exemple sur internet pour voir comment sa marche .... et ben ca marche pas :) mais je ne trouve pas pourquoi.
Tout d'abord voilà ma log wildfly :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 10:22:14,801 INFO [stdout] (default task-2) do get method
10:22:14,812 INFO [stdout] (default task-2) Init
10:22:15,007 INFO [org.hibernate.Version] (default task-2) HHH000412: Hibernate Core {5.2.9.Final}
10:22:15,009 INFO [org.hibernate.cfg.Environment] (default task-2) HHH000206: hibernate.properties not found
10:22:15,511 INFO [org.hibernate.annotations.common.Version] (default task-2) HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
10:22:15,611 WARN [org.hibernate.orm.connections.pooling] (default task-2) HHH10001002: Using Hibernate built-in connection pool (not for production use!)
10:22:15,612 INFO [org.hibernate.orm.connections.pooling] (default task-2) HHH10001005: using driver [org.mariadb.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/SuiviPoid2]
10:22:15,613 INFO [org.hibernate.orm.connections.pooling] (default task-2) HHH10001001: Connection properties: {user=tomcat, password=****}
10:22:15,613 INFO [org.hibernate.orm.connections.pooling] (default task-2) HHH10001003: Autocommit mode: false
10:22:15,616 INFO [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (default task-2) HHH000115: Hibernate connection pool size: 10 (min=1)
10:22:15,673 INFO [org.hibernate.dialect.Dialect] (default task-2) HHH000400: Using dialect: org.hibernate.dialect.MySQLInnoDBDialect
10:22:15,988 INFO [org.hibernate.validator.internal.util.Version] (default task-2) HV000001: Hibernate Validator 5.2.4.Final
10:22:16,099 INFO [stdout] (default task-2) listEmployees method ...
10:22:16,239 WARN [org.hibernate.hql.internal.QuerySplitter] (default task-2) HHH000183: no persistent classes found for query class: from Employee e
10:22:16,241 INFO [org.hibernate.hql.internal.QueryTranslatorFactoryInitiator] (default task-2) HHH000397: Using ASTQueryTranslatorFactory
10:22:16,249 INFO [stdout] (default task-2) employees ... [] |
J'ai donc une servlet :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class test
*/
@WebServlet("/test")
public class test extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("do get method");
ManageEmployee mgnt = new ManageEmployee();
mgnt.listEmployees();
response.getWriter().append("Served at: ").append(request.getContextPath());
}
} |
Et une méthode censé allez me chercher mes employés en base :
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
import java.util.List;
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class ManageEmployee {
private static SessionFactory sessionFactory;
public ManageEmployee() {
try {
System.out.println("Init");
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public void listEmployees() {
System.out.println("listEmployees method ...");
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
List employees = session.createQuery("from Employee e").list();
System.out.println("employees ... " + employees.toString());
for (Iterator iterator = employees.iterator(); iterator.hasNext();) {
Employee employee = (Employee) iterator.next();
System.out.print("First Name: " + employee.getFirstName());
System.out.print(" Last Name: " + employee.getLastName());
System.out.println(" Salary: " + employee.getSalary());
}
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
} |
un fichier de configuration hibernate :
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
| <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- local connection properties -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/MaDatabase</property>
<property name="hibernate.connection.driver_class">org.mariadb.jdbc.Driver</property>
<property name="hibernate.connection.username">local</property>
<property name="hibernate.connection.password">p@ssword</property>
<property name="hibernate.connection.pool_size">10</property>
<!-- dialect for MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="cache.use_query_cache">false</property>
<property name="show_sql">true</property>
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration> |
et un fichier de mapping :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping> |
Si vous saviez me guider ... car là je suis un peu sec ... j'essaye des choses mais sans résultat.
Merci.
Syberi@