Hibernate接口多态映射
Hibernate interface polymorphism mapping
我最近在学习Hibernate,基本概念对我来说很清楚,但我现在有一个设计问题。我所有的具体 类 实现接口,这里是代码:
包 com;
import javax.persistence.Entity;
public interface Person {
public void setName(String name);
public String getName();
public void setId(Integer id);
public Integer getId();
public void setWeapon(Weapon weapon);
public Weapon getWeapon();
}
````
package com;
import javax.persistence.Entity;
public interface Weapon {
public void setSharpness(Integer sharpness);
public Integer getSharpness();
public void setId(Integer id);
public Integer getId();
}
````
package com;
import javax.persistence.*;
@Entity
@Table(name = "person_inf")
public class PersonImpl implements Person{
@Id@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "person_id")
private Integer id;
@Column(name = "person_name")
private String name;
@OneToOne(targetEntity = Weapon.class)
@JoinColumn(name = "weapon_id",referencedColumnName = "weapon_id")
private Weapon weapon;
setters and getters omitted
}
````
包 com;
import javax.persistence.*;
@Entity
@Table(name = "weapon_inf")
public class WeaponImpl implements Weapon {
@Id@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "weapon_id")
private Integer id;
@Column(name = "weapon_sharpness")
private Integer sharpness;
setters and getters omitted
}
当我尝试休眠程序(保存一个人的实例)时,它抛出
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.PersonImpl.weapon references an unknown entity: com.Weapon
是否有解决这个问题的方案或注解?
您应该引用 WeaponImpl
而不是 Weapon
,因为您的实体 class 是 WeaponImpl
。
这给了你字段
@OneToOne
@JoinColumn(name = "weapon_id",referencedColumnName = "weapon_id")
private WeaponImpl weapon;
尝试像这样使用 WeaponImpl
而不是 Weapon
:
@OneToOne(targetEntity = WeaponImpl.class)
我最近在学习Hibernate,基本概念对我来说很清楚,但我现在有一个设计问题。我所有的具体 类 实现接口,这里是代码: 包 com;
import javax.persistence.Entity;
public interface Person {
public void setName(String name);
public String getName();
public void setId(Integer id);
public Integer getId();
public void setWeapon(Weapon weapon);
public Weapon getWeapon();
}
````
package com;
import javax.persistence.Entity;
public interface Weapon {
public void setSharpness(Integer sharpness);
public Integer getSharpness();
public void setId(Integer id);
public Integer getId();
}
````
package com;
import javax.persistence.*;
@Entity
@Table(name = "person_inf")
public class PersonImpl implements Person{
@Id@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "person_id")
private Integer id;
@Column(name = "person_name")
private String name;
@OneToOne(targetEntity = Weapon.class)
@JoinColumn(name = "weapon_id",referencedColumnName = "weapon_id")
private Weapon weapon;
setters and getters omitted
}
```` 包 com;
import javax.persistence.*;
@Entity
@Table(name = "weapon_inf")
public class WeaponImpl implements Weapon {
@Id@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "weapon_id")
private Integer id;
@Column(name = "weapon_sharpness")
private Integer sharpness;
setters and getters omitted
}
当我尝试休眠程序(保存一个人的实例)时,它抛出
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.PersonImpl.weapon references an unknown entity: com.Weapon
是否有解决这个问题的方案或注解?
您应该引用 WeaponImpl
而不是 Weapon
,因为您的实体 class 是 WeaponImpl
。
这给了你字段
@OneToOne
@JoinColumn(name = "weapon_id",referencedColumnName = "weapon_id")
private WeaponImpl weapon;
尝试像这样使用 WeaponImpl
而不是 Weapon
:
@OneToOne(targetEntity = WeaponImpl.class)