Follow the instructions point by point, run the code and check the results in the database. Check the states of in-memory objects as well (use the debugger, logging, or System.out
).
- Create a PostgreSQL database
jpaexampleDB
and modify DB username and password inresources/META-INF/persistence.xml
. - Start
JPAExample
.Student
andAddress
are annotated with@Entity
, so if you check the database you should see two tables created by Hibernate. - Use the
@Column
annotation to modify the default O-R mapping! Change the column name for attributezipcode
toZip
, limit its length to 4, and set theemail
field toUNIQUE
andNOT NULL
! - It is not needed to persist
age
of students since it is calculated fromdateOfBirth
- exclude it from the table by marking it@Transient
. - Add a
List<String> phoneNumbers
to students, and add it to the constructor! You can fix the problem by@ElementCollection
. Set the name of the auxiliary table toPhone
. - Now students have an address. What happens when addresses have a student as well? Set a symmetric
@OneToOne
relation and see what happens! - We wouldn't want this... Fix the issue by adding a
mappedBy
attribute to the annotation! What has changed? - Annotate POJO
Klass
and convert it to an entity! The corresponding table should be calledClass
. Fix the issues (use@OneToMany
)! What happened in the database? - Again, use
mappedBy
to make the association bidirectional! (You have to drop a table manually if it's not needed anymore.) - IMPORTANT! When using bidirectional relationships it is your responsibility to set the relations both ways to have the in-memory objects in sync with the database!
- Create a new enumerated class called
CCLocation
with valuesMISKOLC
,BUDAPEST
, andKRAKOW
! Add it as an attribute toKlass
and set it in its constructor! How does it get represented in the database? Use the@Enumerated
annotation to change this default behaviour!