-
Notifications
You must be signed in to change notification settings - Fork 11
simulations v0.6
Albert edited this page Apr 12, 2024
·
10 revisions
pull-request
Conflicts must be resolved: this branch has conflicts that must be resolved.
savasto wants to merge 2 commits into AlbertProfe:master from savasto:master
Use the web editor or the to resolve conflicts.
Cath2Bcn wants to merge 2 commits into AlbertProfe:master from Cath2Bcn:master
Use the web editor or the to resolve conflicts.
- new 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>
Annotations:
-
@OneToMany
: Establishes one-to-many relationship. Example:Player
can have multipleSimulation
. Used in JPA/Hibernate. Annotation: @OneToMany(mappedBy = "Player").mappedBy
: Indicates the field on the inverse side of a bidirectional relationship. Specifies the field in the owning side that maps the relationship. Example: In a bidirectional OneToMany, mappedBy points to the field in the ManyToOne side. -
@ManyToOne
: Specifies many-to-one relationship. Multiple instances are associated with one instance of another entity. Example: Manysimulation
belong to oneplayer
. -
@JoinColumn
: Customizes join column. Example: Specifies foreign key column in @ManyToOne or @OneToOne relationships. Annotation: @JoinColumn(name = "PLAYER_FK"). -
@JsonIgnore
: Excludes field from serialization/deserialization. Example: Hides sensitive or irrelevant data in JSON responses. Annotation: @JsonIgnore.
package com.example.demo.model;
//.. imports
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Player {
@Id
private String id;
private String player;
private int age;
private boolean active;
@OneToMany(mappedBy = "player", cascade = CascadeType.ALL)
private List<Simulation> simulations = new ArrayList<>();
@OneToMany(mappedBy = "player", cascade = CascadeType.ALL)
private List<Subscription> subscriptions = new ArrayList<>();
@OneToMany(mappedBy = "player", cascade = CascadeType.ALL)
private List<Payment> payments = new ArrayList<>();
@OneToMany(mappedBy = "player", cascade = CascadeType.ALL)
private List<Card> cards = new ArrayList<>();
public void addSimulation(Simulation simulation) {
this.getSimulations().add(simulation);
//if (simulation.getPlayer() != null) simulation.getPlayer().getSimulations().remove(simulation);
simulation.setPlayer(this);
}
public void addSubscription(Subscription subscription) {
this.getSubscriptions().add(subscription);
//if (subscription.getgetPlayer() != null) subscription.getPlayer().getSubscriptions().remove(subscription);
subscription.setPlayer(this);
}
public void addPayment(Payment payment) {
this.getPayments().add(payment);
//if (simulation.getId() != null) simulation.getId().getSimulations().remove(simulation);
payment.setPlayer(this);
}
public void addCard(Card card) {
this.getCards().add(card);
card.setPlayer(this);
}
}
package com.example.demo.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor @AllArgsConstructor
@Entity
public class Payment {
@Id
private String id;
private String date;
private double cost;
private String concept;
private boolean isPaid;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="PLAYER_FK")
private Player player;
}
package com.example.demo.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor @AllArgsConstructor
@Entity
public class Card {
@Id
private String id;
private int numberOfCard;
private int maxCredit;
private int expDate;
private boolean active;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="PLAYER_FK")
private Player player;
}
datasource
server.port=8089
#H2 DATASOURCE
spring.datasource.url=jdbc:h2:mem:0f74afd0-eefb-4b37-af9f-64249cb2ffa2
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
#DDL
spring.jpa.hibernate.ddl-auto=create-drop
package com.example.demo.repository;
import com.example.demo.model.Payment;
import org.springframework.data.repository.CrudRepository;
public interface PaymentRepository extends CrudRepository<Payment, String> {}
package com.example.demo.repository;
import com.example.demo.model.Card;
import org.springframework.data.repository.CrudRepository;
public interface CardRepository extends CrudRepository<Card, String> {}
Fake data for subscription
object
Runner
Player & Payment/Card
http://localhost:8089/h2-console/
http://localhost:8089/api/v1/player/players
http://localhost:8089/api/v1/payment/payments
http://localhost:8089/api/v1/card/cards
React - Spring Boot - API Rest - H2 DataBase - Axios - React_router_dom - JPA