@Select
作用是属性实体对象的级联查询。不用 @Select
也可以通过 OneToMany
(cascadeSaveOrUpdate = false)来实现级联查询,但是外键只能参考id,不能是实体的其他属性。
Select.java
注解声明
public @interface Select {
String table();
String joinValue() default "";
String referencePropertyName() default "";
boolean oneToOne() default false;
}
下面的示例表示根据实体 Batch
的属性 materialId
去关联表mm_classification的外键字段material_id
做级联查询。
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Table(value = "mm_batch", comment = "物料批次主数据")
public class Batch extends BaseEntity {
private Long materialId;
@Select(table = "mm_classification", joinValue = "material_id", referencePropertyName = "materialId")
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private List<Classification> classificationList;
}
下面的示例 @Select
等价 OneToMany
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Table(value = "mm_batch", comment = "物料批次主数据")
public class Batch extends BaseEntity {
private Long materialId;
@Select(table = "mm_classification", joinValue = "batch_id", referencePropertyName = "id")
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private List<Classification> classificationList;
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Table(value = "mm_batch", comment = "物料批次主数据")
public class Batch extends BaseEntity {
private Long materialId;
@OneToMany(subTable = "mm_classification", joinValue = "batch_id")
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private List<Classification> classificationList;
}