Search code examples
hibernatejpacompositeusertype

Can Hibernate handle overlapping columns in composite types?


There is a known hibernate bug https://hibernate.atlassian.net/browse/HHH-6221 where overlapping foreign keys triggers an AnnotationException. In @JoinColumns, you cannot have a mix of insertable and non-insertable @JoinColumn entries.

This has a workaround using @JoinColumnsOrFormulas, https://stackoverflow.com/a/13147366/16400385 .

The same bug is there for @Columns, but I can't find a @ColumnsOrFormulas annotation. Is there a similar workaround for @Columns ?

sample code

    @Columns( columns = {
        @Column(name = "currency_id", updatable = false, insertable = false),
        @Column(name = "fees")
    })
    private Money fees;

    @Columns( columns = {
            @Column(name = "currency_id"),
            @Column(name = "tax")
    })
    private Money tax;

the error I get is

'org.hibernate.AnnotationException: Mixing insertable and non insertable columns in a property is not allowed'

, same as HHH-6221


Solution

  • There is no workaround or anything for this as that kind of model simply is problematic/broken. One thing you could do is to map the individual columns in your entity and build a money instance through a @PostLoad listener.