Guest User

Untitled

a guest
May 2nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. import javax.persistence.Entity;
  2. import javax.persistence.GeneratedValue;
  3. import javax.persistence.GenerationType;
  4. import javax.persistence.Id;
  5. import javax.persistence.Table;
  6.  
  7. @Entity
  8. @Table(name="ORDER")
  9. public class Order {
  10.  
  11. @Id
  12. @GeneratedValue(strategy = GenerationType.IDENTITY)
  13. @Column(name="Order_no")
  14. private int _OrderNo;
  15.  
  16.  
  17.  
  18. @Column(name="Customer_no")
  19. private int _CustomerNo;
  20.  
  21. public Order() {
  22.  
  23. }
  24.  
  25. public Order(int CustomerNo) {
  26.  
  27.  
  28. this._CustomerNo = CustomerNo;
  29. }
  30.  
  31.  
  32. //getters and setters logic goes here
  33. public int get_OrderNo() {
  34. return _OrderNo;
  35. }
  36.  
  37. public void set_OrderNo(int OrderNo) {
  38. this._OrderNo = OrderNo;
  39. }
  40.  
  41.  
  42. public int get_CustomerNo() {
  43. return _CustomerNo;
  44. }
  45.  
  46. public void set_CustomerNo(int CustomerNo) {
  47. this._CustomerNo = CustomerNo;
  48. }
  49.  
  50. @Override
  51. public String toString() {
  52. return "Order [_OrderNo=" + _OrderNo + ", _CustomerNo=" + _CustomerNo + "]";
  53. }
  54.  
  55. }
  56.  
  57. public class CreateOrder {
  58.  
  59. public static void main(String[] args) {
  60. // Create SessionFactory
  61.  
  62. SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Order.class)
  63. .buildSessionFactory();
  64.  
  65. // Create Session
  66.  
  67. Session session = factory.getCurrentSession();
  68.  
  69. try {
  70. // use the session object to save the java object
  71.  
  72. // create the order object
  73. System.out.println("Creating the Order Object.......");
  74.  
  75. Order tempOrder = new Order(123);
  76.  
  77. // start the transaction
  78. session.beginTransaction();
  79.  
  80. // save the order object
  81. System.out.println("Saving the Order ");
  82. session.save(tempOrder);
  83.  
  84. // commit the transaction
  85. session.getTransaction().commit();
  86. System.out.println("Done!!");
  87. }
  88.  
  89. finally {
  90. factory.close();
  91. }
  92. }
  93.  
  94. }
  95.  
  96. <!DOCTYPE hibernate-configuration PUBLIC
  97. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  98. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  99.  
  100. <hibernate-configuration>
  101.  
  102. <session-factory>
  103.  
  104. <!-- JDBC Database connection settings -->
  105. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  106. <property name="connection.url">jdbc:mysql://localhost:3306/hb_student_retail?useSSL=false</property>
  107. <property name="connection.username">hbstudent</property>
  108. <property name="connection.password">hbstudent</property>
  109.  
  110. <!-- JDBC connection pool settings ... using built-in test pool -->
  111. <property name="connection.pool_size">1</property>
  112.  
  113. <!-- Select our SQL dialect -->
  114. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  115.  
  116. <!-- Echo the SQL to stdout -->
  117. <property name="show_sql">true</property>
  118.  
  119. <!-- Set the current session context -->
  120. <property name="current_session_context_class">thread</property>
  121.  
  122. </session-factory>
  123.  
  124. </hibernate-configuration>
Add Comment
Please, Sign In to add comment