hibernate在多对多关系中自动删除数据库数据

时间:2016-01-15 11:54:34

标签: java hibernate many-to-many

我有一个使用Struts2SpringHibernate的应用程序。我有一个Many-to-Many关系的实体。当我尝试检索数据时,Hibernate删除db中的所有数据。在日志文件中我发现了这个:

Hibernate: select composantc0_.commandeFournisseur_id as commande2_3_1_, composantc0_.composantCommandeClient_id as composan1_0_1_, composantc1_.id as id1_4_0_, composantc1_.commandeClients_id as commande2_4_0_, composantc1_.medicamentsFournisseurs_id as medicame3_4_0_, composantc1_.quantite as quantite4_4_0_, composantc1_.dateCommande as dateComm5_4_0_, composantc1_.presentation as presenta6_4_0_, composantc1_.prix as prix7_4_0_ from commandefournisseur_has_composantcommandeclient composantc0_ inner join salama.composantcommandeclient composantc1_ on composantc0_.composantCommandeClient_id=composantc1_.id where composantc0_.commandeFournisseur_id=?
Hibernate: delete from commandefournisseur_has_composantcommandeclient where commandeFournisseur_id=?
Hibernate: select medicament0_.id as id1_6_0_, medicament0_.fournisseurs_id as fourniss2_6_0_, medicament0_.prix as prix3_6_0_, medicament0_.designation as designat4_6_0_, medicament0_.laboratoire as laborato5_6_0_, medicament0_.datePeremption as datePere6_6_0_, medicament0_.tva as tva7_6_0_, medicament0_.disponible as disponib8_6_0_ from salama.medicamentfournisseur medicament0_ where medicament0_.id=?
Hibernate: select medicament0_.id as id1_6_0_, medicament0_.fournisseurs_id as fourniss2_6_0_, medicament0_.prix as prix3_6_0_, medicament0_.designation as designat4_6_0_, medicament0_.laboratoire as laborato5_6_0_, medicament0_.datePeremption as datePere6_6_0_, medicament0_.tva as tva7_6_0_, medicament0_.disponible as disponib8_6_0_ from salama.medicamentfournisseur medicament0_ where medicament0_.id=?

Commandefournisseur的关系

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "commandefournisseur_has_composantcommandeclient", catalog = "salama", joinColumns = {
    @JoinColumn(name = "commandeFournisseur_id", nullable = false, updatable = false)}, inverseJoinColumns = {
    @JoinColumn(name = "composantCommandeClient_id", nullable = false, updatable = false)})
@JsonIgnore
public Set getComposantcommandeclients() {
    return this.composantcommandeclients;
}

在composantCommandeClient

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "commandefournisseur_has_composantcommandeclient", catalog = "salama", joinColumns = {
    @JoinColumn(name = "composantCommandeClient_id", nullable = false, updatable = false)}, inverseJoinColumns = {
    @JoinColumn(name = "commandeFournisseur_id", nullable = false, updatable = false)})
@JsonIgnore
public Set getCommandefournisseurs() {
    return this.commandefournisseurs;
}

我的Struts2动作

public String details() {
    commandefournisseur = commandefournisseurService.find(commandeFournisseurId);
    return SUCCESS;
}

我的道法

public T find(Integer id)  {
    return (T) getHibernateTemplate().get(getEntityClass(), id);
}

在我看来

<s:iterator value="commandefournisseur.composantcommandeclients">
                            <tr>
                                <td><s:property value="medicamentfournisseur.designation" /></td>
                                <td><s:property value="prix" /></td>
                                <td><s:property value="quantite" /></td>
                            </tr>
                        </s:iterator>

有人知道如何禁用此自动删除功能吗?我正在使用hibernate 4.2.7.Final

编辑:当我使用eager而不是延迟加载时,它可以工作。那么为什么在延迟加载中删除数据呢?

0 个答案:

没有答案