[英]Spring JPA Hibernate object null when saving ManyToOne
I have got two entities in bidirectional relation ( ManyToOne, OneToMany ) Two tables House & Room. 我在双向关系中有两个实体(ManyToOne,OneToMany)两个表House&Room。 House has many rooms. 房子有很多房间。
I need to save house with it's address and two ( maybe later on more ) rooms. 我需要用它的地址和两个(也许以后会更多)房间来节省房子。 The below code adds house and address without rooms ( room = null ). 下面的代码添加没有房间的房子和地址(room = null)。
How can I achieve this? 我该如何实现?
Does my inputs concerning rooms are ok? 我输入的房间还可以吗? eg in PHP I'll have name="roomName[]"
例如在PHP中,我将具有name="roomName[]"
I thought that Hibernate will do everything for me :) especially when I use cascadeType.ALL 我以为Hibernate会为我做所有事情:),尤其是当我使用cascadeType.ALL时
@Entity
@Data
public class House {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
private String name;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "address_id")
private PostalAddress address;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "house", fetch = FetchType.EAGER)
private Set<Room> rooms;
}
Room 房间
@Entity
@Data
public class Room {
@Id
@GeneratedValue(strategy = IDENTITY)
private int id;
private String name;
@NotNull
private int capacity;
@NotNull
@ManyToOne
@JoinColumn(name = "house_id")
private House house;
}
Create db entry Form 创建数据库条目表
<input type="text" name="name" placeholder="house name..." />
<input type="text" name="address.addressLocality" placeholder="locality" />
<input type="text" name="address.postalCode" placeholder="postalCode" />
<input type="text" name="address.streetAddress" placeholder="street" />
<input type="text" name="room.name" placeholder="room name" />
<input type="text" name="room.capacity" placeholder="capacity" />
<input type="text" name="room.name" placeholder="room name" />
<input type="text" name="room.capacity" placeholder="capacity" />
Finally my Controller and repository 最后是我的控制器和存储库
public interface RoomRepository extends CrudRepository<Room, Long> {
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String saveHouse(@ModelAttribute("house/new") House house, BindingResult bindingResult, Model model) {
houseRepository.save(house);
return "redirect:/house/list";
}
// IMPORTANT EDIT //重要的编辑
I've got tip what can be wrong but I can't resolve this. 我已经提示了什么是错的,但我无法解决。 I'm using Thymeleaf. 我正在用Thymeleaf。 When House has Many Rooms ( Set rooms ) the part of the form that contains room inputs is incorrect. 当房屋有许多房间(设置房间)时,包含房间输入的表格部分不正确。 I'm displaying, by default, 4 rows ( room name, capacity ) in table 默认情况下,我在表格中显示4行(房间名称,可容纳人数)
<tr th:each="i,iterStat : ${#numbers.sequence( 1, 4)}">
<td class="col-xs-9 col-md-9"><input type="text" name="rooms.name" th:value="ROOM + ' ' + ${iterStat.current}" class="form-control"/> </td>
<td class="col-xs-2 col-md-2"><input type="number" name="rooms.capacity" min="1" step="1" value="4" class="form-control"/> </td>
<td class="col-xs-1 col-md-1"><button type="button" class="btn red pull-right" onclick="removeTableRow(this)"> <i class="fa fa-trash"></i> </button></td>
</tr>
when user would like to add more or less rooms he ( using jQuery ) deletes/add table row with inputs I was trying to change input name to 当用户想要添加更多或更少的房间时,他(使用jQuery)删除/添加带有输入的表行,而我试图将输入名称更改为
th:field="*{rooms[ ${iterStat.current} ].name}" th:field =“ * {rooms [ $ {iterStat.current} ] .name}”
but this is not it. 但这不是。 So how to populate these fields? 那么如何填充这些字段呢?
Try House entity mapping with: 尝试使用以下方法进行房屋实体映射:
@OneToOne(fetch = FetchType.EAGER, mappedBy = "house", cascade = {CascadeType.ALL})
private PostalAddress address;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "house", cascade = {CascadeType.ALL}, orphanRemoval = true)
private Set<Room> rooms;
PostalAddress: 邮寄地址:
@OneToOne
@JoinColumn(name="house")
private House house;
Room: 房间:
@NotNull
@ManyToOne
@JoinColumn(name="house")
private House house;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.