I have got 2 classes - 2 entities - Book and BookRentals. BookRentals takes Book object inside.
I want to ignore one of attribute from Book inside BookRentals. This is available
attribute.
I've got method that is rensponsible for finding all rentals. Output in JSON looks like this:
[
{
"id": 1,
"book": {
"id": 1,
"title": "Krzyżacy",
"author": "Henryk Sienkiewicz",
"category": "powieść historyczna",
"available": false
},
"user": {
"id": 2,
"name": "piotri",
"password": "123"
}
}
]
As you can see, available is not necesessary here, but I cant make @JsonIgnore
in Book class, because this variable is needed in finding all books:
[
{
"id": 1,
"title": "Krzyżacy",
"author": "Henryk Sienkiewicz",
"category": "powieść historyczna",
"available": false
}
]
Book class:
package bookrental.model.book;
import lombok.*;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
@Entity
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@NotNull
private String title;
@NotNull
private String author;
@NotNull
private String category;
private boolean available;
public Book(String title, String author, String category, boolean available) {
this.title = title;
this.author = author;
this.category = category;
this.available = available;
}
}
BookRentals class
package bookrental.model.book;
import bookrental.model.account.User;
import lombok.*;
import javax.persistence.*;
@Entity
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class BookRentals {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToOne
private Book book;
@OneToOne
private User user;
public BookRentals(Book book, User user) {
this.book = book;
this.user = user;
}
}
How am I supposed to do that?