4

I try to have 2 tables, as follows:

MISExercise (table)


ID NAME ...

2 a


MISInteractiveExercise(table)


ID NAME ...

1 b


They must have no same id. And they are inherited from the same base. My code is :

@MappedSuperclass  
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class MISExerciseBase {
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Integer id; 

    ...
}

@Entity
public class MISExercise extends MISExerciseBase{
   ...
}

@Entity
public class MISInteractiveExercise extends MISExerciseBase{
   ...
}

Unfortunately, I find that the table of MISExercise and the table of MISInteractiveExercise can have the same id. When I google it I find http://openjpa.208410.n2.nabble.com/same-Id-on-mapped-superclass-td2435374.html. @Kaayan seems has the same problem. But I can't get help from that page.

And it seems if I use @Entity not @MappedSuperclass, it will be fine. But why, and what's the good way?

toxin
  • 93
  • 1
  • 6

2 Answers2

3

As both your classes MISExercise and MISInteractiveExersice both inherit from MISExerciseBase, and you have set your Generation Strategy as @GeneratedValue(strategy = GenerationType.TABLE), your id's are not going to be unique across all your tables, but only unique per table.

If you would like to have unique id across multiple tables, ie in your case MISExercise and MISInteractiveExerice, you need to change your generation Strategy to Auto.

So to fix your problem change your abstract class MISExerciseBase to this...

@MappedSuperclass  
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class MISExerciseBase {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO) //NOTE This Change to AUTO
    private Integer id; 

    ...
}
SpartanElite
  • 624
  • 4
  • 13
  • As http://stackoverflow.com/questions/3154649/java-hibernate-jpa-inheritancetype-table-per-class-and-ids says, it can't use AUTO. But I just try to use AUTO, it seems OK. So I'm confused. – toxin Dec 20 '13 at 13:15
  • If you want to have unique ids per table then stick with what you have. But if you want unique ids (ie both tables using the same global sequence generator then use AUTO). – SpartanElite Dec 20 '13 at 15:47
  • OK, thank u. But is there any difference when I use @Entity to replace @MappedSuperclass? When I look into my database, it won't generate table MISExerciseBase too. – toxin Dec 21 '13 at 04:47
  • And I'm still confused with that link http://stackoverflow.com/questions/3154649/java-hibernate-jpa-inheritancetype-table-per-class-and-ids. When I google it, I find the similar answer happened many times, which all say when using the strategy InheritanceType.TABLE_PER_CLASS, u can only use GenerationType.TABLE. Any idea? – toxin Dec 21 '13 at 04:56
  • 1
    If you use `@MappedSuperclass`, then there will be no database table created. When using `@Entity`, it generated the corresponding db table. – SpartanElite Jan 03 '14 at 18:40
0

I ran into a similar issue. This fixed it for me by adding this as a class level annotation:

@SequenceGenerator(initialValue = 1, name = "idgen", sequenceName = "parentseq", allocationSize = 1)

You don't need to specify ALL that stuff, but the important part is the sequenceName and making sure that the child classes are using the same one as the parent class.

Forrest
  • 2,968
  • 1
  • 27
  • 18