Monday, 16 July 2012

Hibernate With Annotations

  • Data about data is called Meta Data
  • Configuring resources to pass their details to underlying software where the resources will be utilized for execution also comes under meta data operations

  • In earlier days programmers have taken the support of XML files for resources configurations and for meta data operations
  • In recent days we can also use the java statements called annotations as alternative for XML files based application.
  • XML files gives flexibility towards modifications but it does not give good performance because reading data from XML files is always quite complex operations
  • Annotations gives good performance but doesn't provide flexibility towards modifications
  • If Annotations & XML files both are configured for meta data operations then the setting done in XML file will be effected.
  • We can apply annotations at three levels
    • Resource Level
    • Field Level
    • Method Level   
  • We will see Basic application for Hibernate Annotations (Resources for this)
    • Table in Database
    • POJO class
    • hibernate configurations file
    • client applications
  • Table in database
    • create table std (stdno number(3),stdname varchar(10),stdadd varchar(10));

  • std.java(POJO class with Annatations)
import java.io.Serializable;
    import javax.persistence.Column;
      import javax.persistence.Entity;
        import javax.persistence.Id;
          import javax.persistence.Table;

          @Entity
            @Table(name = "std")
            /* Here Entity describe for class to table relations*/
              public class Std implements Serializable {/*@Id is id Generator  for coloumn*/
                @Id
                   @Column(name = "stdno")
                    private int stdno;
                               /* column mapping*/
                      @Column(name = "stdname")
                        private String stdname;
                                /* column mapping*/ 
                          @Column(name="stdadd")
                            private String stdadd;
                              public int getStdno() {
                                return stdno;
                                  }
                                    public void setStdno(int stdno) {
                                      this.stdno = stdno;
                                        }
                                          public String getStdname() {
                                            return stdname;
                                              }
                                                public void setStdname(String stdname) {
                                                  this.stdname = stdname;
                                                    }
                                                      public String getStdadd() {
                                                        return stdadd;
                                                          }
                                                            public void setStdadd(String stdadd) {
                                                              this.stdadd = stdadd;
                                                                }
                                                                }

                                                                hibernate.cfg.xml

                                                                <?xml version='1.0' encoding='utf-8'?>
                                                                <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
                                                                <hibernate-configuration>
                                                                <session-factory>
                                                                <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                                                                <property name="hibernate.connection.url">jdbc:mysql://localhost/abc</property>
                                                                <property name="hibernate.connection.username">root</property>
                                                                <property name="hibernate.connection.password"></property>
                                                                <property name="hibernate.connection.dialect">org.hibernate.dialect.MySQLDialect</property>
                                                                <mapping class="Std"/>
                                                                </session-factory>
                                                                </hibernate-configuration>

                                                                client.java


                                                                import org.hibernate.Session;
                                                                import org.hibernate.SessionFactory;
                                                                import org.hibernate.Transaction;
                                                                import org.hibernate.cfg.Configuration;
                                                                public class Client {
                                                                public static void main(String[] args) {
                                                                // TODO Auto-generated method stub
                                                                Configuration cfg=new Configuration();
                                                                cfg.configure();
                                                                SessionFactory sf=cfg.buildSessionFactory();
                                                                Session s=sf.openSession();
                                                                Std obj=new Std();
                                                                obj.setStdno(10);
                                                                obj.setStdname("abc");
                                                                obj.setStdadd("abcom");
                                                                s.save(obj);
                                                                Transaction t=s.beginTransaction();
                                                                t.commit();
                                                                }
                                                                }

                                                                For Downloading above project with library files



                                                                No comments:

                                                                Post a Comment