当我尝试绑定所有对象时,我无法获取全部数据。
我想绑定整个对象
这是我的百里香精选
<select class="custom-select" id="platformCurrent" name="platform.platformId">
<option value="">--Select Platform to Assign--</option>
<option th:each="platform : ${platforms}" th:value="${platform.platformId}" th:utext="${platform.name}"></option>
</select>
这是我的ManyToOne实体
@Entity
@Table(name="silicon",uniqueConstraints={@UniqueConstraint(columnNames = {"silicon_visual_id"})})
public class Silicon implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id", unique = true, nullable = false)
private Long id;
@Column(name="mir", unique = false, nullable = true)
private Long mir;
@Column(name = "silicon_name", nullable = false, length= 45)
private String siliconName;
@Column(name = "type_silicon", nullable = false, length= 45)
private String typeSilicon;
@Column(name = "qdf", nullable = true, length= 45)
private String qdf;
@Column(name = "silicon_visual_id", nullable = false, length= 45)
private String siliconVisualId;
@Column(name = "cpu_id", nullable = true, length= 45)
private String cpuId;
@Column(name = "stepping", nullable = true, length= 45)
private String stepping;
@Column(name = "socket", nullable = true, length= 45)
private String socket;
@Column(name = "status_silicon", nullable = false,length= 45)
private String statusSilicon;
@JsonIgnore
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="user_owner", nullable = false)
private User userOwner;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(name = "date_admission", nullable = false)
private Date dateAdmission;
@JsonIgnore
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="user_request", nullable = true)
private User userRequest;
@JsonIgnore
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="user_last_returned", nullable = true)
private User userLastReturned;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(name = "date_delivered", nullable = true)
private Date dateDelivered;
@JsonIgnore
@ManyToOne(fetch=FetchType.EAGER,cascade = CascadeType.MERGE)
@JoinColumn(name="platform_current", nullable = true)
private Platform platform;
@Column(name="platform_own", nullable = true)
private String platformOwn;
/*GETTERS AND SETTERS*/
这是我的控制人
@PostMapping({"/admin/siliconAdd"})
public String addOrEditSilicon(@ModelAttribute(name="silicon") Silicon silicon,
final RedirectAttributes redirectAttributes,
HttpServletRequest request,Model model) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
LOG.info(silicon.toString());
if(silicon.getPlatform().getPlatformId() == null) {
silicon.setPlatform(null);
}else {
silicon.setPlatform(platformServiceImpl.getOne(silicon.getPlatform().getPlatformId()));
}
String messageString = null;
Date localDateToday = new Date();
try {
if(silicon.getId() == null) {
messageString = "inserted";
silicon.setDateAdmission(localDateToday);
silicon.setStatusSilicon("AVAILABLE");
silicon.setUserOwner(userServiceImpl.getOne(user.getUsername()));
if(silicon.getUserRequest() != null) {
silicon.setStatusSilicon("ASSIGNED");
siliconServiceImpl.assignSiliconToUser(silicon.getUserRequest(), silicon.getId());
}
} else {
messageString = "updated";
}
Silicon siliconAddOrEdit = siliconServiceImpl.addOne(silicon);
model.addAttribute("silicons",siliconServiceImpl.getAll());
redirectAttributes.addFlashAttribute("success",messageString);
return "redirect:/admin/step2/" + siliconAddOrEdit.getPlatform().getPlatformId();
} catch(Exception ex) {
redirectAttributes.addFlashAttribute("error",messageString);
LOG.info(ex.toString());
return "redirect:/admin/silicon";
}
}
这是我尝试保存一个对象时收到的数据
2019-09-10 16:28:06.975 INFO 10164 --- [nio-8081-exec-3] com.LTR.controller.SiliconController : Silicon [id=6, mir=2323, siliconName=COFFE LAKE, typeSilicon=CPU, qdf=TWER, siliconVisualId=2323, cpuId=50699, stepping=B1, socket=SOCKET 0, statusSilicon=BUSY, userOwner=com.LTR.entity.User@6759637e, dateAdmission=Mon Sep 09 00:00:00 CST 2019, userRequest=com.LTR.entity.User@308fe38c, userLastReturned=null, dateDelivered=null, platform=Platform [platformId=1, location=null, businessUnit=null, name=null, project=null, serialPlatform=null, model=null, chasisSerial=null, chasisModel=null, ismpKitName=null, ismpSerialNumber=null, assignedTo=null, OwnedBy=null, ismNumber=0, receivedDate=null, asset=0, startStatus=false, finalStatus=false, cloudReady=false, annotations=null, silicons=[], Host=null], platformOwn=]
所以你能看到平台对象只有Platformid不为空吗,所以我的问题是我怎么能不用再次查询具有id的对象