簡體   English   中英

Hibernate ManyToMany 雙向關系不獲取子實體中的父實體

[英]Hibernate ManyToMany bidirectional relationship is not fetching parent entities in child

我有一個應用程序,其中有與多個文本實體相關聯的項目,而文本實體與多個項目(多對多)相關聯。 現在,當我嘗試在 hibernate 中獲取文本實體時,我得到 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;
    }
}

這個問題與所有關系有關,因為它們無法獲取關系的所有者方。

Output

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM