I've a Java REST API with JPA. Whenever I create an entity, I also want to create another entity with a forgein key. Or maybe someone can advise me otherwise, I would really appreciate it and learn from it =) When i successfully create a company it will make a file entity in the database as well, so that works fine. but, Whenever I execute a findAll method in the JPA repository it will give me a loop of the one company that i've created. like this:
If you need any more information, please let me know!
Company.class
package nl.hulpvriend.dehulpvriend.company;
import javax.validation.constraints.NotNull;
import lombok.*;
import nl.hulpvriend.dehulpvriend.file.File;
import javax.persistence.*;
import javax.validation.constraints.Size;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull
private String email;
@Column(unique = true)
@NotNull(message = "The company name cannot be empty")
@Size(max = 30, message = "Company name cannot be longer than 30 characters")
private String name;
@NotNull(message = "Company must contain a service type")
@Enumerated(EnumType.STRING)
private ServiceType serviceType;
private double stars;
private Integer pricePerHour;
private String description;
private String kvk;
@OneToOne(mappedBy="company", cascade = CascadeType.ALL)
private File file;
}
File.class
package nl.hulpvriend.dehulpvriend.file;
import lombok.*;
import nl.hulpvriend.dehulpvriend.company.Company;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
@AllArgsConstructor
@Getter
@Setter
@NoArgsConstructor
@Entity
@Data
public class File {
@Id
private Integer id;
private String fileId;
@OneToOne(fetch = FetchType.EAGER)
@MapsId
private Company company;
@NotNull(message = "Must contain a data")
@Lob
private byte[] data;
private String downloadUrl;
private String fileName;
private String fileType;
public File(String fileName, String fileType, byte[] data) {
this.fileName = fileName;
this.fileType = fileType;
this.data = data;
}
}