【问题标题】:Why do I get a Hibernate NonUniqueObject Exception?为什么会出现 Hibernate NonUniqueObject 异常?
【发布时间】:2012-08-25 10:27:16
【问题描述】:

我试图弄清楚为什么我很难调试休眠错误,并且我已将触发行为缩小到当我尝试保存将在另一个客户中具有一个 taris 子集的客户对象时。以下是精简的类和 hbm 映射:

**<POJOs>**

public class Tarif {
  private Long idTarifRailingLTD;
  private String name;
  private Set<Customer> customers=new HashSet<Customer>();
}

public class Customer extends Society {
    private Company company;
    private boolean actif;
    private String tvaNumber;
    private Set<Tarif> tarifRailings = new HashSet<Tarif>();
}


**<HBMs>**    
  
<class name="comp.model.accounting.Tarif" table="Tarif" lazy="false">
    <id name="idTarif" type="long">
        <generator class="native"/>
    </id>
        
    <property name="name" length="50" not-null="true"/>
        
    <set name="customers"  table="customer_tarifrailing" inverse="true" lazy="false" fetch="select" cascade="all">
        <key column="idTarif"/>
        <many-to-many class="comp.model.customer.Customer" column="idCustomer"/>
    </set>
        
</class>

<joined-subclass name="comp.model.customer.Customer" table="customer" lazy="false">
    <key column="idSociety"/>
    <property name="actif" type="boolean"/>
    <property name="tvaNumber" length="80"/>
                
    <many-to-one name="company" class="comp.model.company.Company" column="idCompany" 
                             not-null="true"/>  
                
    <set name="tarifRailings"  table="customer_tarif" inverse="false" lazy="false" fetch="select" cascade="all">
        <key column="idCustomer"/>
        <many-to-many class="comp.model.accounting.Tarif" column="idTarif"/>
    </set>
                
</joined-subclass>

触发行为是,当我尝试使用一组费率来持久化客户时,该组费率在另一个客户的费率中具有子集(子集的数量必须大于 1)。例如客户 1tarif #1 #2 #3 #4customer #2 可以有 tarifs #1 #5 # 6 #7,但不是 tarif #1 #2 #5 #6,因为 #1 #2#1 #2 # 的子集3 #4。我现在不确定为什么会这样。

    **<The Function>**
    public static void addTarifToCustomer(Customer customer, Tarif tarif) throws UserServiceException, Exception {
    
                // Begin unit of work
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
                session.beginTransaction();
                    
                customer.addTarifRailing(tarifRailing);
                    session.saveOrUpdate(customer);
                entKeys = session.getStatistics().getEntityKeys();
                // End unit of work
                session.getTransaction().commit();
                
            } catch (ObjectNotFoundException ex) {
                HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
                throw new NotFoundDAOException("L'object d'identifiant " + customer.getIdSociety()
                        + " n'existe pas dans la base", ex);
            } catch (ConstraintViolationException ex) {
                HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
                throw new AlreadyExistDAOException("Nouvel identifiant déja existant en base", ex);
            } catch (Exception ex) {
                ex.printStackTrace();
                HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
                throw new Exception(ex);
            }
        }

Stack trace here

【问题讨论】:

  • 请同时粘贴异常跟踪。
  • 您确定在调用 addTarifToCustomer 之前没有两个 Customer 类实例并且其中一个处于分离状态吗?这可能会导致 NonUniqueException。在使用 session.saveOrUpdate(); 调用 line 之前尝试使用你的 ide 调试器查看你有什么对象;

标签: mysql hibernate jakarta-ee database-design hibernate-mapping


【解决方案1】:

@Luckasz 我已经从堆栈跟踪中知道它是这样的,但是由于这是一个继承的项目,因此很难确定是否加载了另一个实例以及最终我不得不通过在操作层(控制器)中持久化/更新对象,而不是通过 DAO 层。
非常感谢 irc 频道的 sebersole 帮助我找到解决方法。

【讨论】:

  • 是的,您继承的 DAO 内容非常混乱。恕我直言,这实际上是一个很好的例子,说明 DAO 类经常使使用 O/RM 更加变得困难。
猜你喜欢
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 2010-11-06
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 1970-01-01
相关资源
最近更新 更多