Skip to content

simulations v0.8

Albert edited this page Apr 27, 2024 · 23 revisions

Project overview

  • n:m with 1:n and n:1: join table as @Entity
    • @JoinColumn as foreign key at many-side
    • and mappedby at List at one-side
  • n:m relationship: inverse-side or owning-side
    • Every @ManyToMany association has two sides and a join table:
      • the owning side
      • the join table for the relationship created
        • the join table must be specified on the owning side.
      • and the non-owning, or inverse, side.
    • If the association is bidirectional, either side may be designated as the owning side.
    • If the relationship is (not) bidirectional, the non-owning side must use the mappedBy element of the @ManyToMany annotation to specify the relationship field or property of the owning side.
  • Annotations:
    • @OneToMany & @ManyToOne
    • @ManyToMany
    • @JoinTable
    • @JoinColumn
    • @JsonIgnore
  • JUnit Jupiter to test
  • H2 database & CommandLineRunner to fill some tables with fake-data (Java Faker)

Project structure

image

POM

pom.xml

  • dependencies: JPA and H2
<dependency>
	<groupId>com.h2database</groupId>
	<artifactId>h2</artifactId>
	<scope>runtime</scope>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Model

JPA @ManyToMany

In Java Persistence API (JPA), the core of many-to-many relationships revolves around the concept of mapping entities to database tables in a relational database.

In a manytomany relationship, entities from one class can be associated with multiple entities from another class, and vice versa. JPA provides mechanisms to represent and manage these relationships effectively.

The core of JPA many-to-many relationships involves defining associations between entities using annotations like @ManyToMany. These annotations establish the relationships between entities and allow JPA to generate the necessary database tables and foreign key constraints automatically. Additionally, JPA provides ways to manage the association table, allowing for cascading operations, fetching strategies, and specifying join columns.

Note

In addition to managing many-to-many relationships, Java Persistence API (JPA) serves as an Object-Relational Mapping (ORM) framework, abstracting the complexities of database interactions. It maps Java objects to database tables, providing a seamless way to store, retrieve, and manipulate data using familiar object-oriented programming paradigms.

  • Idea: there will be a @ManyToMany relationship between Player and University

JPA @ManyToMany: Player, TrafficTrial & University

This lab will provide the two ways to n:m relationship, in this example, there are two many-to-many relationships:

  • Player with TrafficTrial via Enrollments (using a join table Enrolling)
  • TrafficTrial with University

Player with TrafficTrial via Enrollments: This represents a scenario where a player can enroll in multiple traffic trials, and a traffic trial can have multiple players. The Enrolling class serves as the join table between Player and TrafficTrial.

TrafficTrial with University: This represents a scenario where a traffic trial can be associated with multiple universities, and a university can have multiple traffic trials.

Entities

n:m: Player @OneToMany Enrollment @ManyToOne TrafficTrial

Player

image

Enrollment

image

TrafficTrial

image

n:m: TrafficTrial @ManyToMany University

TrafficTrial

image

University

image

Repository

JUnit

Note

For Maven, check out the junit5-jupiter-starter-maven project.

JUnit is one of the most popular unit-testing frameworks in the Java ecosystem.

The JUnit 5 version contains a number of nice innovations, with the goal of supporting new features in Java 8 and above, as well as enabling many different styles of testing.

Test createOneEnrollment()

	@Test
	void createOneEnrollment() {

		String id1 = UUID.randomUUID().toString();
		Player p1 = new Player();
		p1.setId(id1);

		String id2 = UUID.randomUUID().toString();
		Player p2 = new Player();
		p2.setId(id2);

		String id3 = UUID.randomUUID().toString();
		Player p3 = new Player();
		p3.setId(id3);

		String id4 = UUID.randomUUID().toString();
		Player p4 = new Player();
		p4.setId(id4);

		playerRepository.save(p1);
		playerRepository.save(p2);
		playerRepository.save(p3);
		playerRepository.save(p4);

		assertEquals(id1,
				playerRepository.findById(id1).get().getId());
		assertEquals(id2,
				playerRepository.findById(id2).get().getId());
		assertEquals(id3,
				playerRepository.findById(id3).get().getId());
		assertEquals(id4,
				playerRepository.findById(id4).get().getId());

		String tt_id1 = UUID.randomUUID().toString();
		TrafficTrial tt1 = new TrafficTrial();
		tt1.setId(tt_id1);

		trafficTrialRepository.save(tt1);

		String e_id1 = UUID.randomUUID().toString();
		Enrollment e1 = new Enrollment();
		e1.setId(e_id1);

		tt1.addEnrollment(e1);
		p1.addEnrollment(e1);

		e1.setPlayer(p1);
		e1.setTrafficTrial(tt1);

		enrollmentRepository.save(e1);

		assertEquals(e_id1,
				enrollmentRepository.findById(e_id1).get().getId());

		System.out.println("e1-id: " + enrollmentRepository.findById(e_id1).get().getId());
		System.out.println("p1-id: " + enrollmentRepository.findById(e_id1).get().getPlayer().getId());
		System.out.println("tt1-id: " + enrollmentRepository.findById(e_id1).get().getTrafficTrial().getId());

	}

Test createOneTrial_University()

@Test
	void createOneTrial_University() {

		String u_id1 = UUID.randomUUID().toString();
		University u1 = new University();
		u1.setId(u_id1);

		universityRepository.save(u1);

		String tt_id1 = UUID.randomUUID().toString();
		TrafficTrial tt1 = new TrafficTrial();
		tt1.setId(tt_id1);

		trafficTrialRepository.save(tt1);

		u1.addTrafficTrial(tt1);

		universityRepository.save(u1);

		System.out.println("u1-id: " + universityRepository.findById(u_id1).get().getId());
		System.out.println("tt1-id: " + trafficTrialRepository.findById(tt_id1).get().getId());

	}

Output

createOneEnrollment

image

createOneTrial_University

image

Clone this wiki locally