Newsletter |
Simple Hibernate Application Requirements
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 .:: | ||
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…
This is one the most important concept of OOP, know as "Encapsulation". 🙂
@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.
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
how spring reduces boilerplate code ?
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
thnks sir
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 😛
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
Is internet connection must for running Hibernate application using Eclipse?
it is very useful thank you so much but give brief explanation
Simply explained. I Like it
Hi teja,
can you please explain me, how can we provide security by using encapsulation using private variables
If i use mysql Datebase what are the changes required in the configuration file
what is pojo class
??
POJO is nothing but plain old java program which has global variables and getter and setters
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
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;
}
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.
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)