0

i have a generic class which is supper class of some non-generic class and those are just setting its generic parameter like this:

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
class A<T>{
    @Id
    getId(){..}
    setID(int id){..}
    int id

    T t;
    T getT(){...}
    setT(T t){...}
}

and

@Entity
class B extends A<Integer>{}

but hibernate says that B does not have an identifier what should I do?

1
  • they are in the same package and in my real code they are public as well. but I think i should put some annotations to make hibernate understand it! Commented Oct 22, 2012 at 15:57

4 Answers 4

1

If A won't be directly persisted, but you do want it's subclasses to pick up some (or all) of its Hibernate annotations, you should use @MappedSuperclass:

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
class A<T>{
    @Id
    getId(){..}
    setID(int id){..}
    int id

    T t;
    T getT(){...}
    setT(T t){...}
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to add the @Entity annotation to class A as well. The @Transient annotation on attribute t should help with your second exception

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
class A<T> {
    @Id
    getId(){..}
    setID(int id){..}
    int id

    @Transient
    T t;
    T getT(){...}
    setT(T t){...}
}

2 Comments

now i get this error: A has an unbound type and no explicit target entity. Resolve this Generic usage issue or set an explicit target attribute (eg @OneToMany(target=) or use an explicit @Type
Try adding @Transient to the t property
0

I agree with reply No. 1, use @MappedSuperclass for A - don't make something abstract an Entity. You should probably make this class specifically abstract too.

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
abstract class A<T>{
  @Id
  getId(){..}
  setID(int id){..}
  int id

  T t;
  T getT(){...}
  setT(T t){...}
}

A table-per-class strategy often requires this kind of abstract base. Then the subclass specifies the table name, and additional fields.

@Entity
@Table(name="MY_INTEGERS")
class B extends A<Integer>{}

(Personally I would move this variable type into the subclass, but I don't know what you're trying to achieve).

Comments

0

After lots of testing, trying to get Java parameterisation working with an abstract parent (Single-table inheritance), and an abstract child table (one-table-per-class inheritance), I've given up.

It may be possible, but often you get problems where Hibernate tries to instantiate an abstract (parameterised) class as an entity. this is when you get the error "A has an unbound type and no explicit target entity." It means Hibernate doesn't have a parameter value for a parameterised type. I found that tests for the extending classes were fine, but tests around parent entities would break.

I would suggest rewriting it using the JPA inheritance, moving the parameterised stuff down into extending classes. That way you get the same polymorphism back from the database.

@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "CLASS_TYPE", discriminatorType = DiscriminatorType.STRING)
public abstract class ClassA {

  [...]

}

extension B:

@Entity
@DiscriminatorValue=("B")
public class ClassB extends ClassA {
  @OneToOne
  @JoinColumn(name = "mycolumn_id")
  private Integer instance;

  [...]
}

extension C:

@Entity
@DiscriminatorValue=("C")
public class ClassC extends ClassA {
  @OneToOne
  @JoinColumn(name = "mycolumn_id")
  private String instance;

  [...]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.