Hibernate不会删除/更新一对多(Hibernate not deleting/updating one to many)

编程入门 行业动态 更新时间:2024-10-27 15:15:18
Hibernate不会删除/更新一对多(Hibernate not deleting/updating one to many)

我正在努力学习如何使用hibernate,直到现在我还以为我做得很好......问题是,我有一对多的关系,我无法更新/删除。 我的数据库非常基础,我有一个ClientsBasic与IndirectClients有一对多的关系(它只有一个ClientsBasic ID和一个URL,这两个键因为你可以拥有相同的ID大量的URL)

ClientBasic:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "clientsBasic", cascade=CascadeType.ALL) public List<IndirectClients> getIndirectClients() { return this.indirectClients; } public void setIndirectClients(List<IndirectClients> indirectClients) { // this.indirectClients = indirectClients; this.indirectClients.clear(); this.indirectClients.addAll(indirectClients); }

ClientDao:

public ClientsBasic save(ClientsBasic client) throws HibernateException { Transaction tx = null; tx = session.beginTransaction(); session.saveOrUpdate(client); tx.commit(); log.info("Client saved with id: " + client.getClientId()); return client; }

现在,如果我尝试删除ClientsBasic,它将删除ClientsBasic和所有相关的indirectClients,因此它按预期工作,但如果我只是尝试更新/删除并在indirectClients中输入它不起作用。

示例:我创建了一个新客户端

ClientsBasic cb = new ClientsBasic("company_1", 1234, "company_1@email.com"); cbDao.save(cb);

然后是一个新的间接客户端

List<IndirectClients> indirectClientsSet= new ArrayList<IndirectClients>(); indirectClientsSet.add(new IndirectClients(new IndirectClientsId(cb.getClientId(), "www.url.test_1.com"), cb)); cb.setIndirectClients(indirectClientsSet); cbDao.save(cb);

现在,如果我尝试更改这样的网址

ClientsBasic cb = cbDao.findClientById(1); List<IndirectClients> indC = cb.getIndirectClients(); indC.get(0).getId().setUrl("TEST"); cb.setIndirectClients(indC); cbDao.save(cb);

数据库中没有任何更改。

有人可以帮帮我吗? 谢谢。

I am trying to learn how to work with hibernate, and until now i thought i was doing ok... The problem is, i have a one to many relationship that i can't update/delete. My DB is pretty basic, i have a ClientsBasic that has a one to many relationship with IndirectClients (which simply has a ClientsBasic ID and a URL, both keys because you can have for the same ID lots of URLs)

ClientBasic:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "clientsBasic", cascade=CascadeType.ALL) public List<IndirectClients> getIndirectClients() { return this.indirectClients; } public void setIndirectClients(List<IndirectClients> indirectClients) { // this.indirectClients = indirectClients; this.indirectClients.clear(); this.indirectClients.addAll(indirectClients); }

ClientDao:

public ClientsBasic save(ClientsBasic client) throws HibernateException { Transaction tx = null; tx = session.beginTransaction(); session.saveOrUpdate(client); tx.commit(); log.info("Client saved with id: " + client.getClientId()); return client; }

Now if i try to delete ClientsBasic, it will delete both ClientsBasic and all related indirectClients, so its working as expected, but if i simply try to update/delete and entry in indirectClients it doesn't work.

Example: I create a new Client

ClientsBasic cb = new ClientsBasic("company_1", 1234, "company_1@email.com"); cbDao.save(cb);

And then a new Indirect Client

List<IndirectClients> indirectClientsSet= new ArrayList<IndirectClients>(); indirectClientsSet.add(new IndirectClients(new IndirectClientsId(cb.getClientId(), "www.url.test_1.com"), cb)); cb.setIndirectClients(indirectClientsSet); cbDao.save(cb);

Now if i try to change the url like this

ClientsBasic cb = cbDao.findClientById(1); List<IndirectClients> indC = cb.getIndirectClients(); indC.get(0).getId().setUrl("TEST"); cb.setIndirectClients(indC); cbDao.save(cb);

no changes are made in the DB.

Can someone please help me? Thank you.

最满意答案

如果您的IndirectClients被定义为实体,它有自己的生命周期,这意味着您必须与ClientBasic父项分开地持久保存/删除实例。 如果您想要通过父关系管理所有子项的场景,请考虑使用ElementCollection 。

另请参见JPA:何时选择多值关联与元素集合映射

If your IndirectClients is defined as an Entity it has its own life cycle, meaning you have to persist/delete instances separately from their ClientBasic parent. If you want a scenario where all children are managed through their parent relation, consider using ElementCollection.

See also JPA: When to choose Multivalued Association vs. Element Collection Mapping

更多推荐

本文发布于:2023-04-28 14:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1332446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Hibernate   deleting   updating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!