Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. package com.formation.gestionprojet.doa.entity;
  2.  
  3. import java.io.Serializable;
  4.  
  5. import javax.persistence.Entity;
  6. import javax.persistence.Id;
  7. import javax.persistence.Table;
  8.  
  9.  
  10. @Entity
  11. @Table(name="Type")
  12. public class Type implements Serializable {
  13. /**
  14. *
  15. */
  16. private static final long serialVersionUID = 1L;
  17.  
  18. @Id
  19. private Long id;
  20.  
  21. private String name;
  22.  
  23. private String description;
  24.  
  25. private String active;
  26.  
  27.  
  28.  
  29.  
  30. public Type() {
  31. super();
  32. // TODO Auto-generated constructor stub
  33. }
  34.  
  35. public Long getId() {
  36. return id;
  37. }
  38.  
  39. public void setId(Long id) {
  40. this.id = id;
  41. }
  42.  
  43. public String getName() {
  44. return name;
  45. }
  46.  
  47. public void setName(String name) {
  48. this.name = name;
  49. }
  50.  
  51. public String getDescription() {
  52. return description;
  53. }
  54.  
  55. public void setDescription(String description) {
  56. this.description = description;
  57. }
  58.  
  59. public String getActive() {
  60. return active;
  61. }
  62.  
  63. public void setActive(String active) {
  64. this.active = active;
  65. }
  66.  
  67. }
  68.  
  69. <?xml version="1.0" encoding="UTF-8"?>
  70. <!DOCTYPE hibernate-configuration PUBLIC
  71. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  72. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  73.  
  74. <hibernate-configuration>
  75.  
  76.  
  77. <session-factory>
  78.  
  79. <!-- database connection setting -->
  80.  
  81. <property name ="connection.driver_class">com.mysql.jdbc.Driver</property>
  82. <property name="connection.url">jdbc:mysql://localhost:3306/gestion_projet?createDatabaseIfNotExist=true</property>
  83. <property name="connection.username">root</property>
  84. <property name= "connection.password">root</property>
  85.  
  86. <!-- Dialect -->
  87. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  88.  
  89. <!-- Disable the second level cache -->
  90.  
  91. <property name="cache.provider_class" >org.hibernate.cache.NoCacheProvider</property>
  92.  
  93. <!-- echo all executed SQL to stdout -->
  94.  
  95. <property name="show_sql">true</property>
  96.  
  97. <!-- Drope and re-create the database -->
  98. <property name="hbm2ddl.auto">update</property>
  99.  
  100. <!-- mapping -->
  101.  
  102.  
  103. <mapping class= "com.formation.gestionprojet.doa.entity.Type"/>
  104.  
  105.  
  106. </session-factory>
  107.  
  108. </hibernate-configuration>
  109.  
  110. package com.formation.gestionprojet.utils;
  111. import org.hibernate.HibernateException;
  112. import org.hibernate.Session;
  113. import org.hibernate.SessionFactory;
  114. import org.hibernate.cfg.Configuration;
  115. import org.hibernate.service.ServiceRegistry;
  116. import org.hibernate.service.ServiceRegistryBuilder;
  117.  
  118. @SuppressWarnings("deprecation")
  119. public class HibernateUtil
  120. {
  121. private static SessionFactory sessionFactory;
  122. private static ServiceRegistry serviceRegistry;
  123.  
  124. static
  125. {
  126. try
  127. {
  128.  
  129. Configuration configuration = new Configuration();
  130. configuration.configure("config/hibernate.cfg.xml");
  131.  
  132. serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
  133. sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  134. }
  135. catch (HibernateException ex)
  136. {
  137. System.err.println("Error creating Session: " + ex);
  138. throw new ExceptionInInitializerError(ex);
  139. }
  140. }
  141.  
  142. public static SessionFactory getSessionFactory()
  143. {
  144. return sessionFactory;
  145. }
  146.  
  147.  
  148.  
  149. public static Session openSession()
  150. {
  151. return sessionFactory.openSession();
  152. }
  153.  
  154.  
  155. public static Session getCurrentSession()
  156. {
  157. return sessionFactory.getCurrentSession();
  158. }
  159.  
  160.  
  161. public static void close(){
  162. if(sessionFactory!=null){
  163. sessionFactory.close();
  164. }
  165.  
  166. }
  167.  
  168.  
  169. }
  170.  
  171. package com.formation.gestionprojet.utils;
  172.  
  173. import org.hibernate.Session;
  174.  
  175.  
  176.  
  177. public class Test {
  178.  
  179. static Session session = HibernateUtil.openSession();
  180.  
  181. public static void main(String[] args) {
  182.  
  183. session.createQuery("select o from Type o").list();
  184.  
  185.  
  186.  
  187. }
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement