映射实现接口的两个类之一

时间:2015-07-23 01:11:16

标签: java hibernate

Class具有可以是两个类类型之一的属性,这就是我尝试创建类类型实现的接口的原因

@Entity
@Table(name = "users")
public class User{  
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long userID;
@Column(name="email")
private String email;
@OneToOne(mappedBy = "user")
private Login login;
... getters/setters

@MappedSuperclass
public interface Login {
   User user = new User();
}

@Entity
@Table(name = "user_logins_social")
@IdClass(UserLoginSocialID.class)
public class UserLoginSocial implements Login{      
    @OneToOne
    @JoinColumn(name="uid")
    private User user;
...

@Entity
@Table(name = "user_logins_native")
public class UserLoginNative implements Login{
  @OneToOne
  @JoinColumn(name="uid")
private User user;
...

所以我不能使用目标实体,因为可以使用这两个类。这是错误堆栈:org.hibernate.AnnotationException:未知的mappedBy in:model.User.login,引用的属性unknown:model.Login.user。请帮帮我

1 个答案:

答案 0 :(得分:1)

无法在界面上进行地图或查询。

@MappedSuperclass 应该能够使用抽象类和类。

请检查:wiki http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Interfaces

@MappedSuperclass 
public abstract class User{  
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long userID;

@Column(name="email")
private String email;

@OneToOne(mappedBy = "user")
private Login login;

// ... getters/setters

@Entity
@Table(name = "login")
public class Login{      
    @OneToOne
    private User user;
}

@Entity
@Table(name = "user_logins_social")
@IdClass(UserLoginSocialID.class)
public class UserLoginSocial extends User{      
    //...
}

@Entity
@Table(name = "user_logins_native")
public class UserLoginNative extends User{
  //...
}