使用 Java/Hibernate 归档

发布于 2024-10-11 02:43:54 字数 1664 浏览 6 评论 0原文

我有一个简单的对象,只需通过 hibernate 映射

class SimpleObject {
   private int id;
   private String textA;
   private String textB;
   private Date date;
   private Status status;
   //+getters/setters/other stuff
}

现在,由于我的表非常大(几百万个条目),我决定归档所有我并不真正需要的条目(用户稍后可能需要它们)上,通过检查一个选项,他们应该能够搜索实际的和存档的两个表,但这是我还不关心的事情,这将在其他地方完成)。
因此,我决定让我的映射文件尽可能简单,以及转换对象(存档/非存档)的方式并使用简单的继承,

abstract class AbstractSimpleObject {
    // idem SimpleObject
}

class SimpleObject extends AbstractSimpleObject {

}

class SimpleObjectArchived extends AbstractSimpleObject {

}

我在休眠映射中使用了union-subclass将 SimpleObject 映射到我的旧表,并将 SimpleObjectArchived 映射到相同的表。
到目前为止,一切都很好,我可以删除/创建/更新我的对象。 现在进行归档:

在我的 SimpleObjectBusinessRules 类中,我定义了一个方法归档:

class SimpleObjectBusinessRules {
    // the daos for the SimpleObject and the SimpleObjectArchived both using HibernateDaoSupport
    SODao soDao;
    SOADao soaDao;

    //...
    //you can say which objects to archive by some criterias
    public void archive(Map<String,Object> pCrit) {
        List<SimpleObject> lSOs = soDao.getByCriteria(pCrit);
        //I wrote myself a converter (based on dozer)
        List<SimpleObjectArchived> lSOAs = Converter.convertToSOA(lSOs);
        soDao.deleteAll(lSOs);
        soaDao.saveAll(lSOAs); //based on getHibernateTemplate().saveOrUpdateAll(pEntities)
    }
}

我省略了所有 try/catch/... 我在 saoDao.saveAll(...) 上遇到休眠异常 org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已与会话关联:[...]

有人知道如何解决此问题吗?或者任何人已经使用休眠归档并有更好的解决方案(甚至是如何使用休眠归档的工作解决方案)?

I have a simple object which is mapped simply by hibernate

class SimpleObject {
   private int id;
   private String textA;
   private String textB;
   private Date date;
   private Status status;
   //+getters/setters/other stuff
}

Now, since my table is gettig quite huge (a couple of millions of entries), I decided to archive all of the entries I don't really need (the user might need them later on, by checking an option they schould be able to search the two tables the actual and the archived, but that's a thing I don't carre about yet and which will be done somewhere else).
So I've decided to keep my mapping files as simple as possible and also the way of converting an object (archived/non-archived) and use simple heritage

abstract class AbstractSimpleObject {
    // idem SimpleObject
}

class SimpleObject extends AbstractSimpleObject {

}

class SimpleObjectArchived extends AbstractSimpleObject {

}

I used union-subclass in my hibernate mapping and mapped SimpleObject to my old table, and SimpleObjectArchived to an identical table.
Upto now everything is fine, I can delete/create/update my objects.
Now to the archiving:

In my SimpleObjectBusinessRules class I define a method archive:

class SimpleObjectBusinessRules {
    // the daos for the SimpleObject and the SimpleObjectArchived both using HibernateDaoSupport
    SODao soDao;
    SOADao soaDao;

    //...
    //you can say which objects to archive by some criterias
    public void archive(Map<String,Object> pCrit) {
        List<SimpleObject> lSOs = soDao.getByCriteria(pCrit);
        //I wrote myself a converter (based on dozer)
        List<SimpleObjectArchived> lSOAs = Converter.convertToSOA(lSOs);
        soDao.deleteAll(lSOs);
        soaDao.saveAll(lSOAs); //based on getHibernateTemplate().saveOrUpdateAll(pEntities)
    }
}

I ommited all the try/catch/...
I get an hibernate exception on saoDao.saveAll(...)
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:[...]

Anyone any idea how to solve this problem? Or anyone already treated archiving with hibernate and has a better solution (or even a working solution how to archive with hibernate)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话难免假 2024-10-18 02:43:54

我只需创建两个会话,一个业务逻辑会话和一个存档会话,从业务逻辑会话中删除对象并将转换后的对象保存到存档会话中。您可以在 DAO 中实现归档方法。这将为您省去很多麻烦。

我不会像您那样用归档逻辑污染我的完整继承层次结构,除非它在您的业务逻辑中的其他地方使用(它可能不是)。只需使用两个会话并将存档记录放入另一个模式或数据库或表中(会话的映射由您决定)。

顺便说一句:由于您的映射策略而发生异常。通过联合子类映射,所有标识符都存储在同一个表中,因此存档记录和业务逻辑记录都从同一个池中获取 ID。您需要自定义生成策略来避免该异常或更改映射。但是,当有一个更优雅的解决方案可以解决您的问题(存档会话)时,为什么还要费心呢?

I'd just create two sessions, one business-logic session and one archive-session, delete the object from the business logic session and save the converted object to the archive-session. You could implement an archive method in your DAO. This will spare you a lot of trouble.

I would not go as far as you and pollute my complete inheritance hierarchy with the archiving logic, unless it's used elsewhere in your business-logic (it probably isn't). Just use two session and put the archived records in another schema or database or table (the mapping for the session is up to you).

BTW: The exception occurs due to your mapping strategy. With a Union-Subclass-Mapping all the identifiers are stored within the same table, so the archive records and the business logic records are fed IDs from the same pool. You'd need a custom generation strategy to avoid that exception or change the mapping. But why bother with it when there's a more elegant solution to your problem (archiving session)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文