我试图弄清楚为什么我很难调试休眠错误,并且我已将触发行为缩小到当我尝试保存将在另一个客户中具有一个 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,客户 #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);
}
}