Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring JPA瞬态列表未初始化_Spring_Jpa_Javafx_Transient - Fatal编程技术网

Spring JPA瞬态列表未初始化

Spring JPA瞬态列表未初始化,spring,jpa,javafx,transient,Spring,Jpa,Javafx,Transient,因为我想在JavaFX中的entities类中使用ObservableList,所以最初我在Hibernate默认使用的专用列表实现和反射注入方面遇到了问题。因此,我决定用@Access(AccessType.PROPERTY)对实体类进行注释,因为我想强制Hibernate使用我的getter和setter方法,而不是反射 在特定类别中,我有许多列表属性,例如受保护列表成本估算。对于每个列表,我都有相应注释的getter和setter。到目前为止还不错,这似乎奏效了。 问题是,在我的UI中,我

因为我想在JavaFX中的entities类中使用ObservableList,所以最初我在Hibernate默认使用的专用列表实现和反射注入方面遇到了问题。因此,我决定用
@Access(AccessType.PROPERTY)
对实体类进行注释,因为我想强制Hibernate使用我的getter和setter方法,而不是反射

在特定类别中,我有许多列表属性,例如
受保护列表成本估算。对于每个列表,我都有相应注释的getter和setter。到目前为止还不错,这似乎奏效了。
问题是,在我的UI中,我不想显示(例如)随时间创建的所有成本估算,而只显示最后一个。因此,我创建了一个方法
public CostEstimate getLastCostEstimate()
,该方法只返回列表中的最后一个元素。这个方法用@Transient注释,因为MySQL数据库中没有匹配的列,因为它只返回相关列表中的最后一个元素

My controller类将实体的
getLastCostEstimate()
绑定到相应的UI元素

在实体类的默认构造函数中,costEstimates列表用初始默认估计值初始化,这样
getLastCostEstimate()
应该始终返回有意义的CostEstimate。在调试器中,我可以看到已执行此初始化。但是,在运行时,costEstimates列表是空的,我得到一个IndexOutOfBoundsException。我想这和@Transient注释有关?!我想知道我是否有编码或设计问题?我想我的问题是:如何在JPA实体类中建模“只给我列表中的最后一个元素”(没有太多样板代码)

谢谢你的帮助

为方便起见,请查找以下一些相关代码片段:

@Entity
@Inheritance
@Access(AccessType.PROPERTY)    // JPA reading and writing attributes through their accessor getter and setter methods
public abstract class UserRequirement extends Requirement implements Serializable {
..
    protected List<CostEstimate> costEstimates; // Money needed 
..
    protected UserRequirement() {
        ..
        costEstimates = FXCollections.observableArrayList();
        setLastCostEstimate(new CostEstimate(0));
        ..
    }

..
    @OneToMany(mappedBy="parent", orphanRemoval = true, cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    public List<CostEstimate> getCostEstimates() {
         return costEstimates;
    }

    @SuppressWarnings("unused")     // Used by JPA
    public void setCostEstimates(List<CostEstimate> newCostEstimates) {

        if(newCostEstimates != null) {

      ((ObservableList<CostEstimate>)costEstimates).setAll(newCostEstimates);   
        } else {
            costEstimates.clear();
        }
    }

    @Transient
    public CostEstimate getLastCostEstimate() {

        return costEstimates.get(costEstimates.size()-1);
    }

    public void setLastCostEstimate(CostEstimate costEstimate) {
        if(costEstimate = null) {
            return;
        }
        costEstimates.add(costEstimate);
    }

..
}
@实体
@继承权
@Access(AccessType.PROPERTY)//JPA通过访问器getter和setter方法读取和写入属性
公共抽象类UserRequirement扩展需求实现可序列化{
..
受保护的列表成本估算;//所需资金
..
受保护的用户需求(){
..
costEstimates=FXCollections.observableArrayList();
setLastCostEstimate(新的CostEstimate(0));
..
}
..
@OneToMany(mappedBy=“parent”,orphanRemoving=true,cascade=CascadeType.PERSIST,fetch=FetchType.LAZY)
公共列表getCostEstimates(){
回报成本估算;
}
@SuppressWarnings(“未使用”)//由JPA使用
公共作废设置成本估算(列出新成本估算){
if(newCostEstimates!=null){
((可观察列表)成本估算)。setAll(新成本估算);
}否则{
成本估算;
}
}
@短暂的
公共成本估算getLastCostEstimate(){
返回costEstimates.get(costEstimates.size()-1);
}
public void setLastCostEstimate(成本估算){
如果(costEstimate=null){
返回;
}
成本估算。添加(成本估算);
}
..
}

我想这是使用
fetch=FetchType.LAZY
获取结果的结果。直接访问字段应该可以防止延迟获取结果。使用
List estimates=getCostEstimates();返回estimates.get(estimates.size()-1)可能会工作…不幸的是,它不会改变任何东西。使用getCostEstimates()得到的估计值也是空的。我还将getter注释限制为@OneToMany(mappedBy=“parent”),仍然是相同的行为。还有其他想法吗?我不知道我是否明白。如果特定的
UserRequirement
在数据库中没有任何关联的
costEstimates
s,那么显然,当从数据库加载它时,JPA将使用默认构造函数初始化它(这将导致在
costEstimates
中添加一个元素),然后立即使用空集合调用
setCostEstimates
,从而清除
costEstimates
。这有什么奇怪的?嗨,克里兹。谢谢你的反馈。我想我明白你的意思了。但是,如果我不能在构造函数中“初始化”列表,我该如何“初始化”列表呢?