Reputation: 39
In my spring project, I want to connect the entities named room and product to each other.
First i will add a hotel then i will add the rooms of the hotels
A hotel will have more than one room and a room will have a hotel
I connected using ManyToOne and OneToMany, but the room array is empty when listing.
I want list rooms in product. How should I write a code
Like This
{
"status": true,
"result": [
{
"pid": 28,
"otel_name": "Hi Otel",
"rooms": [],
"description": "hi otel",
"location": {
"lid": 11,
"city": "Antalya",
"district": "Kaş"
},
"taxonomies": [
{
"tax_id": 25,
"name": "Disco",
"description": "Disco",
"product": null
},
{
"tax_id": 26,
"name": "Suitable for family",
"description": "suitable for family",
"product": null
}
],
"star_ratings": 5
}
]
}
Product.java
@Entity
@Data
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer pid;
@NotBlank(message = "Otel name can not be blank")
@Length(message = "Otel name must contain min 2 max 50", min = 2, max=50)
private String otel_name;
@OneToMany
@JoinColumn(name="room_id")
private List<Rooms> rooms = new ArrayList<>();
@NotBlank(message = "Description name can not be blank")
@Length(message = "Description must contain min 2 max", min = 2)
private String description;
@ManyToOne
@JoinColumn(name="lId",referencedColumnName = "lid")
private Location location;
@OneToMany(cascade ={CascadeType.MERGE})
@JoinTable(name="products_taxonomy",joinColumns = @JoinColumn(name="p_id" ),
inverseJoinColumns = @JoinColumn( name = "t_id", referencedColumnName = "tax_id")
)
private List<Taxonomy> taxonomies;
@PositiveOrZero(message = "Star ratings must be between 0 5")
@NotNull(message = "Star ratings must be between 0 5")
@Max(value = 5, message = "Star ratings must be between 0 5")
private Integer star_ratings;
}
Rooms.java
@Entity
@Data
public class Rooms {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer room_id;
@ManyToOne
@JoinColumn(name="p_id",referencedColumnName = "pid")
private Product product;
@NotBlank(message = "Name can not be blank")
@Length(message = "Name must contain min 2 max 20 character.", min = 2, max = 20)
private String name;
@NotBlank(message = "Description can not be blank")
@Length(message = "Description must contain min 2 max 255 character.", min = 2, max = 255)
private String description;
@PositiveOrZero(message = "Star ratings must be positive")
@NotNull(message = "Bed not null")
private Integer bed;
@PositiveOrZero(message = "Price must be positive")
@NotNull(message = "price not null")
private Integer price;
@PositiveOrZero(message = "Price must be positive")
@NotNull(message = "price not null")
private Integer quantity;
}
Upvotes: 0
Views: 469
Reputation: 61
try this:
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL)
private List<Rooms> rooms = new ArrayList<>();
@ManyToOne(fetch = FetchType.LAZY)
private Product product;
Upvotes: 1
Reputation: 67
@OneToMany
@JoinColumn(name="room_id")
private List<Rooms> rooms = new ArrayList<>();
Add here referenced column name, now spring dont know what is id in rooms class
Upvotes: 1