gpt4 book ai didi

java - Hibernate 泛型

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:06:35 25 4
gpt4 key购买 nike

以下类无法使用 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 扩展了样本类。

希望对你有帮助。

注意:据我所知,它不能像您期望的那样通用。您不能提供任何对象作为参数。但是该对象应该与您定义的实体相关,例如 Parent 或 Child。这就是它在 Hibernate 中的工作方式。

关于java - Hibernate 泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35937644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com