Hibernate:org.Hibernate.ErrorClassException
我撞到我的头是因为以下问题: 我有两个实体类继承了一个基类。基类只存储创建/更新日期 基类:Hibernate:org.Hibernate.ErrorClassException,hibernate,Hibernate,我撞到我的头是因为以下问题: 我有两个实体类继承了一个基类。基类只存储创建/更新日期 基类: @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class BaseEntity { @Id @GeneratedValue(strategy = GenerationType.TABLE) @Column(name = "id") private int id; @Temporal(T
@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>();
...
每当我尝试加载酒店实体时,都会出现以下错误:
org.hibernate.ErrorClassException:id为7的对象不属于指定的子类:de.hop.entity.HotelManager(加载的对象属于错误的类de.hop.entity.Hotel)
旁注:两个db表中的hotel row id(唯一id)和hotelmanager row id都是“7”
有什么想法吗?您可以向基类添加
@MappedSuperclass
而不是@heritation
。请在加载实体的位置添加代码。为什么要使用@Inheritance注释。您可以将@MappedSuperclass添加到基类。请尝试将hotel中设置的hotel manager的获取类型设置为lazy。最有可能的问题是,试图加载酒店经理热切地与酒店相关联的邮件。@si mo解决了我的问题。谢谢!:-)我补充了一个答案。你能把它标为anwer吗?THX这个答案有误导性,因为这样做时:“org.hibernate.AnnotationException不能同时使用@entity和@MappedSuperclass对实体进行注释”。所以在你的答案中应该是“而不是@Entity”?请检查/澄清。
@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;
....