In this example you will learn how to map components using Hibernate Annotations. Consider the following relationship between Student and Address entity.
According to the relationship each student should have a unique address.
Since the Student and Address entities are strongly related (composition relation), it is better to store them in a single table. The relational model is shown below.
Student class is used to create the STUDENT table.
01.
package
com.vaannila.student;
02.
03.
import
javax.persistence.Column;
04.
import
javax.persistence.Embedded;
05.
import
javax.persistence.Entity;
06.
import
javax.persistence.GeneratedValue;
07.
import
javax.persistence.Id;
08.
import
javax.persistence.Table;
09.
10.
@Entity
11.
@Table
(name =
"STUDENT"
)
12.
public
class
Student {
13.
14.
private
long
studentId;
15.
private
String studentName;
16.
private
Address studentAddress;
17.
18.
public
Student() {
19.
}
20.
21.
public
Student(String studentName, Address studentAddress) {
22.
this
.studentName = studentName;
23.
this
.studentAddress = studentAddress;
24.
}
25.
26.
@Id
27.
@GeneratedValue
28.
@Column
(name =
"STUDENT_ID"
)
29.
public
long
getStudentId() {
30.
return
this
.studentId;
31.
}
32.
33.
public
void
setStudentId(
long
studentId) {
34.
this
.studentId = studentId;
35.
}
36.
37.
@Column
(name =
"STUDENT_NAME"
, nullable =
false
, length =
100
)
38.
public
String getStudentName() {
39.
return
this
.studentName;
40.
}
41.
42.
public
void
setStudentName(String studentName) {
43.
this
.studentName = studentName;
44.
}
45.
46.
@Embedded
47.
public
Address getStudentAddress() {
48.
return
this
.studentAddress;
49.
}
50.
51.
public
void
setStudentAddress(Address studentAddress) {
52.
this
.studentAddress = studentAddress;
53.
}
54.
55.
}
@Embeddable annotation is used to specify the Address class will be used as a component. The Address class cannot have a primary key of its own, it uses the enclosing class primary key.
01.
package
com.vaannila.student;
02.
03.
import
javax.persistence.Column;
04.
import
javax.persistence.Embeddable;
05.
06.
@Embeddable
07.
public
class
Address {
08.
09.
private
String street;
10.
private
String city;
11.
private
String state;
12.
private
String zipcode;
13.
14.
public
Address() {
15.
}
16.
17.
public
Address(String street, String city, String state, String zipcode) {
18.
this
.street = street;
19.
this
.city = city;
20.
this
.state = state;
21.
this
.zipcode = zipcode;
22.
}
23.
24.
@Column
(name =
"ADDRESS_STREET"
, nullable =
false
, length=
250
)
25.
public
String getStreet() {
26.
return
this
.street;
27.
}
28.
29.
public
void
setStreet(String street) {
30.
this
.street = street;
31.
}
32.
33.
@Column
(name =
"ADDRESS_CITY"
, nullable =
false
, length=
50
)
34.
public
String getCity() {
35.
return
this
.city;
36.
}
37.
38.
public
void
setCity(String city) {
39.
this
.city = city;
40.
}
41.
42.
@Column
(name =
"ADDRESS_STATE"
, nullable =
false
, length=
50
)
43.
public
String getState() {
44.
return
this
.state;
45.
}
46.
47.
public
void
setState(String state) {
48.
this
.state = state;
49.
}
50.
51.
@Column
(name =
"ADDRESS_ZIPCODE"
, nullable =
false
, length=
10
)
52.
public
String getZipcode() {
53.
return
this
.zipcode;
54.
}
55.
56.
public
void
setZipcode(String zipcode) {
57.
this
.zipcode = zipcode;
58.
}
59.
60.
}
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<!DOCTYPE hibernate-configuration PUBLIC
03.
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
05.
<
hibernate-configuration
>
06.
<
session-factory
>
07.
<
property
name
=
"hibernate.connection.driver_class"
> org.hsqldb.jdbcDriver</
property
>
08.
<
property
name
=
"hibernate.connection.url"
> jdbc:hsqldb:hsql://localhost<;/
property
>
09.
<
property
name
=
"hibernate.connection.username"
>sa</
property
>
10.
<
property
name
=
"connection.password"
></
property
>
11.
<
property
name
=
"connection.pool_size"
>1</
property
>
12.
<
property
name
=
"hibernate.dialect"
> org.hibernate.dialect.HSQLDialect</
property
>
13.
<
property
name
=
"show_sql"
>true</
property
>
14.
<
property
name
=
"hbm2ddl.auto"
>create-drop</
property
>
15.
<
mapping
class
=
"com.vaannila.student.Student"
/>
16.
<
mapping
class
=
"com.vaannila.student.Address"
/>
17.
</
session-factory
>
18.
</
hibernate-configuration
>
01.
package
com.vaannila.student;
02.
03.
import
org.hibernate.HibernateException;
04.
import
org.hibernate.Session;
05.
import
org.hibernate.Transaction;
06.
07.
import
com.vaannila.util.HibernateUtil;
08.
09.
public
class
Main {
10.
11.
public
static
void
main(String[] args) {
12.
Session session = HibernateUtil.getSessionFactory().openSession();
13.
Transaction transaction =
null
;
14.
try
{
15.
transaction = session.beginTransaction();
16.
Address address =
new
Address(
"OMR Road"
,
"Chennai"
,
"TN"
,
"600097"
);
17.
Student student =
new
Student(
"Eswar"
, address);
18.
session.save(student);
19.
transaction.commit();
20.
}
catch
(HibernateException e) {
21.
transaction.rollback();
22.
e.printStackTrace();
23.
}
finally
{
24.
session.close();
25.
}
26.
27.
}
28.
29.
}
Each student has one address and the values are stored in the same STUDENT table.
The folder structure of the example is shown below.
No comments:
Post a Comment