0

In my current project I have a inheritance structure that looks like this:

@Entity
@Table(name="groups")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorValue("G")
@DiscriminatorColumn(name="group_type")
public class Group{  //some annotations removed 
    private Long id;
    private String name;
    private Set<Subject> subjects;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="parent_group_id")
    private Group parent;            #####  tree parent  ####
@OneToMany(cascade=CascadeType.ALL, mappedBy="parent")
    private Set<Group> subGroups;    ##### tree children #####
        ...
}

My Group objects can have kind of a tree like structure by containing a list of other Group objects. As some groups are a bit special, there is a second class that extends this class:

   @Entity
   @DiscriminatorValue("C")
   @Table(name="fix_groups")
   public class FixGroup extends Group{
        private Layout lay;
        private Set<Person> instr;
            ...
    }

I tried to use a joined multi table approach (as described here: http://en.wikibooks.org/wiki/Java_Persistence/Inheritance#Joined.2C_Multiple_Table_Inheritance) but it seems not to work with a non abstract superclass like Group! I get the following exception:

Caused by: java.lang.ClassCastException: org.hibernate.mapping.JoinedSubclass 
cannot be cast to org.hibernate.mapping.RootClass

Is there a solution apart from declaring Group as abstract and making a new class Group2 that only extends it? And if I did so, would this self-reference Set<Group> subGroups still cause problems?

ZeroOne
  • 3,041
  • 3
  • 31
  • 52
chris
  • 247
  • 2
  • 5
  • 16

1 Answers1

1

I was able to cause this error by setting the ID in the subclass when it is already mapped in the parent class (in this case Group). For example in the parent class:

@Entity
@Table(name="groups")
@Inheritance(strategy=InheritanceType.JOINED)
public class Group {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;
...

and then setting the id in the subclass like so:

@Entity
@Table(name="sub_groups")
public class SubGroup extends Group {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;
...

In this case, Group does not need to be abstract, but you can't define the id on the subclass.

Also, as a side note, if you are using Hibernate with an inheritance of type "join", then the discriminator column and value are not needed. Hibernate only utilizes those if using a single table. Please reference this post for further information:

Hibernate 4: persisting InheritanceType.JOINED discriminator column values

Community
  • 1
  • 1
Waggins
  • 164
  • 1
  • 4