Fork and clone the lab and open the project in IntelliJ.
The lab repository has the required dependencies defined in pom.xml
and the database configuration is defined in persistence.xml
.
You are given the following entity relationship model:
There is a many-to-many relationship between Owner
and Cat
.
An owner has many cats and each cat may have many owners.
Open the Owner
class. Notice it has a field for a list of cats.
private List<Cat> cats = new ArrayList<>();
Open the Cat
class. It has a field for a list of owners.
private List<Owner> owners = new ArrayList<>();
The Owner
and Cat
classes are POJOs (plain old Java objects).
Neither class contains JPA annotations (@Entity, @Id, etc).
While either entity can be chosen as the relationship owner,
we will pick Owner
to be on the owning side of the relationship.
- Each list should be annotated with
@ManyToMany
. Cat
is on the non-owning side and should specify themappedBy
attribute in the@ManyToMany
annotation.- An association between
Owner
andCat
should be created by calling theaddCat()
method in theOwner
class.
NOTE: Since Owner
and Cat
both contain a list of instances of the other class, it is important
to avoid an infinite cycle of method calls if you have IntelliJ generate the toString()
method.
The default toString()
generated by IntelliJ would include a call to the toString()
method
for each object in the list, which would in turn call the toString()
method for each
object in the other list, etc. We avoid this infinite cycle of method calls
by removing the lists from the toString()
methods generated by IntelliJ.
-
You will use the database named
jpalab_db
that you created from the previous lab.
Checkpersistence.xml
to make sure thehibernate.hbm2ddl.auto
property is set tocreate
. -
Edit the
Owner
class:- Add the
@Entity
annotation to the class. - Add the
@Id
and@GeneratedValue
annotations to theid
field to make it an auto-incremented primary key. - Add the
@ManytoMany
annotation to thecats
field.
- Add the
-
Edit the
Cat
class:- Add the
@Entity
annotation to the class. - Add the
@Id
and@GeneratedValue
annotations to theid
field to make it an auto-incremented primary key. - Add the
@ManyToMany
annotation to theowners
field. Set themappedBy
property to"cats"
.
- Add the
-
Run the
JpaCreate.main
method. The code should create tablesOWNER
,CAT
, and the join tableOWNER_CAT
. -
Use the pgAdmin query tool to query the tables.
SELECT * FROM OWNER;
ID | NAME |
---|---|
1 | Jia |
2 | Yuri |
3 | Luka |
SELECT * FROM CAT;
ID | AGE | BREED | NAME |
---|---|---|---|
4 | 5 | Tabby | Meowie |
5 | 1 | Birman | Whiskers |
6 | 10 | American Shorthair | Freddy Purrcury |
SELECT * FROM OWNER_CAT;
OWNERS_ID | CATS_ID |
---|
The OWNER_CAT
join table is empty because JpaCreate
has not set up any associations.
- Edit
JpaCreate
to add associations between owners and cats.
The first two owners own the first two cats, while the third owner owns the third cat. Establish the associations by calling theaddCat
method on the owner (sinceOwner
is on the owning side of the relationship). - Run the
JpaCreate.main
method to create and populate all three tables. - Use the pgAdmin query tool to query the tables.
SELECT * FROM OWNER;
ID | NAME |
---|---|
1 | Jia |
2 | Yuri |
3 | Luka |
SELECT * FROM CAT;
ID | AGE | BREED | NAME |
---|---|---|---|
4 | 5 | Tabby | Meowie |
5 | 1 | Birman | Whiskers |
6 | 10 | American Shorthair | Freddy Purrcury |
SELECT * FROM OWNER_CAT;
OWNERS_ID | CATS_ID |
---|---|
1 | 4 |
1 | 5 |
2 | 4 |
2 | 5 |
3 | 6 |
- Edit
persistence.xml
to sethibernate.hbm2ddl.auto
property is set tonone
. - Edit
persistence.xml
to sethibernate.show_sql
property tofalse
. - Edit
JpaRead
to find and print the data for owner with id 1, along with their cats.- Replace
null
on the lineOwner owner = null
to call the entity manager'sfind
method to get the owner with id 1. - Replace
null
on the lineList<Cat> cats = null;
to call thegetCats
method to get the owner's cats.
- Replace
- Run
JpaRead
to find and print the owner data. - Confirm the output from the two print statements:
Owner{id=1, name='Jia'}
[Cat{id=4, name='Meowie', age=5, breed='Tabby'}, Cat{id=5, name='Whiskers', age=1, breed='Birman'}]