【发布时间】: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)。例如客户 1 有 tarif #1 #2 #3 #4,customer #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);
}
}
【问题讨论】:
-
请同时粘贴异常跟踪。
-
您确定在调用 addTarifToCustomer 之前没有两个 Customer 类实例并且其中一个处于分离状态吗?这可能会导致 NonUniqueException。在使用 session.saveOrUpdate(); 调用 line 之前尝试使用你的 ide 调试器查看你有什么对象;
标签: mysql hibernate jakarta-ee database-design hibernate-mapping