Hibernate: A simple example #48

Hibernate: A simple example

  1. First, you need to create an xml file to specify the database parameters that you are going to use. Database parameters may include: the database driver, user name and password to access the database.
  2. Then you should create a persistent class to represent the database table. One member variable for each table column.Also, you are required to write get and set methods for all of the member variable.
  3. Then you need to write another XML file to map table columns to class variables.
  4. Finally, you need to write the code to make use of the persistent class to store and/or retrieve data from the table.

Examples

  1. XML file (hibernate.cfg.xml) to specify the database parameters:

    com.mysql.jdbc.Driver
    jdbc:mysql://localhost/hibernatetutorial
    root

    10
    true
    org.hibernate.dialect.MySQLDialect
    update

  2. I think you know how to write a class with get and set methods
  3. XML file to map table columns to class variables.

  4. SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

    session =sessionFactory.openSession();

    //Create new instance of Contact and set values in it by reading them from form object, Contact is the persistent class here

    System.out.println("Inserting Record");
    Contact contact = new Contact();
    contact.setFirstName("XYZ");
    session.flush();
    session.close();

From: http://sitestree.com/?p=4827
Categories:48
Tags:
Post Data:2011-09-19 21:45:24

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>

Leave a Reply