When using @Inheritance and @DiscriminatorColumn, the value from discriminator column don't get updated when the concrete class change

404 views
Skip to first unread message

Lucas Dillmann

unread,
Aug 3, 2018, 11:03:51 AM8/3/18
to Ebean ORM
Hello,

We are facing an issue here where the Ebean don't update the column in database described by @DiscriminatorColumn in a scenario where the entities are using @Inheritance with single table strategy whenever the entity class change.

To explain this better, consider this following classes (abbreviated to save space):

Super class

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "attribute_type", length = 50)
public abstract class MetaAttribute {
    private UUID id;
}

Entity implementation 1

@Entity
@DiscriminatorValue(AttributeType.BOOLEAN_ATTRIBUTE)
public class BooleanAttribute extends MetaAttribute {
    private boolean defaultBooleanValue;
}

Entity implementation 2

@Entity
@DiscriminatorValue(AttributeType.TEXT_ATTRIBUTE)
public class TextAttribute extends MetaAttribute {
    private String defaultTextValue;
    private Long maxLenght;
}

Theses classes describe attributes of a dynamic entity where each row in MetaAttribute table represents one attribute metadata (like it's type, default value and more). These attributes can be changed from one type to another (like from BooleanAttribute to TextAttribute), but whenever this happens Ebean don't update the column attribute_type in database and when the row is fetched again it is returned with the wrong implementation.

To reproduce this scenario these are the required steps:
  1. Create an instance of BooleanAttribute and insert it in the database.
  2. Convert the BooleanAttribute instance to TextAttribute keeping the same entity ID.
  3. Save the changes in database using Ebean update.
  4. Retrieve the entity from the database. It will be returned as an instance of BooleanAttribute.
Is there anything that we are missing (like some configuration, annotation or even misuse of the Ebean)? Or this isn't supported by Ebean for now?

Rob Bygrave

unread,
Aug 5, 2018, 6:02:43 PM8/5/18
to ebean@googlegroups
> Or this isn't supported by Ebean for now?

Well, there isn't any code in Ebean to update the discriminator value.  So yes you could say it is not supported or not expected.


> or even misuse of the Ebean? 

The current expectation is that something changing it's type would effectively result in a delete and insert rather than an update. For a TextAttribute to become a BooleanAttribute that is expected to be a delete and insert.




Why are we trying to keep the same entity ID value?  



Cheers, Rob.


--

---
You received this message because you are subscribed to the Google Groups "Ebean ORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lucas Dillmann

unread,
Aug 5, 2018, 6:48:22 PM8/5/18
to Ebean ORM

Why are we trying to keep the same entity ID value? 

Because the row is still the same, the type of the dynamic attribute (defined by the discriminator column) which the row represents is the only thing that is changing, and the row may be referenced by a foreign key,

For a TextAttribute to become a BooleanAttribute that is expected to be a delete and insert.

I agree. This is what we first think to do, but an update is way more efficient for this scenario than an delete (which will validate the FK violations for exemple) and a new insert (which will also validate fields values and more), without mention the fact that an update is a lot less of network traffic.

Rob Bygrave

unread,
Aug 5, 2018, 7:33:57 PM8/5/18
to ebean@googlegroups
So code wise how are you doing the update?  Are you using stateless update or explicit update or SqlUpdate or UpdateQuery?  The first 2 are Ebean features that don't really have an equivalent in JPA, I am thinking this can not be done via JPA right? 

Ultimately the question will become how Ebean is going to be able to detect the change in the discriminator property (because it is not really a property per say but part of the type).  Do you have a proposal?  We don't really want to include the discriminator in every update.

Are you considering using SqlUpdate?

Lucas Dillmann

unread,
Aug 9, 2018, 7:33:01 AM8/9/18
to Ebean ORM
Since it's a new feature we are evaluating what we will do about it. In our tests we was using a regular EbeanServer#update, fixing the discriminator by declaring it as a regular column with "insertable = false" on @Column annotation. But, as suggested, we will also try the SqlUpdate.

Thanks for your attention for now.
Reply all
Reply to author
Forward
0 new messages