[Hibernate] Différentes stratégies pour faire une requête
Bonjour à tous !
Je commence a débuter en Hibernate et, pour commencer, j'ai utilisé cet exemple :
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
| package examples;
import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import roseindia.tutorial.hibernate.Insurance;
public class SelectHQLExample {
public static void main(String[] args) {
Session session = null;
try {
// This step will read hibernate.cfg.xml and prepare hibernate for
// use
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
session = sessionFactory.openSession();
// Using from Clause
String SQL_QUERY = "from Insurance insurance";
Query query = session.createQuery(SQL_QUERY);
for (Iterator it = query.iterate(); it.hasNext();) {
Insurance insurance = (Insurance) it.next();
System.out.println("ID: " + insurance.getLngInsuranceId());
System.out.println("First Name: "
+ insurance.getInsuranceName());
}
session.close();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
}
}
} |
Avec ces fichiers de conf:
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
| <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="roseindia.tutorial.hibernate.Insurance"
table="insurance">
<id name="lngInsuranceId" type="long" column="ID">
<generator class="increment" />
</id>
<property name="insuranceName">
<column name="insurance_name" />
</property>
<property name="investementAmount">
<column name="invested_amount" />
</property>
<property name="investementDate">
<column name="investement_date" />
</property>
</class>
</hibernate-mapping> |
Et le fichier hibernate.cfg.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test_hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">mysql5</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration> |
Ma fameuse question est :
Quelle est la différence entre cet exemple et un autre exemple qui utiliserait des requêtes SQL en utilisant un fichier de propriétés (qui remplacerait le fichier hibernate.cfg.xml) ?
Merci de m'éclaircir.