【问题标题】:Hibernate, Persistance and delation of associations休眠,持久性和关联删除
【发布时间】:2011-03-30 09:33:21
【问题描述】:

我对关联关系有一些问题,这是我的代码:

类模块版本

@ManyToMany(mappedBy="listModuleVersions")
private List<SuiteVersion> suiteVersions = new ArrayList<SuiteVersion>();

类套件版本

@ManyToMany()
private List<ModuleVersion> listModuleVersions = new ArrayList<ModuleVersion>();*

我希望能够通过删除关联自动删除套件版本或模块版本。例如,如果我使用此代码删除 SuiteVersion,则会自动删除 SuiteVersion 与 ModuleVersion 的关联。但是,如果我删除一个 moduleVersion,它不会自动删除 SuiteVersion -> ModuleVersion 的关联。可以使用 ModuleVersion 自动执行此操作吗?

更新

如果我删除一个添加了 CASCADETYPE.REMOVE 的模块,这是休眠执行的操作 ...

Hibernate: delete from SUITE_VERSION_MODULE_VERSION where suiteVersions_ID_SUITE_VERSION=?
Hibernate: delete from SUITE_VERSION_MODULE_VERSION where suiteVersions_ID_SUITE_VERSION=?
Hibernate: delete from SUITE_VERSION_MODULE_VERSION where suiteVersions_ID_SUITE_VERSION=?
Hibernate: delete from SUITE_VERSION_MODULE_VERSION where suiteVersions_ID_SUITE_VERSION=?
Hibernate: delete from SUITE_VERSION_MODULE_VERSION where suiteVersions_ID_SUITE_VERSION=?
//Hibernate: delete from SUITE_VERSION where ID_SUITE_VERSION=?
//Hibernate: delete from SUITE_VERSION where ID_SUITE_VERSION=?
Hibernate: delete from MODULE_VERSION where ID_MODULE_VERSION=?
//Hibernate: delete from SUITE_VERSION where ID_SUITE_VERSION=?
//Hibernate: delete from SUITE_VERSION where ID_SUITE_VERSION=?
Hibernate: delete from MODULE_VERSION where ID_MODULE_VERSION=?
//Hibernate: delete from SUITE_VERSION where ID_SUITE_VERSION=?
Hibernate: delete from MODULE_VERSION where ID_MODULE_VERSION=?
Hibernate: delete from MODULE where ID_MODULE=?

我只想执行 none comitted 部分。 提前感谢您的帮助,

更新2

我尝试使用此方法实现删除模块:

    public static void removeModule(Module module) {
    for(ModuleVersion moduleVersion : module.getListModuleVersion()){
        for(SuiteVersion suiteVersion : moduleVersion.getSuiteVersions()){
            sessionFactory = HibernateFactory.getSessionFactory();
            session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
            suiteVersion.removeFromModuleVersions(moduleVersion);
            transaction.commit();
        }
    }
    sessionFactory = HibernateFactory.getSessionFactory();
    session = sessionFactory.getCurrentSession();
    session.delete(module);
    transaction.commit();
}

但我现在有这个问题:

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException

最好的问候,

弗洛伦特。

P.S:我是法国人,对不起我的英语。

【问题讨论】:

    标签: java hibernate orm persistence


    【解决方案1】:

    谢谢你的回答。但如果我有这个。 suiteVersion 也被删除。我只想删除关联表中的行 suiteVersion -> moduleVersion。

    我想我现在明白了:您想打破一个关联并从连接表中删除相应的记录。这不是我从原始问题中理解的。总之……

    要做到这一点,你必须按照我写的来打破关联,即从它们各自的关联集合中删除相应的实体(你也必须更新链接的两侧,因为它是双向关联) .像这样的:

    session.getTransaction().begin();
    SuiteVersion suite = ... // retrieved in some way
    ModuleVersion module = ... // retrieved in some way
    suite.getModuleVersions().remove(module);
    module.getSuiteVersions().remove(suite);
    session.getTransaction().commit(); // the "association" record should get removed
    

    但我建议使用辅助方法:

    @Entity
    public class SuiteVersion {
        ...
        @ManyToMany
        private List<ModuleVersion> moduleVersions = new ArrayList<ModuleVersion>();
    
        ...
    
        public void removeFromModuleVersions(ModuleVersion module) {
            this.moduleVersions.remove(module);
            module.getSuiteVersions().remove(this);
        }
    
        public void addToModuleVersions(ModuleVersion module) {
            this.moduleVersions.add(module);
            module.getSuiteVersions().add(this);
        }
    
    }
    

    然后你的代码变成:

    session.getTransaction().begin();
    SuiteVersion suite = ... // retrieved in some way
    ModuleVersion module = ... // retrieved in some way
    suite.removeFromModuleVersions(module);
    session.getTransaction().commit();
    

    另见


    更新:您的“新”问题是一个纯 Java 相关问题:您不能在迭代集合时修改集合,至少不能使用您当前的方法。您需要使用IteratorIterator#remove()

    相关问题

    【讨论】:

    • 感谢您的回答。但如果我有这个。 suiteVersion 也被删除。我只想删除关联表中的行 suiteVersion -> moduleVersion。
    • @Delildor:我想我误解了你最初的问题,请参阅我的更新。
    • 我尝试测试您删除模块的方法,但我面临另一个问题。 (见更新2)。谢谢。
    • @Delildor 我在回答中提到了这个新问题,但请避免下次在一个问题中遗漏问题(您的新问题应该作为新问题发布,这是一个不相关的问题)。一个问题 = 一个问题。
    • 感谢您的回答。它解决了我的问题。下次,我会发布一个新问题,就像你注意到的那样。
    猜你喜欢
    • 1970-01-01
    • 2012-10-07
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 2010-10-29
    • 2015-05-31
    • 1970-01-01
    相关资源
    最近更新 更多