3
votes

I have and audited entity with a foreign key to a class (which I don't want to audit):

@Entity
@Audited
public class CitaAgenda {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reconocimiento_id")
    @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
    private Reconocimiento reconocimiento;
    ...
}

Also, Reconocimiento is an entity with an embedded property:

@Entity
public class Reconocimiento {
     @Embedded
     private Enfermeria enfermeria;
     ...
}

And the embeddable class is as follows:

@Embeddable
public class Enfermeria {
    private boolean diabetes;
    ...
}

Now, when I bring the data from the revisions and fetch CitaAgenda, I get a

"Can not set boolean field ...Enfermeria.diabetes to null value".

What I think it is happening is that Hibernate tries to init the enfermeria property of Reconocimiento with NULL because it believes all fields are NULL as the only field of Reconocimiento stored in the _AUD table is the ID (as the others are not audited). But this is not true as if I audit the other fields, diabetes would be false and not NULL.

I cannot set diabetes to Boolean. Are there any other solutions? Thanks in advance.

1
can you add annotations to Enfemeria? - Maciej Kowalski
@Javi_Swift do you have a bi-directional mapping here? If so @NotAudited on Reconocimiento is what you want i believe - Eugene
@Eugene that is not my case, but thank you for trying - Javier Vazquez
@Javi_Swift if the solution below works for you, would you mind submitting a JIRA so that I can address this from an Envers perspective to avoid necessarily having to use a Converter ? - Naros
@Naros Link to the issue: hibernate.atlassian.net/browse/HHH-11429 (this is the first time I use JIRA so sorry if there is any mistake) - Javier Vazquez

1 Answers

2
votes

If you can add additional annotations to Enfemeria then you could mark it with a custom converter:

Embedded class

@Convert(converter=MyBooleanConverter.class)
private boolean diabetes;

Converter class

@Converter
public class MyBooleanConverter implements AttributeConverter<Boolean, Integer>{
    @Override
    public String convertToDatabaseColumn(Boolean value) {
        if (Boolean.TRUE.equals(value)) {
            return Integer.valueOf(1);
        } else {
            return Integer.valueOf(0);
        }
    }

    @Override
    public Boolean convertToEntityAttribute(Integer value) {
        if(value != null and value.equals(1){
            return Boolean.TRUE;
        }

        return Boolean.FALSE;
    }
}

Of course you may need to consider using String instead of Integer for converting. It depends on the underlying column data type in your database.