Hibernate ManyToMany 双向关系不获取子实体中的父实体
Hibernate ManyToMany bidirectional relationship is not fetching parent entities in child
我有一个应用程序,其中有与多个文本实体相关联的项目,并且文本实体与多个项目(多对多)相关联。
现在,当我尝试在休眠中获取文本实体时,我得到了 projects=null
这是关系的项目方:
import lombok.Data;
import javax.persistence.*;
import java.util.List;
@Data
@Entity
@Table(name = "project_data")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name="project_id")
private int projectId;
@Column(name="project_name")
private String projectName;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name="project_text", joinColumns = {@JoinColumn(name = "project_id")}, inverseJoinColumns = {@JoinColumn(name="text_id")})
private List<Text> texts;
public Project(String projectName){
this.projectName = projectName;
}
}
这是关系的文本端:
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.util.List;
@Data
@Entity
@Table(name="text_data")
public class Text {
@Id
@GeneratedValue(generator = "text_hash")
@GenericGenerator(name = "text_hash",strategy = "TextHashGenerator")
@Column(name="text_id")
private String text_id;
@Column(name="text_value")
private String text;
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "texts", fetch = FetchType.LAZY)
private List<Project> projects;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="text_keyword", joinColumns = {@JoinColumn(name = "text_id")}, inverseJoinColumns = {@JoinColumn(name = "keyword_id")})
private List<Keyword> keywords;
public Text(String text){
this.text = text;
}
}
这是我用来为文本实体生成 ID 的自定义 TextHashGenerator
import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import java.io.Serializable;
public class TextHashGenerator implements IdentifierGenerator{
@Override
public Serializable generate(SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException {
String textHash = "";
if(o instanceof Text){
Text text = (Text) o;
textHash = DigestUtils.sha256Hex(text.getText());
}
return textHash;
}
}
此问题与所有关系有关,因为它们无法获取关系的所有者端。
输出
Text(text_id=21e57b707ffe2bd2d89b2b6f6c999597dc6e9dd90eaee809fbd06c222cf54de8, text=Text 1, projects=null, keywords=[Keyword(keywordId=2, keyword=Text 1 Keyword 1, texts=null, meanings=[Meaning(meaningId=3, meaning=Text 1 Keyword 1 meaning 1, keyword=null), Meaning(meaningId=4, meaning=Text 1 Keyword 1 meaning 2, keyword=null), Meaning(meaningId=5, meaning=Text 1 Keyword 2 meaning 1, keyword=null)]), Keyword(keywordId=6, keyword=Text 1 Keyword 2, texts=null, meanings=null), Keyword(keywordId=7, keyword=Text 2 Keyword 1, texts=null, meanings=null), Keyword(keywordId=8, keyword=Text 2 Keyword 2, texts=null, meanings=null)])
我正在使用以下查询来获取文本
Query query = session.createQuery("from Text");
如果您设置 fetch = FetchType.LAZY
,则必须加入关系(您应该这样做,不要更改)。这样做:
Query query = session.createQuery("SELECT t from Text t JOIN t.projects")
我有一个应用程序,其中有与多个文本实体相关联的项目,并且文本实体与多个项目(多对多)相关联。 现在,当我尝试在休眠中获取文本实体时,我得到了 projects=null
这是关系的项目方:
import lombok.Data;
import javax.persistence.*;
import java.util.List;
@Data
@Entity
@Table(name = "project_data")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name="project_id")
private int projectId;
@Column(name="project_name")
private String projectName;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name="project_text", joinColumns = {@JoinColumn(name = "project_id")}, inverseJoinColumns = {@JoinColumn(name="text_id")})
private List<Text> texts;
public Project(String projectName){
this.projectName = projectName;
}
}
这是关系的文本端:
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.util.List;
@Data
@Entity
@Table(name="text_data")
public class Text {
@Id
@GeneratedValue(generator = "text_hash")
@GenericGenerator(name = "text_hash",strategy = "TextHashGenerator")
@Column(name="text_id")
private String text_id;
@Column(name="text_value")
private String text;
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "texts", fetch = FetchType.LAZY)
private List<Project> projects;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="text_keyword", joinColumns = {@JoinColumn(name = "text_id")}, inverseJoinColumns = {@JoinColumn(name = "keyword_id")})
private List<Keyword> keywords;
public Text(String text){
this.text = text;
}
}
这是我用来为文本实体生成 ID 的自定义 TextHashGenerator
import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import java.io.Serializable;
public class TextHashGenerator implements IdentifierGenerator{
@Override
public Serializable generate(SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException {
String textHash = "";
if(o instanceof Text){
Text text = (Text) o;
textHash = DigestUtils.sha256Hex(text.getText());
}
return textHash;
}
}
此问题与所有关系有关,因为它们无法获取关系的所有者端。
输出
Text(text_id=21e57b707ffe2bd2d89b2b6f6c999597dc6e9dd90eaee809fbd06c222cf54de8, text=Text 1, projects=null, keywords=[Keyword(keywordId=2, keyword=Text 1 Keyword 1, texts=null, meanings=[Meaning(meaningId=3, meaning=Text 1 Keyword 1 meaning 1, keyword=null), Meaning(meaningId=4, meaning=Text 1 Keyword 1 meaning 2, keyword=null), Meaning(meaningId=5, meaning=Text 1 Keyword 2 meaning 1, keyword=null)]), Keyword(keywordId=6, keyword=Text 1 Keyword 2, texts=null, meanings=null), Keyword(keywordId=7, keyword=Text 2 Keyword 1, texts=null, meanings=null), Keyword(keywordId=8, keyword=Text 2 Keyword 2, texts=null, meanings=null)])
我正在使用以下查询来获取文本
Query query = session.createQuery("from Text");
如果您设置 fetch = FetchType.LAZY
,则必须加入关系(您应该这样做,不要更改)。这样做:
Query query = session.createQuery("SELECT t from Text t JOIN t.projects")