How to remove Hibernate @Embeddables containing nullable fields?

When deleting the @Embeddable object, I run into some problems.

I have the following domain classes: SwitchVoipTrunkGroup and PrioritizedCodec. The latter contains several fields that may be null.

class SwitchVoipTrunkGroup {

    //...

    @CollectionOfElements(fetch = FetchType.LAZY)
    @JoinTable(
            name = "SWITCH_VOIP_TKG_CODEC",
            joinColumns = @JoinColumn(name = "FK_SWITCH_VOIP_TKG_ID")
    )
    @ForeignKey(name = "FK_CODEC_SWITCH_VOIP_TKG")
    private Set<PrioritizedCodec> prioritizedCodecs = new HashSet<PrioritizedCodec>();

    //...
}

@Embeddable
public class PrioritizedCodec {

    @Column(name = "PRIORITY")
    private String priority;

    @Column(name = "FAX_MODE")
    private String faxMode;

    //... some more columns ...
}

When I edit the PrioritizedCodecs SwitchVoipTrunkGroup field (for example, deleting an entry) and saving the object, I see the following in the Hibernate log:

13:54:31,919 INFO  [STDOUT] Hibernate: delete from T_SWITCH_VOIP_TKG_CODEC where 
fk_switch_voip_tkg_id=? and fax_mode=? and priority=?

From this question, I understand why Hibernate uses all the fields in the where clause. However, this gives problems: if some of these fields are empty, the query will look like this:

delete from T_SWITCH_VOIP_TKG_CODEC where fk_switch_voip_tkg_id=1 and fax_mode = '' 
and priority =''

However, this will not delete any entries, since it is really necessary for Hibernate to check NULL iso for an empty string. For instance:

delete from T_SWITCH_VOIP_TKG_CODEC where fk_switch_voip_tkg_id=1 and fax_mode
IS NULL and priority IS NULL

(. , )

, ? !

+3
1

, , "--" SwitchVoipTrunkGroup PrioritizedCodec, , Hibernate PrioritizedCodec, SwitchVoipTrungGroup.

@Entity
class SwitchVoipTrunkGroup {

    //...

    @OneToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, orphanRemoval = true)
    @JoinColumn(name = "switchVoipTrunkGroup_id")
    @ForeignKey(name = "FK_PrioritizedCodec_SwitchVoipTrunkGroup")
    private Set<PrioritizedCodec> prioritizedCodecs = new HashSet<PrioritizedCodec>();

    //...
}

@Entity
public class PrioritizedCodec {

        @Column(name = "PRIORITY")
        private String priority;

        @Column(name = "FAX_MODE")
        private String faxMode;

        //... some more columns ...
    }

@Serice("someService")
public class SomeService  {

     @Autowired 
     private SwitchVoipTrunkGroupDao trunkDao;

     public SwitchVoipTrunkGroup doOperation("criteria") {
         SwitchVoipTrunkGroup tg = trunkDao.find("criteroa");
         tg.getPrioritizedCodecs().[remove(2)]; //remove should be implemened, that is just lame statement
         tg.getPrioritizedCodecs().get(5).setFaxMod("ENABLED");
         return trunkDao.save(tg); //hibernate will remove missing elements from PrioritizedCodec table, and will update necessary entities.
    }
}

priority faxMode @Column

@Column(columnDefinition = "VARCHAR(20) default 'NONE'", nullable = false)
private String faxMode;
+1

Source: https://habr.com/ru/post/1776238/


All Articles