Quantcast
Viewing all articles
Browse latest Browse all 40

Hibernate Hello World Example With XML Mapping

This article is about hibernate practical example. Below is an example of hibernate with my sql using hibernate mapping files.

Here is the Student bean class with id and name properties.

  1. package beans;
  2.  
  3. public class Student  {
  4.  
  5. private long id;
  6. private String name;
  7.  
  8. /**
  9. * getters and setters
  10. * @return
  11. */
  12. public long getId() {
  13. return id;
  14. }
  15. public void setId(long id) {
  16. this.id = id;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. }

Here is the student.hbm.xml file.

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5.  
  6. <hibernate-mapping>
  7. <class name="beans.Student" table="student_master"  schema="test">
  8. <id name="id" type="long" column="id">
  9. <generator />
  10. </id>
  11. <property name="name">
  12. <column name="name" />
  13. </property>
  14. </class>
  15. </hibernate-mapping>

Here is the hibernate.cfg.xml file. For this example mysql used as the data base.

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5.  
  6. <hibernate-configuration>
  7. <session-factory>
  8. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
  10. <property name="hibernate.connection.username">root</property>
  11. <property name="hibernate.connection.password">root</property>
  12. <property name="hibernate.connection.pool_size">10</property>
  13. <property name="show_sql">true</property>
  14. <property name="hibernate.hbm2ddl.auto">update</property>
  15. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  16. <property name="hibernate.current_session_context_class">thread</property>
  17.  
  18. <mapping resource="mappings/student.hbm.xml"/>
  19. </session-factory>
  20. </hibernate-configuration>

Here is the Driver class. In the driver class we have created an instance of student and saved to the database.

  1. package driver;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.cfg.Configuration;
  6.  
  7. import beans.Student;
  8.  
  9. public class Driver {
  10. /**
  11.   * main method
  12.   */
  13. public static void main(String[] args) {
  14. Session session = null;
  15. try {
  16. //getting session factory instance
  17. SessionFactory sessionFactory = new Configuration().configure(
  18. "hibernate/hibernate.cfg.xml").buildSessionFactory();
  19. session = sessionFactory.openSession();
  20. session.beginTransaction();
  21. //student instance
  22. Student student = new Student();
  23. student.setName("Sanjay ");
  24. session.save(student);
  25. session.getTransaction().commit();
  26. } catch (Exception e) {
  27. System.out.println(e.getMessage());
  28. } finally {
  29. session.flush();
  30. session.close();
  31. }
  32. }
  33. }

Overall project structure.

Image may be NSFW.
Clik here to view.
Hibernate Project Structure

Hibernate Project Structure

Your Suggestions Are Always Welcomed.

Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 40

Trending Articles