10. What is the need for
Hibernate xml mapping file?
Hibernate mapping file tells Hibernate which tables and columns to use to load and store objects.
<class name="com.test.User" table="user">
<property column="USER_NAME" length="255"
name="userName" not-null="true" type="java.lang.String"/>
<property column="USER_PASSWORD" length="255"
name="userPassword" not-null="true" type="java.lang.String"/>
</class>
</hibernate-mapping>
Hibernate mapping file tells Hibernate which tables and columns to use to load and store objects.
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml
version="1.0"?>
<!DOCTYPE
hibernate-mapping PUBLIC
<hibernate-mapping>
<class
name="sample.MyPersistanceClass"
table="MyPersitaceTable">
<id
name="id" column="MyPerId">
<generator
class="increment"/>
</id>
<property
name="text" column="Persistance_message"/>
<many-to-one
name="nxtPer" cascade="all"
column="NxtPerId"/>
</class>
</hibernate-mapping>
|
Everything
should be included undertag. This is the main tag for an xml mapping document.
11. What
the Core interfaces are of hibernate framework?
There are
many benefits from these. Out of which the following are the most important
one.
- Session Interface – This is the primary
interface used by hibernate applications. The instances of this interface
are lightweight and are inexpensive to create and destroy. Hibernate sessions
are not thread safe.
- SessionFactory Interface – This is a factory that
delivers the session objects to hibernate application. Generally there
will be a single SessionFactory for the whole application and it will be
shared among all the application threads.
- Configuration Interface – This interface is used to
configure and bootstrap hibernate. The instance of this interface is used
by the application in order to specify the location of hibernate specific
mapping documents.
- Transaction Interface – This is an optional interface but the above three interfaces are mandatory in each and every application. This interface abstracts the code from any kind of transaction implementations such as JDBC transaction, JTA transaction.
- Query and Criteria Interface – This interface allows the user to perform queries and also control the flow of the query execution.
12. What role does the
Session interface play in Hibernate?
The
Session interface is the primary interface used by Hibernate applications. It
is a single-threaded, short-lived object representing a conversation between
the application and the persistent store. It allows you to create query objects
to retrieve persistent objects.
Session session = sessionFactory.openSession();
Session session = sessionFactory.openSession();
Session
interface role:
- Wraps a JDBC connection
- Factory for Transaction
13. What is the general
flow of Hibernate communication with RDBMS?
The
general flow of Hibernate communication with RDBMS is :
- Load the Hibernate
configuration file and create configuration object. It will automatically
load all
Hibernate mapping files
- Create session factory from
configuration object
- Get one session from this
session factory
- Create HQL Query
- Execute query to get list containing Java objects
14. How do you map Java
Objects with Database tables?
- First we need to write Java
domain objects (beans with setter and getter).
- Write hbm.xml, where we map
java class to table and database columns to Java class variables.
Example :
<hibernate-mapping><class name="com.test.User" table="user">
<property column="USER_NAME" length="255"
name="userName" not-null="true" type="java.lang.String"/>
<property column="USER_PASSWORD" length="255"
name="userPassword" not-null="true" type="java.lang.String"/>
</class>
</hibernate-mapping>
0 comments:
Post a Comment