【发布时间】:2020-03-29 13:11:33
【问题描述】:
我有一个 Customers 表,其中包含 AddressEmbedded。
我还有一个硬编码表Countries,其中我已经为每个国家/地区设置了一个区域,国家/地区是主键。
我想加入AddressEmbedded 和Countries,所以我使用了ManyToOne 并将CountryEntity 放入AddressEmbedded。
但我收到了 mapstruct 无法生成 setCountry 的错误。
所以问题是,我如何制作AddressEmbedded.setCountry(string country)?
它应该调用 db 以获取该国家/地区的相应区域,但在 setter 中添加 db 调用似乎是错误的。
以下是实体定义:
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Data
@Embeddable
public class AddressEmbedded {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "country")
private CountryEntity country;
}
@Data
@Entity
@Table(name = "Countries")
public class CountryEntity {
@Id
@Column(name = "country")
private String country;
@Column(name = "region")
private String region;
}
@Data
@Entity
@Table(name = "Customers")
public class CustomerEntity {
@Embedded
private AddressEmbedded address;
}
【问题讨论】:
-
您将无法将
String传递给AddressEmbedded.setCountry。看起来您需要通过 ID (country) 查找CountryEntity,然后将其传递给AddressEmbedded.setCountry。 -
在 setter 中查找?
-
我个人认为将任何逻辑置于 getter/setter 是不好的做法。相关话题:stackoverflow.com/questions/44600536/…
-
当然,这就是我问这个问题的原因,我不想在setter中添加db调用。
标签: java spring-boot jpa entity