0

I have an abstract base class with existing subclasses that is mostly used for defining a number of common fields and associated methods. I have a separate concrete class that "organically evolved" (i.e., bad design due to unforeseen feature requests) to end up with all the same fields defined in that abstract subclass.

Is there any way of having that separate class extend the abstract class and carry over the data of existing stored instances of that separate class? I would like to use InheritanceType.SINGLE_TABLE, but if another strategy makes it easier or possible, I guess that's fine too.

Also, those are entities referenced in other entities (OneToMany). Is that a problem? Hibernate uses only one global sequence for assigning entity ids - so it should in theory be possible to not break those references even if the data is moved to another table, right?

Already tried a few things, but no luck so far (e.g., add the "extend" to the separate class, hard-code it to use the same table as the base class, manually add a field for the discriminator...).

I am also happy about any pointers to examples/docs on how to carry out class hierarchy changes and other data model changes with JPA/Hibernate without losing data!


So, here's a simplified example of the situation. Base is the abstract base class that already has sub-classes.

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "Base")
public abstract class Base {
    private long persistenceId;

    private String privateField;

    @Id
    @GeneratedValue
    public long getPersistenceId() {
        return persistenceId;
    }

    public void setPersistenceId(long persistenceId) {
        this.persistenceId = persistenceId;
    }

[...]
}

@Entity
public class SubclassToBe {
    private long persistenceId;

    private String privateField;

    private String someFieldNotInBaseClass;

    @Override
    @Id
    @GeneratedValue
    public long getPersistenceId() {
        return persistenceId;
    }

    @Override
    public void setPersistenceId(long persistenceId) {
        this.persistenceId = persistenceId;
    }

[...]
}

The goal would be to have SubclassToBe inherit from Base, removing the definitions of shared fields but keeping the information stored there. And at the same time, not break references to the persistence ids of SubclassToBe objects that are used in other objects as part of OneToMany relations.

  • 2
    Have you tried `@MappedSuperclass` as described in [this answer](http://stackoverflow.com/a/3827546/3080094)? – vanOekel Jun 05 '14 at 21:25
  • Just tried it in a small example project and it removes all instances of the to-be-integrated class from its table, and I get a column index out of range error. –  Jun 06 '14 at 14:35
  • Hmm, that's unfortunate. Maybe providing a simple (code) example of what you are trying to achieve/the problem will attract an answer from somebody with more in-depth knowledge. – vanOekel Jun 06 '14 at 18:28

0 Answers0