Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. public static List<Product> productsList() {
  2. //singleton factory object
  3. SessionsGenerator FactoryObject = new SessionsGenerator();
  4. Session session = SessionsGenerator.getFactory().openSession();
  5. List<Product> list = new ArrayList<>();
  6. try {
  7. list = session.createQuery("from Product where deleted= false").list();
  8. System.out.println("-------------- list product: "+list); // testing from console here I get the same results on the user interface, so it can't be a Javafx problem.
  9. } finally {
  10. session.close();
  11. }
  12. return list;
  13. }
  14.  
  15. public static boolean SaveOrUpdate(Product product) {
  16. SessionsGenerator FactoryObject = new SessionsGenerator();
  17. Session session = SessionsGenerator.getFactory().openSession();
  18. try {
  19. session.beginTransaction();
  20. session.saveOrUpdate(product);
  21. session.getTransaction().commit();
  22. } catch (Exception e) {
  23. return false;
  24. } finally {
  25. session.close();
  26. return true;
  27. }
  28. }
  29.  
  30. @Entity
  31. @Table(name = "Product")
  32. public class Product{
  33. @Id
  34. @GeneratedValue(strategy = GenerationType.AUTO)
  35. @Column(name = "id", nullable = false)
  36. int id;
  37. @Column(name = "code", nullable = false)
  38. String code;
  39. @Column(name = "matricule", nullable = false)
  40. String matricule;
  41. @Column(name = "marque", nullable = false)
  42. String marque;
  43. @Column(name = "type", nullable = false)
  44. String type;
  45. @OneToMany(targetEntity = Facture.class, mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  46. private List<Facture> factures;
  47. @OneToMany(targetEntity = Achat.class, mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  48. private List<Achat> achats;
  49. @Column(name = "deleted", nullable = false)
  50. boolean deleted;
  51.  
  52. public Product() {
  53. }
  54.  
  55. public Product(String code, String matricule, String marque,String type) {
  56. this.code = code;
  57. this.matricule = matricule;
  58. this.marque = marque;
  59. this.type = type;
  60. this.deleted = false;
  61. }
  62. //setters and getters
  63.  
  64. <?xml version="1.0" encoding="UTF-8"?>
  65. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  66. <hibernate-configuration>
  67. <session-factory>
  68. <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
  69. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  70. <property name="hibernate.connection.username">root</property>
  71. <property name="hibernate.connection.password">root</property>
  72. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/gestioncommerciale</property>
  73. <property name="connection_pool_size">1</property>
  74. <property name="hbm2ddl.auto">update</property>
  75. <property name="show_sql">true</property>
  76. <property name="hibernate.current_session_context_class">thread</property>
  77. </session-factory>
  78. </hibernate-configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement