如何解决Java Hibernate:findAll 包含“一对一”关系
我想返回所有与processor
表有关系的命令记录
@Entity // This tells Hibernate to make a table out of this class
public class Command {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String type;
private Integer status;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "processor_id",referencedColumnName = "id")
private Processor processor;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
@Entity // This tells Hibernate to make a table out of this class
public class Processor {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String ip;
private String mac;
private boolean status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}
public boolean getStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
}
CommandRepository.java
// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create,Read,Update,Delete
public interface CommandRepository extends CrudRepository<Command,Integer> {
}
main.java
@GetMapping(path="/all")
public @ResponseBody Iterable<Command> getAllCommand() {
// This returns a JSON or XML with the users
return commandRepository.findAll();// this is not returning processor data
}
解决方法
Command 实体类中缺少处理器的 Getter/Setter。添加它们检查,它应该可以工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。