what to use after @entity is deprecated in hibernate

Hide - Examples


Let usa now take an example to understand how we can use Hide to provide Java persistence in a standalone application. We volition go through the different steps involved in creating a Java Application using Hide technology.

Create POJO Classes

The first footstep in creating an application is to build the Java POJO class or classes, depending on the awarding that will be persisted to the database. Let us consider our Employee grade with getXXX and setXXX methods to make information technology JavaBeans compliant class.

A POJO (Manifestly Onetime Java Object) is a Java object that doesn't extend or implement some specialized classes and interfaces respectively required past the EJB framework. All normal Java objects are POJO.

When y'all blueprint a form to be persisted by Hibernate, information technology is important to provide JavaBeans compliant code too equally one attribute, which would work every bit index like id aspect in the Employee class.

public course Employee {    private int id;    private String firstName;     private Cord lastName;       private int salary;       public Employee() {}    public Employee(String fname, String lname, int salary) {       this.firstName = fname;       this.lastName = lname;       this.salary = salary;    }        public int getId() {       return id;    }        public void setId( int id ) {       this.id = id;    }        public String getFirstName() {       render firstName;    }        public void setFirstName( Cord first_name ) {       this.firstName = first_name;    }        public String getLastName() {       return lastName;    }        public void setLastName( Cord last_name ) {       this.lastName = last_name;    }        public int getSalary() {       return salary;    }        public void setSalary( int salary ) {       this.salary = salary;    } }        

Create Database Tables

Second step would exist creating tables in your database. In that location would be one tabular array corresponding to each object, you are willing to provide persistence. Consider above objects need to be stored and retrieved into the following RDBMS tabular array −

create table EMPLOYEE (    id INT NOT Zip auto_increment,    first_name VARCHAR(20) default NULL,    last_name  VARCHAR(20) default NULL,    salary     INT  default NULL,    PRIMARY KEY (id) );        

Create Mapping Configuration File

This step is to create a mapping file that instructs Hibernate how to map the divers class or classes to the database tables.

<?xml version = "i.0" encoding = "utf-eight"?> <!DOCTYPE hibernate-mapping PUBLIC  "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hide.org/dtd/hibernate-mapping-3.0.dtd">   <hibernate-mapping>    <class name = "Employee" tabular array = "EMPLOYEE">              <meta aspect = "class-description">          This grade contains the employee detail.        </meta>              <id proper noun = "id" type = "int" column = "id">          <generator class="native"/>       </id>              <belongings name = "firstName" column = "first_name" type = "string"/>       <property name = "lastName" column = "last_name" type = "string"/>       <property name = "salary" column = "bacon" type = "int"/>           </class> </hibernate-mapping>        

You should relieve the mapping document in a file with the format <classname>.hbm.xml. We saved our mapping document in the file Employee.hbm.xml. Let united states of america come across little particular about the mapping document −

  • The mapping document is an XML document having <hibernate-mapping> as the root element which contains all the <class> elements.

  • The <class> elements are used to ascertain specific mappings from a Java classes to the database tables. The Coffee class name is specified using the name attribute of the course chemical element and the database table proper noun is specified using the table attribute.

  • The <meta> element is optional element and can be used to create the class description.

  • The <id> element maps the unique ID aspect in form to the primary key of the database table. The proper noun attribute of the id element refers to the holding in the class and the column attribute refers to the column in the database table. The type aspect holds the hibernate mapping blazon, this mapping types will catechumen from Java to SQL data blazon.

  • The <generator> element within the id chemical element is used to generate the primary key values automatically. The class attribute of the generator element is set to native to permit hide pick up either identity, sequence or hilo algorithm to create main cardinal depending upon the capabilities of the underlying database.

  • The <property> chemical element is used to map a Java form belongings to a column in the database tabular array. The name aspect of the element refers to the property in the class and the column attribute refers to the column in the database tabular array. The blazon attribute holds the hibernate mapping blazon, this mapping types will convert from Coffee to SQL data type.

At that place are other attributes and elements available, which will be used in a mapping certificate and I would try to cover as many every bit possible while discussing other Hibernate related topics.

Create Application Class

Finally, nosotros volition create our application class with the master() method to run the awarding. Nosotros will use this application to save few Employee'south records and so we volition apply Grime operations on those records.

import java.util.List;  import java.util.Date; import java.util.Iterator;    import org.hibernate.HibernateException;  import org.hibernate.Session;  import org.hibernate.Transaction; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;  public class ManageEmployee {    private static SessionFactory mill;     public static void main(String[] args) {              try {          factory = new Configuration().configure().buildSessionFactory();       } grab (Throwable ex) {           System.err.println("Failed to create sessionFactory object." + ex);          throw new ExceptionInInitializerError(ex);        }              ManageEmployee ME = new ManageEmployee();        /* Add together few employee records in database */       Integer empID1 = ME.addEmployee("Zara", "Ali", g);       Integer empID2 = ME.addEmployee("Daisy", "Das", 5000);       Integer empID3 = ME.addEmployee("John", "Paul", 10000);        /* List down all the employees */       ME.listEmployees();        /* Update employee's records */       ME.updateEmployee(empID1, 5000);        /* Delete an employee from the database */       ME.deleteEmployee(empID2);        /* List downwards new list of the employees */       ME.listEmployees();    }        /* Method to CREATE an employee in the database */    public Integer addEmployee(String fname, Cord lname, int salary){       Session session = factory.openSession();       Transaction tx = null;       Integer employeeID = cypher;              try {          tx = session.beginTransaction();          Employee employee = new Employee(fname, lname, salary);          employeeID = (Integer) session.save(employee);           tx.commit();       } catch (HibernateException e) {          if (tx!=nil) tx.rollback();          east.printStackTrace();        } finally {          session.shut();        }       return employeeID;    }        /* Method to  READ all the employees */    public void listEmployees( ){       Session session = mill.openSession();       Transaction tx = naught;              try {          tx = session.beginTransaction();          List employees = session.createQuery("FROM Employee").list();           for (Iterator iterator = employees.iterator(); iterator.hasNext();){             Employee employee = (Employee) iterator.next();              Organization.out.print("Commencement Proper name: " + employee.getFirstName());              System.out.impress("  Terminal Name: " + employee.getLastName());              System.out.println("  Bacon: " + employee.getSalary());           }          tx.commit();       } catch (HibernateException e) {          if (tx!=null) tx.rollback();          east.printStackTrace();        } finally {          session.close();        }    }        /* Method to UPDATE bacon for an employee */    public void updateEmployee(Integer EmployeeID, int bacon ){       Session session = manufacturing plant.openSession();       Transaction tx = nix;              try {          tx = session.beginTransaction();          Employee employee = (Employee)session.get(Employee.class, EmployeeID);           employee.setSalary( salary ); 		 session.update(employee);           tx.commit();       } take hold of (HibernateException eastward) {          if (tx!=naught) tx.rollback();          e.printStackTrace();        } finally {          session.close();        }    }        /* Method to DELETE an employee from the records */    public void deleteEmployee(Integer EmployeeID){       Session session = manufactory.openSession();       Transaction tx = null;              effort {          tx = session.beginTransaction();          Employee employee = (Employee)session.go(Employee.class, EmployeeID);           session.delete(employee);           tx.commit();       } catch (HibernateException e) {          if (tx!=zero) tx.rollback();          e.printStackTrace();        } finally {          session.close();        }    } }        

Compilation and Execution

Here are the steps to compile and run the above mentioned application. Make sure, you lot accept set PATH and CLASSPATH accordingly before proceeding for the compilation and execution.

  • Create hibernate.cfg.xml configuration file as explained in configuration chapter.

  • Create Employee.hbm.xml mapping file as shown above.

  • Create Employee.java source file as shown to a higher place and compile it.

  • Create ManageEmployee.java source file as shown above and compile it.

  • Execute ManageEmployee binary to run the program.

You would become the following outcome, and records would be created in the EMPLOYEE table.

$java ManageEmployee .......Various LOG MESSAGES Volition DISPLAY Hither........  Outset Name: Zara  Last Name: Ali  Bacon: thou First Name: Daisy  Last Name: Das  Salary: 5000 First Proper noun: John  Last Name: Paul  Salary: 10000 First Proper noun: Zara  Last Name: Ali  Salary: 5000 Commencement Name: John  Terminal Name: Paul  Salary: 10000        

If you check your EMPLOYEE table, information technology should have the following records −

mysql> select * from EMPLOYEE; +----+------------+-----------+--------+ | id | first_name | last_name | bacon | +----+------------+-----------+--------+ | 29 | Zara       | Ali       |   5000 | | 31 | John       | Paul      |  10000 | +----+------------+-----------+--------+ 2 rows in set (0.00 sec  mysql>        

Useful Video Courses


JSP, Servlet, JSLT + Hibernate: A complete guide

Video

Learn Hibernate Java Framework the Easy way!

Video

Hibernate in Practice - The Complete Course

Video

honigbrong1975.blogspot.com

Source: https://www.tutorialspoint.com/hibernate/hibernate_examples.htm

0 Response to "what to use after @entity is deprecated in hibernate"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel