代码如下所示:
传感器类:
@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;
......
}
传感器类型:
@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 为空,因为我只是从 jsp 向 SensorController 发送 SensorType“id”,而控制器需要一个 SensorType 对象。
传感器控制器:
@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";
}
}
我该如何解决这个问题?