English 中文(简体)
休眠:如何将两个收藏字段指向相同的对象
原标题:Hibernate: how to have two collection fields pointing to same objects
  • 时间:2012-05-24 14:46:53
  •  标签:
  • hibernate

我试图将两个一对多个命令的收藏字段映射成相同的值。字段有不同的过滤要求。因此,一个字段将有一个项目从另一个字段中过滤出来。

这里列出实体的相关部分。

public class OptionValueSet extends ConfigurationDomainObject {
    ...

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = OPTION_VALUE_SET_ID_COLUMN, nullable = false)
    @IndexColumn(name = "option_index", nullable = false)
    @Where(clause = OBSOLETE_FILTER)
    private final List<OptionValue> allOptions = new java.util.ArrayList<OptionValue>();

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = OPTION_VALUE_SET_ID_COLUMN, nullable = false, insertable = false, updatable = false)
    @IndexColumn(name = "option_index", nullable = false)
    @Where(clause = OBSOLETE_FILTER + " AND disabled =  0 ")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @Immutable
    private final List<OptionValue> options = new java.util.ArrayList<OptionValue>();

    ...
}

和例外

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.casenet.domain.configuration.screen.OptionValue column: option_value_set_id (should be mapped with insert="false" update="false")
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:670)
    at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:692)
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:714)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:468)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:215)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1135)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1320)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
    ... 137 more

好奇的是, 我正在解开使用同一个冬眠字段的两个使用案例。 目前, 过滤器打开了一个来隐藏禁用选项Values, 剩下一个来编辑它们。 然而, @ Filter 注释干扰了缓存。 我必须在不改变 DB 基本方案的情况下解决这个问题。 我们在释放周期里太迟, 无法做更激进的事情 。

最佳回答

既然这些收藏还是热切地被获取, 为什么你不简单地添加一个获取选项( options) 的方法, 返回 < code> alloptions 的过滤视图( 或副本)?

public List<OptionValue> getOptions() {
    List<OptionValue> result = new ArrayList<OptionValue>(allOptions.size());
    for (OptionValue o : allOptions) {
        if (!o.isDisabled()) {
            result.add(o);
        }
    }
    return result;
}
问题回答

在冬眠的概念中,您已经两次绘制了相同的字段(在此情况下, 孩子列表; 如果您绘制了简单的属性, 您将得到相同的错误信息 两次 ) 。 休眠无法处理它, 因为更新或插入后状态可能无效( 如果在 Java 代码中绘制了两次的天体得到了不同的更新值, 那么冬眠应该怎么做? ) 。

如果两个字段中只有一个仅用于阅读,您可以两次绘制字段。如果是简单的属性,错误信息已经指示您如何操作( Indepte=“false”更新=“false”” )。对于子列表,您可能也必须为只读属性设置 cascade none





相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签