Java hibernate:访问foregin密钥
Processor.javaJava hibernate:访问foregin密钥,java,spring-boot,hibernate,Java,Spring Boot,Hibernate,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
@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;
}
}
固件.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> {
}
您可以尝试通过以下方式更正映射:
@实体
公共类处理器{
// ...
//@NotFound(action=NotFoundAction.IGNORE)
@OneToOne(级联=级联类型.ALL)
@JoinColumn(name=“firmware\u id”,referencedColumnName=“id”,insertable=false,updateable=false)
私有固件;
@列(name=“固件\u id”)
私有整数firmwareId;
// ...
}
然后通过固件id
设置固件id
对于这种情况,您可能还应该使用
@NotFound
注释(请参阅文档中的部分)是的,但是,我不想使用getFirmware()
我不知道这是什么,insertable=false,updateable=false
,但是,这很好用,谢谢
...
@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);
}
...
// ex) p.setFirmware_id(8)
// ex) int tmp = p.getFirmware_id();
p.getFirmware().getId() or p.getFirmware().setId(id) whatever works