Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.java.dojo</groupId>
<artifactId>recipes</artifactId>
Expand Down Expand Up @@ -36,6 +37,19 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/java/dojo/recipes/entities/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.java.dojo.recipes.entities;

import javax.persistence.Entity;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@AllArgsConstructor
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isso serve para todas as classes:

Eu acho que a gente não deveria usar @DaTa nas entidades do JPA por que tem algumas chances de ter conflitos no futuro, essa annotation gera @tostring, @EqualsAndHashCode, @requiredargsconstructor e também @Getter e @Setter.

Tem um artigo legal falando sobre os "riscos" de algumas anotações do JPA com Lombok:

https://thorben-janssen.com/lombok-hibernate-how-to-avoid-common-pitfalls/

Em relação ao @AllArgsConstructor eu acho que a gente poderia colocar acesso private nele, visto que a gente vai usar o @builder então não vamos utilizar em outras partes da aplicação um construtor com os parametros da classes, mas o builder pattern:

@AllArgsConstructor(access = AccessLevel.PRIVATE)

Eu acho que a gente pode colocar o id dessas classes pra serem auto generated, assim cada objeto salvo gera um id automaticamente... uma forma seria:

@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bem demais Pedrão, eu também não manjo muito de utilizar o lombok mas vou correr atrás pra ficar por dentro!

@NoArgsConstructor
@Data
@Entity
@Builder
@EqualsAndHashCode(of = "id")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Por qual motivo vc usou essa annotation do equals e hash code ?

Segundo o time do lombok:

Will soon be marked @deprecated; use the @EqualsAndHashCode.Include annotation together with @EqualsAndHashCode(onlyExplicitlyIncluded = true).

Ela usando "of" vai ser deprecada em breve,,,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cara não manjo muito de lombok kkk

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não sabia disso, vou corrigir dps

@Table(name = "comment")
public class Comment {
private Long id;
private Long post_id;
private String username;
private String content;
private String data;

}
29 changes: 29 additions & 0 deletions src/main/java/com/java/dojo/recipes/entities/Post.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.java.dojo.recipes.entities;

import javax.persistence.Entity;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Builder
@EqualsAndHashCode(of = "id")
@Table(name = "post")
public class Post {

private Long id;
private Long user_id;
private String username;
private String imageUrl;
private String title;
private String content;
private String data;

}
24 changes: 24 additions & 0 deletions src/main/java/com/java/dojo/recipes/entities/Recipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.java.dojo.recipes.entities;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
@AllArgsConstructor
@Builder
public class Recipe {
Comment thread
pedroluiznogueira marked this conversation as resolved.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

precisamos de um @NoArgsConstructor nessa classe também


private List<String> ingredients = new ArrayList<>();
private List<String> preparingMode = new ArrayList<>();

Comment on lines +26 to +28
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


}
26 changes: 26 additions & 0 deletions src/main/java/com/java/dojo/recipes/entities/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.java.dojo.recipes.entities;

import javax.persistence.Entity;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Builder
@EqualsAndHashCode(of = "id")
@Table(name = "user")
public class User {

private Long id;
private String name;
private String surname;
private String email;
private String password;
}
5 changes: 5 additions & 0 deletions src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
4 changes: 3 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@

spring.profiles.active=test
spring.jpa.open-in-view=false
spring.jpa.properties.hibernate.jdbc.time_zone=UTC