Newsletter

Simple Hibernate Application Requirements

Hibernate » on May 13, 2011 { 20 Comments } By Sivateja

Any hibernate application, for example consider even first hello world program must always contains 4 files totally.

  • POJO class
  • Mapping XML
  • Configuration XML
  • One java file to write our logic

Actually these are the minimum requirement to run any hibernate application, and in fact we may require any number of POJO classes and any number of mapping xml files (Number of POJO classes = that many number of mapping xmls), and only one configuration xml and finally one java file to write our logic.

POJO Class:

  • POJO is a simple java file, no need to extend any class or implement any interface.
  • This POJO class contain private properties variables, and for each property a setter and a getter

Example:

public class  Java4s
{
private int stNo;
private String stName;
private String stAddress;

     public void setStno(int stNo)
     {
     this.stNo=stNo;
     }
     public int getStNo()
     {
     return stNo;
     }

     public void setStName(int stName)
     {
     this.stName=stName;
     }
     public String getStName()
     {
     return stName;
     }

     public void setStAddress(String stAddress)
     {
     this.stAddress=stAddress;
     }
     public String getStAddress()
     {
     return stAddress;
     }

}

 

Mapping xml For POJO:

Here is the mapping file related to above pojo class, if you have any doubts on the syntax of the Mapping xml file, you can check our previous session

<hibernate-mapping>
   <class name="Java4s" table="STable">
         <id name="stNo" column="SNo">
              <generator class="assigned"/>
         </id>
         <property name="stName" column="SName" />
         <property name="stAddress "/> </class>
</hibernate-mapping>

Yes., see in this above mapping xml, for stAddress property i have not written any column name i just been specified  <property name=”stAddress “/>, this means in the database the column name for stAddress property will also be stAddress, in these cases we can ignore the column attribute to write, and i will explain about this <generator /> element later.

 

Configuration XML

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory“>

<!-- Related to the connection START -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property>
<property name="connection.user">user</property>
<property name="connection.password">password</property>
<!-- Related to the connection END -->

<!-- Related to hibernate properties START -->
<property name="show_sql">true</property>
<property name="dialet">org.hibernate.dialect.OracleDialect</property>
<property name="hbm2ddl.auto">update</property>
<!-- Related to hibernate properties END -->

<!-- Related to mapping START -->
<mapping resource="Our mapping xml file name" />
<!-- Related to the mapping END -->

</session-factory>
</hibernate-configuration>

Usually configuration file name will be hibernate.cfg.xml

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

20 Responses to “Simple Hibernate Application Requirements”
  1. Pavan says:

    Sir,can u explain about the POJO class i.e., why should the properties be private always and why getters and setters public …have some confusion about it…

  2. Java4s says:

    @Pavan

    Hi pavan, its really good question.

    We used to take instance variables as private, and methods as public because of security.

    If you would like to ask these type of questions, please post the questions at ‘http://answers.java4s.com/‘ so that we will be able to answer you with some simple examples if possible.

  3. chakradhar says:

    Good Work Svateja….But we need some more related to real time examples…i suggest you to take some examples what we are commonly using in Max of the real time applications.Post that type of real time examples in your blog.That will helpful for you and every one.

    Thanks&Regards
    Chakradhar

  4. ravi says:

    how spring reduces boilerplate code ?

  5. Rajasekhar says:

    Hi Siva Teja,

    As you said that we can implement / extend any class or interface in pojo/entity class but in newer version of hibernate why we implement the serialize interface in every hibernate entity class , is there any particular reason behind this..? are there any dis advantages if don’t implement serialize interface?
    If you answer for this question it’s helpful to me.
    thank you very mnuch

  6. Arun Singh says:

    thnks sir

  7. Annonymous says:

    Hi Rajsekhar.. I think we implement serializable interface with some specific entity/pojo class while we have a requirement of composite primary key in our project ,at that time only we serialize our entiry class and provide a tag in our mapping file either, But i’ve no exact idea that, why there is a requirement of serialization 😛

  8. k.ravindhar says:

    hai rajashekar,

    we are implementing interface serialization why because in hibernate directly hibernate column values stored directly.cannot convert into text like jdbc.serialization means stream format it can any format

  9. Beginner says:

    Is internet connection must for running Hibernate application using Eclipse?

  10. yamini says:

    it is very useful thank you so much but give brief explanation

  11. balamurugan says:

    Simply explained. I Like it

  12. Rajesh says:

    Hi teja,
    can you please explain me, how can we provide security by using encapsulation using private variables

  13. Susmitha says:

    If i use mysql Datebase what are the changes required in the configuration file

  14. saroj kumar says:

    what is pojo class
    ??

  15. GopiKolli says:

    Sir,can u explain about the POJO class i.e., why should the properties be private always and why getters and setters public …have some confusion about it

  16. Kalidas says:

    Hi sir,
    I have found 1 mistake in POJA class.
    You are passing int type variable in setStName(int stName).

    public void setStName(int stName)
    {
    this.stName=stName;
    }

  17. Kompalli Siva says:

    Not working. I used <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
    I got no suitable driver found error. Explain deeply please.

  18. nithish says:

    I think there is an error in the POJO class in 16th line
    your code:-public void setStName(int stName)

    according to my guess, it should be:- public void setStName(String stName)

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.