Tutorial hibernate:
Voici le cdoe de la classe de l'entité Client.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package entities; | |
// Generated 19 févr. 2015 17:55:19 by Hibernate Tools 3.4.0.CR1 | |
/** | |
* Client generated by hbm2java | |
*/ | |
public class Client implements java.io.Serializable { | |
@Override | |
public String toString() { | |
return "Client [idclient=" + idclient + ", nom=" + nom + ", prenom=" | |
+ prenom + "]"; | |
} | |
private int idclient; | |
private String nom; | |
private String prenom; | |
public Client() { | |
} | |
public Client(int idclient) { | |
this.idclient = idclient; | |
} | |
public Client(int idclient, String nom, String prenom) { | |
this.idclient = idclient; | |
this.nom = nom; | |
this.prenom = prenom; | |
} | |
public int getIdclient() { | |
return this.idclient; | |
} | |
public void setIdclient(int idclient) { | |
this.idclient = idclient; | |
} | |
public String getNom() { | |
return this.nom; | |
} | |
public void setNom(String nom) { | |
this.nom = nom; | |
} | |
public String getPrenom() { | |
return this.prenom; | |
} | |
public void setPrenom(String prenom) { | |
this.prenom = prenom; | |
} | |
} | |
Ceci est le fichier de mapping Client.hbm.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" | |
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> | |
<!-- Generated 19 f?vr. 2015 17:55:19 by Hibernate Tools 3.4.0.CR1 --> | |
<hibernate-mapping> | |
<class name="entities.Client" table="client" catalog="mabase"> | |
<id name="idclient" type="int"> | |
<column name="idclient" /> | |
<generator class="assigned" /> | |
</id> | |
<property name="nom" type="string"> | |
<column name="nom" /> | |
</property> | |
<property name="prenom" type="string"> | |
<column name="prenom" /> | |
</property> | |
</class> | |
</hibernate-mapping> | |
Le code de fichier de configuration hibernate-cfg.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!DOCTYPE hibernate-configuration PUBLIC | |
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" | |
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> | |
<hibernate-configuration> | |
<session-factory> | |
<property name="hibernate.bytecode.use_reflection_optimizer">false</property> | |
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> | |
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mabase</property> | |
<property name="hibernate.connection.username">root</property> | |
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> | |
<property name="hibernate.search.autoregister_listeners">false</property> | |
<mapping resource="entities/Client.hbm.xml" /> | |
</session-factory> | |
</hibernate-configuration> |
La classe HibernateUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import org.hibernate.*; | |
import org.hibernate.cfg.*; | |
/** | |
* Startup Hibernate and provide access to the singleton SessionFactory | |
*/ | |
public class HibernateUtil { | |
private static SessionFactory sessionFactory; | |
static { | |
try { | |
sessionFactory = new Configuration().configure().buildSessionFactory(); | |
} catch (Throwable ex) { | |
throw new ExceptionInInitializerError(ex); | |
} | |
} | |
public static SessionFactory getSessionFactory() { | |
// Alternatively, we could look up in JNDI here | |
return sessionFactory; | |
} | |
public static void shutdown() { | |
// Close caches and connection pools | |
getSessionFactory().close(); | |
} | |
} |
L'interface IClientDao,contenant les methodes qu'on veut implémenter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.util.List; | |
import entities.Client; | |
public interface IClientDao { | |
public void ajouterClient(Client client); | |
public List<Client> tousClients(); | |
public Client GetClientById(int id); | |
public void supprimerClient(int id); | |
public void modifierClient(int id); | |
public List<Client> rechercheByCriteria(String nom); | |
} |
Et voici la classe ClientDaoImpl.java qui implemente l'interface IClientDao
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import java.util.List; | |
import org.hibernate.Criteria; | |
import org.hibernate.Session; | |
import org.hibernate.Transaction; | |
import org.hibernate.criterion.Criterion; | |
import org.hibernate.criterion.Restrictions; | |
import entities.Client; | |
public class ClientDaoImpl implements IClientDao { | |
@Override | |
public void ajouterClient(Client client) { | |
// TODO Auto-generated method stub | |
Session session=HibernateUtil.getSessionFactory().openSession(); | |
Transaction tx=session.beginTransaction(); | |
session.save(client); | |
tx.commit(); | |
session.close(); | |
} | |
@Override | |
public List<Client> tousClients() { | |
// TODO Auto-generated method stub | |
Session session=HibernateUtil.getSessionFactory().openSession(); | |
List<Client> listclient=session.createQuery("from Client").list(); | |
session.close(); | |
return listclient; | |
} | |
@Override | |
public Client GetClientById(int id) { | |
// TODO Auto-generated method stub | |
Session session=HibernateUtil.getSessionFactory().openSession(); | |
Client client=(Client) session.get(Client.class, id); | |
session.close(); | |
return client; | |
} | |
@Override | |
public void supprimerClient(int id) { | |
// TODO Auto-generated method stub | |
Session session=HibernateUtil.getSessionFactory().openSession(); | |
Transaction tx=session.beginTransaction(); | |
Client client=(Client) session.get(Client.class, id); | |
session.delete(client); | |
tx.commit(); | |
session.close(); | |
} | |
@Override | |
public void modifierClient(int id) { | |
// TODO Auto-generated method stub | |
Session session=HibernateUtil.getSessionFactory().openSession(); | |
Transaction tx=session.beginTransaction(); | |
Client client=(Client) session.get(Client.class, id); | |
client.setNom("newname"); | |
session.update(client); | |
tx.commit(); | |
session.close(); | |
} | |
@Override | |
public List<Client> rechercheByCriteria(String nom) { | |
// TODO Auto-generated method stub | |
Session session=HibernateUtil.getSessionFactory().openSession(); | |
Criteria criteria=session.createCriteria(Client.class); | |
Criterion criterion=Restrictions.eq("nom", nom); | |
criteria.add(criterion); | |
List<Client> listclient=criteria.list(); | |
session.close(); | |
return listclient; | |
} | |
} |
code source complet GitHub
i have this excpetion
ReplyDeleteorg.hibernate.LazyInitializationException: could not initialize proxy - no Session