The @Embedded annotation is used to specify the Address entity should be stored in the STUDENTtable as a component.
@Embeddable annotation is used to specify the Address class will be used as a component. TheAddress 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.
}
Now create the hibernate configuration file and add the Student and Address classes.
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
>
Create the Main class to run the example.
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.
}
On executing the Main class you will see the following output.
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