|
Hibernate with Annotation |
|
In the Dec 2007 issue of Java Jazz Up
magazine, we have discussed a lot of
details about the Hibernate. In the current issue
we are taking you further in the same direction
but this time we are talking about the
annotations with the hibernate.
Hibernate needs a metadata to govern the
transformation of data from POJO to database
tables and vice versa. Most commonly XML file
is used to write the metadata information in
Hibernate
Annotations : A Brief Overview
The Java 5 version has introduced a powerful
way to provide the metadata to the JVM. The
mechanism is known as Annotations.
Annotation is the java class which is read
through reflection mechanism during the
runtime by JVM and does the processing
accordingly. The Hibernate Annotations is the
powerful way to provide the metadata for the
Object and Relational Table mapping. All the
metadata is clubbed into the POJO java file along
with the code this helps the user to understand
the table structure and POJO simultaneously
during the development. This also reduces the
management of different files for the metadata
and java code.
Prerequisites for setting up a project :
- Make sure to have Java 5.0 or a higher
version.
- Hibernate Core 3.2.0GA and above.
- Download and add the Hibernate-
Annotations jar file in the project
workspace.
Prerequisites for setting up a project :
Let us assume we have a table Employee which
has only two columns i.e ID and Name. In
hibernate core to achieve the mapping for the
above employee table the user should create
the following files
- Utility file for configuring and building the
session factory.
- 2. Hibernate.cfg.xml or any other
Datasource metadata file
- Employee POJO object.
- Real application file which has the actual
logic manipulate the POJO
|
|
Note:- Annotations are only the step to remove
the hbm.xml file so all the steps remain same
only some modifications in some part and
adding the annotation data for the POJO.
Please note that coming example will use the
Employee table from the database. The SQL
query to create the employee table is as
follows :-
Create table Employee( ID int2 PRIMARY
KEY, NAME n varchar(30) );
Step 1:- Creating A simple Utility Class
The following is the code given for the utility
class for sessionFactory
package net.roseindia;
import org.hibernate.SessionFactory;
import
org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory
sessionFactory;
static {
try {
// Create the SessionFactory from
hibernate.cfg.xml
sessionFactory = new
AnnotationConfiguration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it
might be swallowed
System.err.println(“Initial SessionFactory
creation failed.” + ex);
throw new
ExceptionInInitializerError(ex);
}
}
|
|
Jan 2008 | Java Jazz Up |36 |
|
|
|
View All Topics |
All Pages of this Issue |
Pages:
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53 ,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63 ,
64,
65 ,
66 ,
67 ,
68 ,
69 ,
70 ,
71 ,
72 ,
73 ,
74 ,
75 ,
76 ,
77 ,
78 ,
79 ,
80 ,
81 ,
82 ,
83,
84 ,
85 ,
86,
87 ,
88,
89 ,
90 ,
91 ,
92 ,
93 ,
94 ,
95 ,
96 ,
97 ,
98 ,
99 ,
100 ,
101 ,
102 ,
103,
104 ,
105 ,
106,
107,
Download PDF |
|
|
|
|
|
|
|
|
|