由于以下问题,我正在敲打头:
我有2个实体clases继承基类。基类只存储创建/更新日期。
基类:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "id")
private int id;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATED", nullable = true)
private Date created;
@PrePersist
protected void onCreate() {
created = new Date();
}
...
子类1:
@Entity
@Table(name = "hotel")
@XmlRootElement
public class Hotel extends BaseEntity implements Serializable
...
@OneToMany(fetch = FetchType.EAGER, mappedBy = "hotel", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<HotelManager> hotelManagers = new HashSet<HotelManager>();
...
子类2:
@Entity
@Table(name = "hotel_manager")
public class HotelManager extends BaseEntity implements Serializable {
...
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "hotel_id", nullable = false)
private Hotel hotel;
....
每当我尝试加载酒店实体时,我都会收到以下错误:
org.hibernate.WrongClassException:id为7的对象不是指定的子类:de.hop.entity.HotelManager(加载的对象是类错误的类de.hop.entity.Hotel)
附注:偶然酒店行id(唯一ID)以及hotelmanager行id在两个db表中都是'7'
任何想法???
答案 0 :(得分:0)
您可以将@ MappedSuperclass
代替@Inheritance
添加到基类。