Quantcast
Channel: Coffeewithcode.com
Viewing all articles
Browse latest Browse all 40

Hibernate Hello World Example With Annotation

$
0
0

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

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

  1. package beans;
  2.  
  3. import javax.persistence.Column;
  4. import javax.persistence.Entity;
  5. import javax.persistence.GeneratedValue;
  6. import javax.persistence.Id;
  7. import javax.persistence.Table;
  8.  
  9. @Entity
  10. @Table(name="student_master")
  11. public class Student  {
  12.  
  13. @Id
  14. @GeneratedValue
  15. @Column(name="stud_id")
  16. private long id;
  17.  
  18. @Column(name="name")
  19. private String name;
  20.  
  21. /**
  22.   * getters and setters
  23.   * @return
  24.   */
  25. public long getId() {
  26. return id;
  27. }
  28. public void setId(long id) {
  29. this.id = id;
  30. }
  31. public String getName() {
  32. return name;
  33. }
  34. public void setName(String name) {
  35. this.name = name;
  36. }
  37. }

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="show_sql">true</property>
  13. <property name="hibernate.hbm2ddl.auto">create</property>
  14. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  15.  
  16.   <mapping class="beans.Student"/>
  17. </session-factory>
  18. </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. }

Your Suggestions Are Always Welcomed.


Viewing all articles
Browse latest Browse all 40

Latest Images

Trending Articles



Latest Images