Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1.  
  2. import java.util.List;
  3. import org.hibernate.Hibernate;
  4. import org.hibernate.Session;
  5. import org.hibernate.*;
  6. import org.hibernate.cfg.*;
  7.  
  8. //import org.hibernate.classic.Session;
  9. import org.hibernate.SessionFactory;
  10. import org.hibernate.cfg.Configuration;
  11. import java.util.*;
  12. //import com.someorg.persist.Order;
  13. // use as
  14. // java test. FindOrderById name
  15.  
  16. public class FindOrderById {
  17.  
  18.  
  19.  
  20. public static void main(String[] args) throws Exception {
  21.  
  22. String id1 ="id1";
  23. // query to issue
  24. /* String query =
  25. "select order from Order "
  26. + "where order.id=: id1";
  27. */
  28. // search for what?
  29. String name = args[0];
  30.  
  31. // init
  32. Configuration cfg = new Configuration()
  33. .addClass(Order.class);
  34. cfg.setProperty("connection.driver_class","com.mysql.jdbc.Driver");
  35. cfg.setProperty("connection.url","jdbc:mysql://localhost:3306/hibernate");
  36. cfg.setProperty("connection.username","sandbox");
  37. cfg.setProperty("connection.password","sandbox");
  38. cfg.setProperty("connection.pool_size", 8 );
  39. cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect" );
  40.  
  41.  
  42. SessionFactory sf = cfg.buildSessionFactory();
  43. // sf.getDialect();
  44.  
  45. //org.hibernate.dialect.Dialect.setDialect("org.hibernate.dialect.MySQLDialect");
  46.  
  47. // open session
  48. Session sess = sf.openSession();
  49. Transaction tx = sess.beginTransaction();
  50. Query query = sess.createQuery("select o from Order as o where o.id = :id1");
  51. query.setString("id1", "id1");
  52. for (Iterator it = query.iterate(); it.hasNext();) {
  53. Order order = (Order) it.next();
  54. System.out.println("Order Price : " + order.getPriceTotal() );
  55. }
  56. tx.commit();
  57.  
  58. // search and return
  59. sess.close();
  60. // System.out.println("Found Order: " + o);
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement