代码如下所示:
传感器类别:
@Entity
@Table(name = "sensor")
public class Sensor {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
private SensorType sensorType;
@NotEmpty
@Size(min = 3, max = 255)
@Column(name = "name")
private String name;
......
}
SensorType:
@Entity
@Table(name = "sensortype")
public class SensorType {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "type")
private String type;
......
}
我得到一个错误,当我尝试插入一个新的传感器,SensorType为null,因为我只从JSP发送SensorType“ID”到SensorController和控制器期待一个SensorType对象。
SensorController:
@Controller
public class SensorController {
@Autowired
private ISensor sensorService;
@RequestMapping(value = "/sensor/add", method = RequestMethod.POST)
public String add(Sensor sensor, BindingResult result, Model model) {
if (result.hasErrors()) {
return "sensorAdd";
}
sensorService.insert(sensor);
return "redirect:/sensors";
}
}
我该如何解决这个问题?