views:

30

answers:

1

Hi.

I'm having the following design for my hibernate project:

@MappedSuperclass
public abstract class User {
    private List<Profil>    profile;

    @ManyToMany (targetEntity=Profil.class)
    public List<Profil> getProfile(){
        return profile;
    }
    public void setProfile(List<Profil> profile) {
        this.profile = profile;
    }
}

@Entity
@Table(name="kunde")
public class Kunde extends User {
    private Date    birthdate;
    @Column(name="birthdate")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getBirthdate() {
        return birthdate;
    }
    public void setBirthdate(Date birthdate) {
        this.birthdate= birthdate;
    }
}

@Entity
@Table(name="employee")
public class Employee extends User {
    private Date    startdate;
    @Column(name="startdate")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getStartdate() {
        return startdate;
    }
    public void setStartdate(Date startdate) {
        this.startdate= startdate;
    }
}

As you can see, User hat a ManyToMany relationship to Profile.

@Entity
@Table(name="profil")
public class Profil extends GObject {
    private List<User>  user;

    @ManyToMany(mappedBy = "profile", targetEntity = User.class )
    public List<User> getUser(){
        return user;
    }
    public void setUser(List<User> user){
        this.user = user;
    }
}

If I now try to create an employee, i get a hibernate exception:

org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: de.ke.objects.bo.profile.Profil.user[de.ke.objects.bo.user.User]

How can I use a ManyToMany-Relationship to Profile on the superclass User, so it works for Kunde and Employee?

Thanks in advance.

+2  A: 

The problem is with the reference to User, which isn't an entity. A foreign key can only reference one table. Either use @ManyToAny, which can handle multiple tables, or @Inheritance(strategy=InheritanceType.SINGLE_TABLE), which will put all of the entities onto one table.

Here's the documentation on inheritance in hibernate: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1168

sblundy