我使用Spring Boot和Hibernate在MYSQL数据库中保存两个对象。 "健康"与" Checks"有一个OneToMany关系。
HealthRepository.java:
int u = (int)Char.GetNumericValue(c);
健康实体:
import org.springframework.data.repository.CrudRepository;
import healthcheckapi.model.Health;
public interface HealthRepository extends CrudRepository<Health, Integer> {
}
检查实体:
@Entity
@Table(name="health")
public class Health {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String server;
private String description;
private String timestamp;
private String offset;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "health", fetch = FetchType.LAZY)
private List<Checks> checks;
public Health() {
}
public Health(int id, String server, String description, String timestamp, String offset, List<Checks> checks) {
this.server = server;
this.description = description;
this.timestamp = timestamp;
this.offset = offset;
this.checks = checks;
}
//getters&setters
传入的JSON数据:
@Entity
@Table(name="checks")
public class Checks {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private int status;
@ManyToOne(/*cascade = CascadeType.MERGE,*/ targetEntity = Health.class, fetch = FetchType.EAGER)
@JoinColumn(name = "healthid", referencedColumnName="id")
private Health health;
public Checks() {
}
public Checks(String name, int status) {
this.name = name;
this.status = status;
}
//getters&setters
问题:
检查对象列表已成功保留在其数据库中,健康对象也是如此。
但是,在检索健康对象时,尽管数据库具有外键关系,但只返回空的检查对象数组。
在任何其他类中几乎没有逻辑,这在实体类的注释中定义。