Code Monkey home page Code Monkey logo

java-mod-6-jpa-relationship-lab-3's Introduction

JPA Relationship Lab 3

Setup

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.

Entity Relationship Model

You are given the following entity relationship model:

Owner cat many to many relationship diagram

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 the mappedBy attribute in the @ManyToMany annotation.
  • An association between Owner and Cat should be created by calling the addCat() method in the Owner 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.

Implement many-to-many relationship using JPA

  1. You will use the database named jpalab_db that you created from the previous lab.
    Check persistence.xml to make sure the hibernate.hbm2ddl.auto property is set to create.

  2. Edit the Owner class:

    • Add the @Entity annotation to the class.
    • Add the @Id and @GeneratedValue annotations to the id field to make it an auto-incremented primary key.
    • Add the @ManytoMany annotation to the cats field.
  3. Edit the Cat class:

    • Add the @Entity annotation to the class.
    • Add the @Id and @GeneratedValue annotations to the id field to make it an auto-incremented primary key.
    • Add the @ManyToMany annotation to the owners field. Set the mappedBy property to "cats".
  4. Run the JpaCreate.main method. The code should create tables OWNER, CAT, and the join table OWNER_CAT.

  5. 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.

  1. 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 the addCat method on the owner (since Owner is on the owning side of the relationship).
  2. Run the JpaCreate.main method to create and populate all three tables.
  3. 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
  1. Edit persistence.xml to set hibernate.hbm2ddl.auto property is set to none.
  2. Edit persistence.xml to set hibernate.show_sql property to false.
  3. Edit JpaRead to find and print the data for owner with id 1, along with their cats.
    • Replace null on the line Owner owner = null to call the entity manager's find method to get the owner with id 1.
    • Replace null on the line List<Cat> cats = null; to call the getCats method to get the owner's cats.
  4. Run JpaRead to find and print the owner data.
  5. 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'}]

java-mod-6-jpa-relationship-lab-3's People

Contributors

alveem avatar linda-seiter avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.