Java 我向多对多关系添加了一个条目,但是当我得到一个对象时,列表是空的

Java 我向多对多关系添加了一个条目,但是当我得到一个对象时,列表是空的,java,hibernate,many-to-many,Java,Hibernate,Many To Many,我有两个领域: 类别 在类别中,相关方法为 public void addSolution(Solution solutionAdded) { //TODO make checks that the solution doesn't already exist. List allows duplicate entries if (solutions == null) { solutions = new ArrayList<Soluti

我有两个领域: 类别

在类别中,相关方法为

public void addSolution(Solution solutionAdded) {
        //TODO make checks that the solution doesn't already exist. List allows duplicate entries
        if (solutions == null) {
            solutions = new ArrayList<Solution>(1);
        }
        this.solutions.add(solutionAdded);

        if(!solutionAdded.getCategories().contains(this)) {
            solutionAdded.addCategory(this);
        }
    }
public void addSolution(解决方案added){
//TODO检查解决方案是否不存在。列表允许重复条目
如果(解决方案==null){
解决方案=新阵列列表(1);
}
this.solutions.add(solutionAdded);
如果(!solutionAdded.getCategories().包含(此)){
solutionAdded.addCategory(本);
}
}
在这个解决方案中

protected void addCategory(Category categoryAdded) {
        //TODO Check for duplicate blah blah blah
        if (categories == null) {
            categories = new ArrayList<Category>(1);
        }
        this.categories.add(categoryAdded);

        //This is not needed if i make this protected and always work from the category side
        if(!categoryAdded.getSolutions().contains(this)) {
            categoryAdded.addSolution(this);
        }

    }
protectedvoid addCategory(类别added){
//TODO检查重复的废话废话
如果(类别==null){
类别=新阵列列表(1);
}
此.categories.add(categoryAdded);
//如果我将此设置为受保护并且始终从类别方面工作,则不需要这样做
如果(!categoryAdded.getSolutions().包含(此)){
categoryAdded.addSolution(本);
}
}
保存或更新基本上是调用entityManagerProvider.get().persist(对象)在底层(我的访问级别(DAO))的某个地方

因此,如果我运行servlet并遵循此路径,我可以在我的解决方案|类别|映射表中看到一个条目|解决方案|类别|

但是,如果在同一页中,然后我尝试获取类别或解决方案条目,则对象中的列表成员为null。它不应该带来什么吗?或者,因为它是延迟获取,所以我必须做一些事情让hibernate从表中检索实际数据


感谢您的帮助、指点和建议。

如果列表为空,获取类别和测试的代码在哪里?另外,调用persist()是错误的:persist()用于使临时实体持久化。你所拥有的已经是一个持久的实体。如果代码在事务中(应该在事务中),则不需要调用任何方法来持久化更改。addSolution和addCategory是域类的一部分。我检查null的部分,这些是顶部用ManyToMany Anotation定义的列表。我在更高的级别上,在servlet级别上有事务处理。我应该在服务级别中使用它吗?此外,为了使代码在相关POITNSO中更具可读性,代码被剥离了一点。如果您在事务中,则对
saveOrUpdate()
的两个调用是无用的。如何“获取类别或解决方案条目,[并确定]对象中的列表成员为空。”。发布相关代码。不要使用调试器来确定某个内容是否为空。Hibernate使用代理,并惰性地初始化它们。打印
category.getSolutions()
的值,然后查看。您使用的是Spring吗?Spring的事务性注释不能应用于servlet方法:servlet不是Springbean。它通常应用于服务方法。回答你的问题:是的,我肯定。如果它不这样做,那么您可能无法正确处理事务。
private void insertRelationship(CategoryKey categoryKey, SolutionKey solutionKey) {
        Solution solutionToBeEdited = solutionAccess.fetchByPrimaryKey(solutionKey);
        Category categoryToBeEdited = categoryAccess.fetchByPrimaryKey(categoryKey);

        //The category is the owing side so I assume that as the start of all the thinking
        categoryToBeEdited.addSolution(solutionToBeEdited);

        solutionAccess.saveOrUpdate(solutionToBeEdited);
        categoryAccess.saveOrUpdate(categoryToBeEdited);
    }
public void addSolution(Solution solutionAdded) {
        //TODO make checks that the solution doesn't already exist. List allows duplicate entries
        if (solutions == null) {
            solutions = new ArrayList<Solution>(1);
        }
        this.solutions.add(solutionAdded);

        if(!solutionAdded.getCategories().contains(this)) {
            solutionAdded.addCategory(this);
        }
    }
protected void addCategory(Category categoryAdded) {
        //TODO Check for duplicate blah blah blah
        if (categories == null) {
            categories = new ArrayList<Category>(1);
        }
        this.categories.add(categoryAdded);

        //This is not needed if i make this protected and always work from the category side
        if(!categoryAdded.getSolutions().contains(this)) {
            categoryAdded.addSolution(this);
        }

    }