From a3cb07010c7c761d22f023975979743cd73bf143 Mon Sep 17 00:00:00 2001 From: GabryelBoeira Date: Tue, 8 Oct 2024 23:17:14 -0300 Subject: [PATCH] =?UTF-8?q?configura=C3=A7=C3=A3o=20de=20dados=20base?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Para facilitar o trabalho na aplicação esta sendo utilizado um script sql para inserir os dados necessários --- VideoLocadora/pom.xml | 4 ++++ .../entity/customer/AddressEntity.java | 16 +++++++++++-- .../entity/customer/CustomerEntity.java | 18 ++++++++++++++- .../src/main/resources/application.yaml | 23 ++++++++++++------- VideoLocadora/src/main/resources/data.sql | 8 +++++++ 5 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 VideoLocadora/src/main/resources/data.sql diff --git a/VideoLocadora/pom.xml b/VideoLocadora/pom.xml index 62b81f2..3520e53 100644 --- a/VideoLocadora/pom.xml +++ b/VideoLocadora/pom.xml @@ -31,8 +31,12 @@ 21 + UTF-8 + 21 + 21 + org.springframework.boot diff --git a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/entity/customer/AddressEntity.java b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/entity/customer/AddressEntity.java index bbbea85..5c42874 100644 --- a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/entity/customer/AddressEntity.java +++ b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/entity/customer/AddressEntity.java @@ -1,7 +1,15 @@ package io.github.gabryel.videolocadora.entity.customer; +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonManagedReference; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import lombok.Data; @@ -11,12 +19,15 @@ @Data @Entity @Table(name = "address") +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") +@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler"}) public class AddressEntity implements Serializable { private static final long serialVersionUID = 198873988892602989L; @Id - private String id; + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; private String street; @@ -34,7 +45,8 @@ public class AddressEntity implements Serializable { private boolean isBillingAddress; - @ManyToOne + @ManyToOne(fetch= FetchType.LAZY) + @JoinColumn(name = "CUSTOMER_ID", nullable = false) private CustomerEntity customer; public AddressEntity() { diff --git a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/entity/customer/CustomerEntity.java b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/entity/customer/CustomerEntity.java index 3a76dc2..aeaba34 100644 --- a/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/entity/customer/CustomerEntity.java +++ b/VideoLocadora/src/main/java/io/github/gabryel/videolocadora/entity/customer/CustomerEntity.java @@ -1,6 +1,14 @@ package io.github.gabryel.videolocadora.entity.customer; +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; @@ -12,18 +20,26 @@ @Data @Entity @Table(name = "customer") +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") +@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler"}) public class CustomerEntity implements Serializable { private static final long serialVersionUID = 2106833642965188722L; @Id + @GeneratedValue(strategy = GenerationType.AUTO) private Long id; + private String name; + private String cpf; + + @Column(name = "delay_devolution") private boolean delayDevolution; + private String email; - @OneToMany + @OneToMany(mappedBy = "customer", cascade= CascadeType.ALL, fetch= FetchType.LAZY) private List addressList; public CustomerEntity() {} diff --git a/VideoLocadora/src/main/resources/application.yaml b/VideoLocadora/src/main/resources/application.yaml index cd0939f..ae4cb54 100644 --- a/VideoLocadora/src/main/resources/application.yaml +++ b/VideoLocadora/src/main/resources/application.yaml @@ -32,21 +32,28 @@ spring: config: activate: on-profile: development + + sql.init.mode: always datasource: driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/videolocadora + url: jdbc:mysql://localhost:3306/videolocadora?useSSL=false username: root password: root - + initialization-mode: always jpa: - show-sql: true + show-sql: false database: mysql generate-ddl: false - hibernate.ddl-auto: none - properties: - hibernate: - globally_quoted_identifiers: true - format_sql: true + defer-datasource-initialization: true + hibernate: + ddl-auto: create + globally_quoted_identifiers: true + format_sql: true + dialect : org.hibernate.dialect.MySQL8Dialect + jackson: + serialization: + FAIL_ON_EMPTY_BEANS: false + --- spring: diff --git a/VideoLocadora/src/main/resources/data.sql b/VideoLocadora/src/main/resources/data.sql new file mode 100644 index 0000000..4161235 --- /dev/null +++ b/VideoLocadora/src/main/resources/data.sql @@ -0,0 +1,8 @@ +#-- ADD CLIENTES, CPFs gerados automaticamente https://www.4devs.com.br/gerador_de_cpf + + +insert into CUSTOMER (ID, NAME, CPF, DELAY_DEVOLUTION, EMAIL) values (-2, 'João' , '37772382014', false, 'j@j.com'); +insert into CUSTOMER (ID, NAME, CPF, DELAY_DEVOLUTION, EMAIL) values (-3, 'Jose' , '06355528091', false, 'j@j.com'); +insert into CUSTOMER (ID, NAME, CPF, DELAY_DEVOLUTION, EMAIL) values (-4, 'Carlos', '80071319069', false, 'j@j.com'); +insert into CUSTOMER (ID, NAME, CPF, DELAY_DEVOLUTION, EMAIL) values (-5, 'Pedro' , '81454522011', false, 'j@j.com'); +insert into CUSTOMER (ID, NAME, CPF, DELAY_DEVOLUTION, EMAIL) values (-6, 'Will' , '14915748014', false, 'j@j.com');