- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有 4 张 table 。
国家/地区(id、名称)、CountryType(id、名称)、客户端(id、名称)和Country_CountryType_Client关系表(country_id、countryType_id、client_id)。
这是我的国家/地区类别
:
@GeneratePojoBuilder(
intoPackage = "*.builder")
@Entity
@Table(name = "MD_COUNTRY")
@SequenceGenerator(
name = "SEQ_MD_COUNTRY",
sequenceName = "SEQ_MD_COUNTRY",
allocationSize = 1)
public class Country implements Serializable {
private static final long serialVersionUID = -3313476149373055743L;
private Long md_country_id;
private String nameKey;
private List<CountryCountryTypeClient> cCTypeClients;
@Id
@GeneratedValue(
generator = "SEQ_MD_COUNTRY")
@Column(
name = "MD_COUNTRY_ID")
public Long getMd_country_id() {
return md_country_id;
}
public void setMd_country_id(Long md_country_id) {
this.md_country_id = md_country_id;
}
@Column(name = "MD_COUNTRY_NAME_KEY")
public String getNameKey() {
return this.nameKey;
}
public void setNameKey(String name) {
this.nameKey = name;
}
@OneToMany(fetch=FetchType.EAGER,mappedBy="pk.country",cascade=CascadeType.ALL)
public List<CountryCountryTypeClient> getCountryCountryTypeClient() {
return cCTypeClients;
}
public void setCountryCountryTypes(List<CountryCountryTypeClient> countryCountryTypeClient) {
this.cCTypeClient = countryCountryTypeClient;
}
/* ... hashCode and equals methods..*/
CountryType
和 Client 类
看起来相同。
这是我的CountryCountryTypeClient
类:
@GeneratePojoBuilder(
intoPackage = "*.builder")
@Entity
@Table(
name = "COUNTRY_COUNTRY_TYPE_CLIENT")
@AssociationOverrides({
@AssociationOverride(name= "pk.country",
joinColumns=@JoinColumn(name = "COUNTRY_ID")),
@AssociationOverride(name="pk.countryType",
joinColumns=@JoinColumn(name = "COUNTRY_TYPE_ID")),
@AssociationOverride(name="pk.client",
joinColumns=@JoinColumn(name = "CLIENT_ID"))
})
public class CountryCountryTypeClient implements Serializable{
private static final long serialVersionUID = -879391903880384781L;
private CountryCountryTypeClientPK pk = new CountryCountryTypeClientPK();
public CountryCountryTypeClient() {}
@EmbeddedId
public CountryCountryTypeClientPK getPk() {
return pk;
}
public void setPk(CountryCountryTypeClientPK pk) {
this.pk = pk;
}
@Transient
public Country getCountry(){
return getPk().getCountry();
}
public void setCountry(Country country) {
getPk().setCountry(country);
}
@Transient
public CountryType getCountryType(){
return getPk().getCountryType();
}
public void setCountryType(CountryType countryType) {
getPk().setCountryType(countryType);
}
@Transient
public Client getClient() {
return getPk().getClient();
}
public void setClient(Client client) {
getPk().setClient(client);
}
/* ... hashCode and equals ... */
这是我的CountryCountryTypeClientPK
类:
@Embeddable
public class CountryCountryTypeClientPK implements Serializable {
private static final long serialVersionUID = -3934592006396010170L;
private Country country;
private CountryType countryType;
private Client client;
public CountryCountryTypeClientPK() {}
@ManyToOne
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
@ManyToOne
public CountryType getCountryType() {
return countryType;
}
public void setCountryType(CountryType countryType) {
this.countryType = countryType;
}
@ManyToOne
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
/*... hashCode and equals methods ..*/
我的CountryCountryTypeClientRepository
类:
public interface CountryCountryTypeRepository extends JpaRepository<CountryCountryTypeClient, CountryCountryTypeClientPK> {}
对于我的 Country 类,我有带有 saveCountry 方法的 CountryService 类:
public Country saveCountry(final Country dtoCountry) {
//save NEW Country
if(dtoCountry.getId()==null){
de.bonprix.global.masterdata.model.Country modelWithoutID = convertToModel(dtoCountry);
for (CountryCountryTypeClient cCTypeClient : modelWithoutID.getCountryCountryTypeClients()) {
cCTypeClient.setCountry(modelWithoutID);
}
return convertToDTO(this.countryRepository.saveAndFlush(modelWithoutID));
}
//save EDITED Country
else if (!(dtoCountry.getId()==null)){
de.bonprix.global.masterdata.model.Country modelWithID = convertToModel(dtoCountry);
for (CountryCountryTypeClient cCTypeClient : modelWithID.getCountryCountryTypeClients()) {
cCTypeClient.setCountry(modelWithID);
ccTypeClientRepository.delete(cCTypeClient);
}
return convertToDTO(this.countryRepository.saveAndFlush(modelWithID));
}
return null;
}
问题是:如何按 Country_ID 从 Country_CountryType_Client 表中删除所有行。不是通过 PK,而是通过 Country_ID。当我在国家/地区表中保存国家/地区时,Country_CountryType_Client 会自动更新为相应的值。
小例子,只是为了解决当前的问题。现在我的 Country_CountryType_Client 表中有这个。
现在我想保存除最后一行 (298-2-9) 之外具有所有相同关系的 NewCountry。我的NewCountry对(298-2-9关系)一无所知。在保存之前,我必须删除所有 id 为 298 的行。
希望问题清楚。
最佳答案
我不太明白这个问题。似乎您真正想做的就是从标识符为 298 的国家/地区中删除单个 CountryCountryTypeClient。
因此,如果您要按照以下概述更新映射:
https://docs.oracle.com/cd/E19798-01/821-1841/giqxy/
@OneToMany(fetch=FetchType.EAGER,mappedBy="pk.country",cascade=CascadeType.ALL, orphanRemoval = true)
public List<CountryCountryTypeClient> getCountryCountryTypeClient() {
return cCTypeClients;
}
然后您可以简单地执行以下操作:
Country country = // the country with id 298
CountryCountryTypeClient client = // the client with id 298/2/9
country.getCountryCountryTypeClient().remove(client);
countryRepository.save(country);
关于java - 按主键删除具有复合键的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41180672/
我听说过两种数据库架构。 大师级 主从 master-master不是更适合现在的web吗,因为它就像Git一样,每个单元都有整套数据,如果一个宕机也无所谓。 主从让我想起了 SVN(我不喜欢它),你
我们当前将 MySQL 配置为支持故障转移:Site1 Site2。当它们被设置为主/主时。在给定时间点,应用程序服务器仅主动写入一个站点。我们想要设置一个新的故障转移站点。然后我们将拥有 Site
我听说过两种数据库架构。 大师-大师 主从 master-master 不是更适合当今的网络吗,因为它就像 Git,每个单元都有整套数据,如果其中一个发生故障,也没关系。 主从让我想起 SVN(我不喜
我正在创建一个标记为类别的表,其中主类别(父列)包含 0,子类别包含父类别的 ID。我听说这叫引用。我的问题:这张表的结构正确吗?或者是否有更好的方法,例如实现遍历树或类似方法? CREATE TAB
我正在阅读一份关于 C++ 与 C 的文档。该文档说与 C 相比,C++ 编写得非常紧凑。一个例子是,C 允许 main() 函数类型为 void。另一方面,C++ 不允许这样做,他给出了标准中的以下
C main函数和Java main函数有什么区别? int main( int argc, const char* argv[] ) 对比 public static void main(Strin
我一直摸不着头脑,但运气不好。设计器有一个包含 3 栏的站点、两个侧边栏和一个主要内容区域。 专为桌面设计,左栏、主要内容、右栏。但是,在较小的设备上,我们希望首先堆叠主要内容。 所以通常情况下,你可
我一直在阅读有关 Jenkins 主/从配置的信息,但我仍然有一些问题: 是不是真的没有像 Jenkins 主站那样安装和启动从站 Jenkins?我假设我会以相同的方式安装一个主 Jenkins 和
据我了解,Viemodel中MVVM背后的概念包括业务逻辑和/或诸如暴露于 View 的数据的主/明细关系之类的事物 因此,正如我发现的那样,有很多ORM生成器,例如模型的telerik a.o以及另
我们有一个群集,其中包含3个主分区,每个主分区有2个副本。主/副本分片的总文档数相同;但是,对于同一查询/文档,我们得到3个不同的分数。当我们将preference = primary添加为查询参数时
我有一个非常大/旧/长时间运行的项目,它使用相对于启动目录的路径访问文件资源(即应用程序仅在从特定目录启动时才工作)。当我需要调试程序时,我可以从 eclipse 启动它并使用“运行配置”->->“工
谁能向我解释一下为什么我在这段代码上遇到段错误?我一直试图弄清楚这一点,但在各种搜索中却一无所获。当我运行代码而不调用 main(argc, argv) 时,它会运行。 Slave 仅将 argv 中
使用 xcode 中的默认项目作为主从应用程序,如果我在折叠委托(delegate)中放置 print 调试语句,当我旋转设备时它似乎永远不会被触发(事实上我永远无法触发它)。 我编辑的代码位于 Ap
是否有任何产品可以使 mysql 主/从故障转移过程更容易?一些可以自动发生的事情,而不是手动修复它。 最佳答案 [...稍后...;) 你所说的“更容易”是什么?MySQL 有很多解决方案: MyS
我有两个 mysql 数据库。我想做主/主复制。 复制以一种方式进行。然而,反过来说却不然。该错误表明它无法与用户“test@IPADDRESS”连接。 如何将用户名更改为 repl?从未进行过测试,
我正在尝试在 MySQL 中运行以下查询: GRANT REPLICATION SLAVE ON *.* TO 'replication'@’10.141.2.%’ IDENTIFIED BY ‘sl
我正在尝试使用 Android 提供的主/详细流程模板创建一个应用程序,并且我正在尝试将多个操作栏菜单项添加到操作栏的主要部分和详细信息部分。这就是我要实现的目标: (来源:softwarecrew.
我正在寻找一个跨平台的 C++ master/worker 库或工作队列库。一般的想法是我的应用程序将创建某种任务或工作对象,将它们传递给工作主机或工作队列,这将依次在单独的线程或进程中执行工作。为了
我似乎看到很多人在他们的 MySQL 模式中任意分配大尺寸的主/外键字段,例如 INT(11) 甚至 WordPress 使用的 BIGINT(20)。 如果我错了,请纠正我,但即使是 INT(4)
如果我有一个可以与多个键相关联的用户,正确的表设置应该是: 一个表有两列,例如: UserName | Key 没有主键且用户可以有多行,或者: 具有匹配标识符的两个表 Table 1 Us
我是一名优秀的程序员,十分优秀!