Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. package com.enigma.facebookorm;
  2.  
  3. import com.enigma.facebookorm.entities.Person;
  4.  
  5. import org.hibernate.Session;
  6. import org.hibernate.SessionFactory;
  7. import org.hibernate.Transaction;
  8. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  9. import org.hibernate.cfg.Configuration;
  10. import org.hibernate.query.Query;
  11. import org.hibernate.service.ServiceRegistry;
  12. import java.sql.Date;
  13. import java.util.List;
  14.  
  15. import javax.persistence.criteria.CriteriaBuilder;
  16. import javax.persistence.criteria.CriteriaQuery;
  17. import javax.persistence.criteria.Root;
  18.  
  19. /**
  20. * Hello world!
  21. *
  22. */
  23. public class App {
  24. private static SessionFactory factory;
  25.  
  26. public List<Person> findAll() {
  27. Session session = factory.openSession();
  28. CriteriaBuilder cb = session.getCriteriaBuilder();
  29. CriteriaQuery<Person> cr = cb.createQuery(Person.class);
  30. Root<Person> root = cr.from(Person.class);
  31. cr.select(root)
  32. .where (cb.like(root.<String>get("name"), "%dik%"));
  33.  
  34. Query<Person> query = session.createQuery(cr);
  35.  
  36. List<Person> persons = null;
  37.  
  38. try {
  39. Transaction tx = null;
  40. tx = session.beginTransaction();
  41. persons = session.createQuery("FROM Person", Person.class).list();
  42. return persons;
  43. } catch (Exception x) {
  44. x.printStackTrace();
  45. }
  46. return persons;
  47. }
  48.  
  49. public void create() {
  50. Session session = factory.openSession();
  51. Transaction tx = null;
  52. try {
  53. tx = session.beginTransaction();
  54. Person person = new Person(null, "dede", Date.valueOf("1998-06-06"));
  55. session.save(person);
  56. tx.commit();
  57.  
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. }
  62.  
  63. public void update() {
  64. Session session = factory.openSession();
  65. Transaction tx = null;
  66. try {
  67. tx = session.beginTransaction();
  68. Person person = session.get(Person.class, "ff808181706174eb01706174ee530000");
  69. person.setName("bopak");
  70. session.update(person);
  71. tx.commit();
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. }
  75. }
  76.  
  77. public void delete() {
  78. Session session = factory.openSession();
  79. Transaction tx = null;
  80. try {
  81. tx = session.beginTransaction();
  82. Person person = session.get(Person.class, "ff808181706192620170619265c80000");
  83. session.delete(person);
  84. tx.commit();
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. }
  89.  
  90. public static void main(String[] args) {
  91. try {
  92. Configuration configuration = new Configuration();
  93. configuration.configure();
  94. configuration.addAnnotatedClass(Person.class);
  95.  
  96. ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
  97. .applySettings(configuration.getProperties()).build();
  98. factory = configuration.buildSessionFactory(serviceRegistry);
  99.  
  100. } catch (Exception x) {
  101. System.err.println("Failed to create sessionFactory object." + x);
  102. x.printStackTrace();
  103.  
  104. }
  105. App app = new App();
  106. List<Person> persons = app.findAll();
  107. app.create();
  108. app.update();
  109. app.delete();
  110. for (Person person : app.findAll()) {
  111. System.out.println(person);
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement