Processor.java
@Entity
public class Processor {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private boolean running;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "firmware_id", referencedColumnName = "id")
private Firmware firmware;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
...
public Firmware getFirmware() {
return firmware;
}
public void setFirmware(Firmware firmware) {
this.firmware = firmware;
}
}
Firmware.java
@Entity
public class Firmware {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String version;
private String filename;
//Getter & Settger
}
ProcessorRepository.java
public interface ProcessorRepository extends CrudRepository<Processor, Integer> {
}
ProcessorController.java
...
@PutMapping(path = "/") // Map ONLY POST Requests
public @ResponseBody
Map<String, Object> addProcessor(@RequestBody Processor p) {
System.out.println("Put: " + p.getId());
processorRepository.save(p);
// I want to access : p.firmware_id;
// ex) p.setFirmware_id(8)
// ex) int tmp = p.getFirmware_id();
return Collections.singletonMap("success", true);
}
...
以下代码在Java/Spring Boot/Hibernate中是否可行?
// ex) p.setFirmware_id(8)
// ex) int tmp = p.getFirmware_id();
, insertable = false, updatable = false
是什么意思,但是它运行良好,谢谢。 - Wang Liang