|
Hibernate with Annotation |
|
public static SessionFactory
getSessionFactory() {
return sessionFactory;
}
}
If you see the only change in the file for the
annotations is that we simply use
AnnotationConfiguration() class instead of the
Configuration() class to build the
sessionFactory for the hibernate.
Step 2: Creating the Hibernate.cfg.xml
The Hibernate.cfg.xml is the configuration file
for the datasource in the Hibernate world. The
following is the example for the configuration
file.
<?xml version=’1.0' encoding=’utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
”-//Hibernate/Hibernate Configuration DTD
3.0//EN”
”http://hibernate.sourceforge.net/hibernateconfiguration-
3.0.dtd”>
<hibernate-configuration>
<session-factory>
<!— Database connection settings —>
<property
name=”connection.driver_class”>com.mysql.jdbc.Driver</
property>
<property
name=”connection.url”>jdbc:mysql://
localhost:3306/test</property>
<property
name=”connection.username”>root</
property>
<property
name=”connection.password”>root</
property>
<!— JDBC connection pool (use the built-in)
—>
<property name=”connection.pool_size”>1
</property>
<!— SQL dialect —>
<property name=”dialect”>
org.hibernate.dialect.MySQLDialect
</property>
<!— Enable Hibernate’s automatic session
context management —> |
|
<property
name=”current_session_context_class”>thread
</property>
<!— Disable the second-level cache —>
<property name=”cache.provider_class”>
org.hibernate.cache.NoCacheProvider
</property>
<!— Echo all executed SQL to stdout —>
<property name=”show_sql”>true
</property>
<!— Drop and re-create the database
schema on startup —>
<property name=”hbm2ddl.auto”>none
</property>
<mapping class=”com.roseindia.Employee”/>
</session-factory>
</hibernate-configuration>
The only change is that, we are just telling
the compiler that instead of any resource get
the metadata for the mapping from the POJO
class itself like in this case it is
net.roseindia.Employee.
Note: Using annotations does not mean that
you cannot give the hbm.xml file mappng. You
can mix annotations and hbm.xml files mapping
for different Pojo but you cannot mix the
mappings for the same POJO in both ways.
Step 3:Developing a simple POJO
The major changes occurred only in the POJO
files if you want to use the annotations as this
is the file which will now contain the mapping of
the properties with the database.
The following is the code for the Employee
POJO.
package net.roseindia;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
|
|
|
Jan 2008 | Java Jazz Up |37 |
|
|
|
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 |
|
|
|
|
|
|