Hibernate的泛型 [英] Generics with Hibernate
本文介绍了Hibernate的泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下类无法通过Hibernate加载
Following class fail to load with the Hibernate
package com.project.alice.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonProperty;
@Table
@Entity
public class AnyInformation<T, K> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty("id")
private long id;
@JsonProperty("parent")
@ManyToOne
private T parent;
@ManyToOne
@JsonProperty("parentType")
private K parentType;
@JsonProperty("informationType")
private String informationType;
@JsonProperty("information")
private String information;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public T getParent() {
return parent;
}
public void setParent(T parent) {
this.parent = parent;
}
public K getParentType() {
return parentType;
}
public void setParentType(K parentType) {
this.parentType = parentType;
}
public String getInformationType() {
return informationType;
}
public void setInformationType(String informationType) {
this.informationType = informationType;
}
public String getInformation() {
return information;
}
public void setInformation(String information) {
this.information = information;
}
}
org.hibernate.AnnotationException:
Property com.project.alice.entities.AnyInformation.parent
has an unbound type and no explicit target entity.
Resolve this Generic usage issue or set an explicit target attribute
(eg @OneToMany(target=) or use an explicit @Type
请在这里帮助我.
推荐答案
您应该尝试-
@ManyToOne(targetEntity=Sample.class)
@JoinColumn(name = "<ID>")
private P parent;
@OneToMany(mappedBy = "parent", cascade = CascadeType.PERSIST, targetEntity=Sample.class)
private List<C> children;
P和C在其中扩展了Sample Class.
Where, P and C extends Sample Class.
希望对您有帮助.
注意:据我所知,它不可能像您期望的那样通用.您不能提供任何对象作为参数.但是对象应该与您要定义的实体相关,例如父级"或子级".这就是它在Hibernate中的工作方式.
Note : As per my knowledge It can't be generic as you are expecting. You can not provide any object as a parameter. But the object should be related to the entity you are defining like Parent or Child. That's how it works in Hibernate.
这篇关于Hibernate的泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文