Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Table;
- @Entity
- @Table(name="ORDER")
- public class Order {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name="Order_no")
- private int _OrderNo;
- @Column(name="Customer_no")
- private int _CustomerNo;
- public Order() {
- }
- public Order(int CustomerNo) {
- this._CustomerNo = CustomerNo;
- }
- //getters and setters logic goes here
- public int get_OrderNo() {
- return _OrderNo;
- }
- public void set_OrderNo(int OrderNo) {
- this._OrderNo = OrderNo;
- }
- public int get_CustomerNo() {
- return _CustomerNo;
- }
- public void set_CustomerNo(int CustomerNo) {
- this._CustomerNo = CustomerNo;
- }
- @Override
- public String toString() {
- return "Order [_OrderNo=" + _OrderNo + ", _CustomerNo=" + _CustomerNo + "]";
- }
- }
- public class CreateOrder {
- public static void main(String[] args) {
- // Create SessionFactory
- SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Order.class)
- .buildSessionFactory();
- // Create Session
- Session session = factory.getCurrentSession();
- try {
- // use the session object to save the java object
- // create the order object
- System.out.println("Creating the Order Object.......");
- Order tempOrder = new Order(123);
- // start the transaction
- session.beginTransaction();
- // save the order object
- System.out.println("Saving the Order ");
- session.save(tempOrder);
- // commit the transaction
- session.getTransaction().commit();
- System.out.println("Done!!");
- }
- finally {
- factory.close();
- }
- }
- }
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <!-- JDBC Database connection settings -->
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://localhost:3306/hb_student_retail?useSSL=false</property>
- <property name="connection.username">hbstudent</property>
- <property name="connection.password">hbstudent</property>
- <!-- JDBC connection pool settings ... using built-in test pool -->
- <property name="connection.pool_size">1</property>
- <!-- Select our SQL dialect -->
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <!-- Echo the SQL to stdout -->
- <property name="show_sql">true</property>
- <!-- Set the current session context -->
- <property name="current_session_context_class">thread</property>
- </session-factory>
- </hibernate-configuration>
Add Comment
Please, Sign In to add comment