spring boot jpa合并两个表
springboot jpa combine two tables
我想查询两个表的数据,
Translation 中的 location 字段是来自 Location
的 id 字段的外键
@Entity
@Table(name = "Translation")
@Data
public class Translation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull private String language;
@NotNull private String name;
@NotNull private String description;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "location", nullable = false, insertable = false, updatable = false)
@Fetch(FetchMode.JOIN)
private Location location;
}
@Entity
@Table(name = "Location")
@Data
public class Location {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull private String code;
@NotNull private String type;
private Double longitude;
private Double latitude;
@Column(name = "parent_id")
private Integer parentId;
@OneToMany(targetEntity = Translation.class, mappedBy="id", fetch = FetchType.EAGER)
private Set<Translation> translations;
}
————————————————————————————————————————
但是当我使用查询时
@Query(
"SELECT new com.afkl.travel.exercise.model.RetrieveLocationResponse("
+ "loc.code, tran.name, loc.type, loc.latitude, loc.longitude, tran.description, loc.parentId)"
+ "FROM Location loc LEFT JOIN loc.translation tran")
List<RetrieveLocationResponse> fetchLeftJoin();
Translation相关的字段全部为null,不知道发生了什么
更新
以下这些对我有用。
@OneToMany(mappedBy = "location", cascade = CascadeType.ALL)
@JsonIgnore
private Set<Translation> translations;
@ManyToOne
@JoinColumn(name = "location")
private Location location;
尝试
@OneToMany(mappedBy = "location", cascade = CascadeType.ALL)
@JsonIgnore
private Set<Translation> translations;
@ManyToOne
@JoinColumn(name = "location")
private Location location;
我想查询两个表的数据,
Translation 中的 location 字段是来自 Location
的 id 字段的外键@Entity
@Table(name = "Translation")
@Data
public class Translation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull private String language;
@NotNull private String name;
@NotNull private String description;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "location", nullable = false, insertable = false, updatable = false)
@Fetch(FetchMode.JOIN)
private Location location;
}
@Entity
@Table(name = "Location")
@Data
public class Location {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull private String code;
@NotNull private String type;
private Double longitude;
private Double latitude;
@Column(name = "parent_id")
private Integer parentId;
@OneToMany(targetEntity = Translation.class, mappedBy="id", fetch = FetchType.EAGER)
private Set<Translation> translations;
}
————————————————————————————————————————
但是当我使用查询时
@Query(
"SELECT new com.afkl.travel.exercise.model.RetrieveLocationResponse("
+ "loc.code, tran.name, loc.type, loc.latitude, loc.longitude, tran.description, loc.parentId)"
+ "FROM Location loc LEFT JOIN loc.translation tran")
List<RetrieveLocationResponse> fetchLeftJoin();
Translation相关的字段全部为null,不知道发生了什么
更新
以下这些对我有用。
@OneToMany(mappedBy = "location", cascade = CascadeType.ALL)
@JsonIgnore
private Set<Translation> translations;
@ManyToOne
@JoinColumn(name = "location")
private Location location;
尝试
@OneToMany(mappedBy = "location", cascade = CascadeType.ALL)
@JsonIgnore
private Set<Translation> translations;
@ManyToOne
@JoinColumn(name = "location")
private Location location;