39

What I want:

@Embedded(nullable = false)
private Direito direito;

However, as you know there's no such attribute to @Embeddable.

Is there a correct way to do this? I don't want workarounds.

André Chalella
  • 13,788
  • 10
  • 54
  • 62

5 Answers5

43

Embeddable components (or composite elements, whatever you want to call them) usually contain more than one property and thus are mapped to more than one column. The entire component being null can therefore be treated in different ways; J2EE spec does not dictate one way or another.

Hibernate considers component to be NULL if all its properties are NULL (and vice versa). You can therefore declare one (any) of the properties to be not null (either within @Embeddable or as part of @AttributeOverride on @Embedded) to achieve what you want.

Alternatively, if you're using Hibernate Validator you can annotate your property with @NotNull although this will only result in app-level check, not db-level.

ChssPly76
  • 99,456
  • 24
  • 206
  • 195
28

It is possible to use "hibernate.create_empty_composites.enabled" since Hibernate 5.1 to change this behavior (see https://hibernate.atlassian.net/browse/HHH-7610 )

user158037
  • 2,659
  • 1
  • 24
  • 27
  • This applies to all Embeddables, which is probably not what you want. With sufficient finesse, I can imagine requiring both scenarios depending on the situation. Not the right answer at all. – gary Oct 25 '19 at 12:23
  • Keep in mind: *An experimental feature has been enabled (hibernate.create_empty_composites.enabled=true) that instantiates empty composite/embedded objects when all of its attribute values are null. This feature has known issues and should not be used in production until it is stabilized. See Hibernate Jira issue HHH-11936 for details.* - Hibernate 5.4.32.Final – George Z. Jul 02 '21 at 20:14
15

Add dummy field into class which is marked @Embeddable.

@Formula("0")
private int dummy;

See https://issues.jboss.org/browse/HIBERNATE-50 .

Tomáš Záluský
  • 10,735
  • 2
  • 36
  • 64
1

I wasn't too thrilled with either of the suggestions previously made, so I created an aspect that would handle this for me.

This isn't fully tested, and definitely not tested against Collections of embedded objects, so buyer-beware. However, seems to work for me so far.

Basically, intercepts the getter to the @Embedded field and ensures that the field is populated.

public aspect NonNullEmbedded {

    // define a pointcut for any getter method of a field with @Embedded of type Validity with any name in com.ia.domain package
    pointcut embeddedGetter() : get( @javax.persistence.Embedded * com.company.model..* );


    /**
     * Advice to run before any Embedded getter.
     * Checks if the field is null.  If it is, then it automatically instantiates the Embedded object.
     */
    Object around() : embeddedGetter(){
        Object value = proceed();

        // check if null.  If so, then instantiate the object and assign it to the model.
        // Otherwise just return the value retrieved.
        if( value == null ){
            String fieldName = thisJoinPoint.getSignature().getName();
            Object obj = thisJoinPoint.getThis();

            // check to see if the obj has the field already defined or is null
            try{
                Field field = obj.getClass().getDeclaredField(fieldName);
                Class clazz = field.getType();
                value = clazz.newInstance();
                field.setAccessible(true);
                field.set(obj, value );
            }
            catch( NoSuchFieldException | IllegalAccessException | InstantiationException e){
                e.printStackTrace();
            }
        }

        return value;
    }
}
Eric B.
  • 23,425
  • 50
  • 169
  • 316
1

You can use a nullsafe getter.

public Direito getDireito() {
    if (direito == null) {
        direito = new Direito();
    }
    return direito;
}
Dominik Hadl
  • 3,609
  • 3
  • 24
  • 58
  • 8
    Please be aware that this will cause your entity to be marked as dirty on every read causing Hibernate to increase the version. This will lead to optimistic locking exceptions. – Arlo Nov 27 '14 at 12:53
  • This solution trigger the sql update instruction to the database server, witch brings problems like concurrency between threads and performance issues. – Felipe Cardoso Martins Mar 05 '15 at 03:36