我有2个数据库表。实体设计为:
@Entity
@Table(name="CityTable")
public class CityTable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="city_id")
private int cityId;
private int addedBy;
@Column(name="city_name")
private String cityName;
@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name="city_id", referencedColumnName="city_id", insertable=false, updatable=false)
private List<VirtualCityMapping> virtualCity;
. getters and setters
}
第二
@Entity
@Table(name="virtualCityMapping")
public class VirtualCityMapping implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private VirtualCityMappingEmbedded key;
public VirtualCityMapping() {
}
public VirtualCityMappingEmbedded getKey() {
return key;
}
public void setKey(VirtualCityMappingEmbedded key) {
this.key = key;
}
@Override
public String toString() {
return "VirtualCityMapping [key=" + key + "]";
}
}
使用嵌入式密钥,如下所述
@Embeddable
public class VirtualCityMappingEmbedded implements Serializable {
@Column(name="city_id",nullable=false)
private int cityId;
@Column(nullable=false)
private int virtualCityId;
public int getCityId() {
return cityId;
}
public void setCityId(int cityId) {
this.cityId = cityId;
}
public int getVirtualCityId() {
return virtualCityId;
}
public void setVirtualCityId(int virtualCityId) {
this.virtualCityId = virtualCityId;
}
@Override
public String toString() {
return "VirtualCityMappingEmbedded [cityId=" + cityId + ", virtualCityId=" + virtualCityId + "]";
}
}
我的城市回购
公共接口CityRepository扩展了JpaRepository {
List<CityTable> findByCityIdIn(List<Integer> stateId);
}
我的问题是当我调用此Repo函数时,两个单个查询被触发而不是JOin查询。
我应该如何更改我的代码?