简体   繁体   中英

Spring JPA Hibernate object null when saving ManyToOne

I have got two entities in bidirectional relation ( ManyToOne, OneToMany ) Two tables 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 ).

How can I achieve this?

Does my inputs concerning rooms are ok? eg in PHP I'll have name="roomName[]"

I thought that Hibernate will do everything for me :) especially when I use 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. 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

<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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM