[hibernate-dev] Bytecode enhancement and inheritance

Guillaume Smet guillaume.smet at gmail.com
Mon Jun 11 13:34:31 EDT 2018


Hi Luis,

This question is related to https://hibernate.atlassian.net/browse/HHH-12601
.

AFAICS, the following case (e.g. having the fields defined in the parent
class and the laziness defined in the child class) is not working very well
with bytecode enhancement:
====
@Entity
public class RequestWithLazyEvents extends RelatedToEvents {

    @ManyToMany(fetch = FetchType.LAZY)
    @Fetch(FetchMode.SELECT)
    public Set<Event> getEvents() {
        return events;
    }
}

@MappedSuperclass
public abstract class RelatedToEvents extends Base {
    protected Set<Event> events = new LinkedHashSet<>();

    public void setEvents(Set<Event> events) {
        this.events = events;
    }
}
====

By not working very well, I mean that the interceptor read/write methods
are defined in the child class but the getters and setters are not
intercepted because we only intercept the getters/setters if the fields are
defined in the current class:

See this method in PersistentAttributeTransformer and especially the last
test:
====
    private boolean isEnhanced(String owner, String name, String desc) {
        for ( FieldDescription enhancedField : enhancedFields ) {
            if ( enhancedField.getName().equals( name )
                    && enhancedField.getDescriptor().equals( desc )
                    &&
enhancedField.getDeclaringType().asErasure().getInternalName().equals(
owner ) ) {
                return true;
            }
        }
        return false;
    }
====

(Note that supporting this case is not only a matter of removing the test)

I don't know if it's a case we want to support or not but I'm a bit worried
that we don't throw any error and the collection is not loaded at all (as
the rest of ORM supposes the enhancer will do the work).

You end up saving the collection in the database and when you get it back
the collection is always empty.

It can lead to serious data loss if you save the entity back to the
database thus I think it's somewhat critical to fix it.

WDYT?

-- 
Guillaume


More information about the hibernate-dev mailing list