开发者

@ManyToMany in an abstract MappedSuperclass

开发者 https://www.devze.com 2023-01-23 00:42 出处:网络
I\'m having the following design for my hibernate project: @MappedSuperclass public abstract class User {

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="client")
public class Client 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 Client and Employee?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消