Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
I have create an hierarchy betwen 2 class A and B. It work,
the superclass have a primary key. I want to add another attribute in the primary key to the sub class B.
A with the primary key id
B with the primary key id ( from A) and c definited in the sub-class
Hibernate version: 3.2.0.GA
...
session = sessionFactory.openSession() and
Transaction tx = session.beginTransaction();
B b = new B();
b.setC(1);
b.setD(2);
AId aid =new AId();
aid.setA(1);
aid.setB(2);
b.setId(aid);
session.save(b);
tx.commit();
session.flush();
@Entity(name="A")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class A implements Serializable{
@Id
private int id;
public class A implements Serializable{
@Id
private AId id;
/** Creates a new instance of A */
public A() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@Entity(name="b")
public class B extends A {
@Id
@Column(name="c")
private int c;
private int d;
/** Creates a new instance of B */
public B() {
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
public int getD() {
return d;
}
public void setD(int d) {
this.d = d;
}
}
I tried in this way, but it doesn't work.
Full stack trace of any exception that occurs:
org.hibernate.AnnotationException: Unable to define/override @Id(s) on a subclass: it.gesan.prova.B
at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1686)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1171)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:706)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:452)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:268)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1210)
at it.gesan.gvf.storage.util.HibernateUtil.<clinit>(HibernateUtil.java:91)
java.lang.ExceptionInInitializerError
at it.gesan.gvf.storage.util.HibernateUtil.<clinit>(HibernateUtil.java:96)
Caused by: org.hibernate.AnnotationException: Unable to define/override @Id(s) on a subclass: it.gesan.prova.B
at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1686)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1171)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:706)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:452)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:268)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1210)
at it.gesan.gvf.storage.util.HibernateUtil.<clinit>(HibernateUtil.java:91)
database i am using: mysql 5