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
如何在使用Facelets+;持久化实体时比较和验证两个字段;JPA?_Jpa_Seam_Validation_Facelets - Fatal编程技术网

如何在使用Facelets+;持久化实体时比较和验证两个字段;JPA?

如何在使用Facelets+;持久化实体时比较和验证两个字段;JPA?,jpa,seam,validation,facelets,Jpa,Seam,Validation,Facelets,我正在与Facelets一起使用,我需要验证JPA实体中的两个字段,在插入之前比较这两个字段以检查其值 这是我的生命片段: @Entity @Scope(ScopeType.CONVERSATION) @Name("metaAbastecimento") public class MetaAbastecimento implements Serializable{ private float abastecimentoMinimo; private float abastecimento

我正在与Facelets一起使用,我需要验证JPA实体中的两个字段,在插入之前比较这两个字段以检查其值

这是我的生命片段:

@Entity
@Scope(ScopeType.CONVERSATION)
@Name("metaAbastecimento")
public class MetaAbastecimento  implements Serializable{
 private float abastecimentoMinimo;

 private float abastecimentoMaximo;


 @Column
 public float getAbastecimentoMinimo() {
  return abastecimentoMinimo;
 }

 @Column
 public float getAbastecimentoMaximo() {
  return abastecimentoMaximo;
 }

 public void setAbastecimentoMinimo(float abastecimentoMinimo) {
  this.abastecimentoMinimo = abastecimentoMinimo;
 }

 public void setAbastecimentoMaximo(float abastecimentoMaximo) {
  this.abastecimentoMaximo = abastecimentoMaximo;
 }
}
我有一个xhtml,它可以持久化这个实体:

<rich:panel>
        <f:facet name="header">Detalhe de Meta de Abastecimento</f:facet>


        <s:decorate id="abastecimentoMinimo" template="../layout/display.xhtml">
            <ui:define name="label">Meta(R$) de Abastecimento M&iacute;nimo</ui:define>
            <h:outputText value="#{metaAbastecimentoHome.instance.abastecimentoMinimo}">
            </h:outputText>
        </s:decorate>

        <s:decorate id="abastecimentoMaximo" template="../layout/display.xhtml">
            <ui:define name="label">Meta(R$) Abastecimento M&aacute;ximo</ui:define>
            <h:outputText value="#{metaAbastecimentoHome.instance.abastecimentoMaximo}"/>
        </s:decorate>

        <div style="clear:both"/>

    </rich:panel>

德梅塔尔德阿巴斯特西门托酒店
梅塔(R$)阿巴斯特西门托Mí;尼莫
Meta(R$)Abastecimento Má;西莫
在持久化这两个字段之前,我需要比较这两个字段,并检查它们是否不同于0f,以及ABASTECIMENTOMIMIMO是否小于ABASTECIMENTOMIMO。如何使用Seam+Facelets+JPA实现这一点


[]在JSF 1中不可能进行跨域验证。因此,您必须手动执行此操作

在您的情况下,这可以通过使用
@PrePersist
@PreUpdate
来完成,也可以在操作方法中手动完成

给你一个提示。避免将实体bean作为seam组件。最好制作一个单独的接缝组件

原因如下:

实体bean可以绑定到上下文变量,并作为seam组件使用。因为实体除了其上下文标识之外还有一个持久标识,所以实体实例通常在Java代码中显式绑定,而不是由Seam隐式实例化

实体bean组件不支持双射或上下文划分。实体bean的调用也不会触发验证。 实体bean通常不用作JSF操作侦听器,但通常用作支持bean,为JSF组件提供属性以供显示或表单提交。特别是,通常使用实体作为支持bean,并使用无状态会话bean操作侦听器来实现创建/更新/删除类型功能。 默认情况下,实体bean绑定到会话上下文。它们可能永远不会绑定到无状态上下文

注意,在集群环境中,直接将实体bean绑定到会话或会话范围的Seam上下文变量比在有状态会话bean中保存对实体bean的引用效率要低一些。因此,并非所有Seam应用程序都将实体bean定义为Seam组件


如果值上的约束不适用,会发生什么?+1,因为这也是我建议的解决方案。我只是想先问一下,异常是否是@Marcos Maia约束检查的有效结果。我个人不会在实体中这样做。至少不仅仅如此,因为这会迫使用户重定向到错误页面。更好的方法是在action方法中进行此检查,这样您就可以返回页面,并给出相应的消息Hi@Shervin,我将尝试并返回结果,谢谢。另外,我是一个讨厌的家伙,实际上很想知道做事情的原因,你能不能从你的答案中再深入一点?“给你们一个提示。避免使用实体bean作为seam组件。最好创建一个单独的seam组件。”@Marcos Maia:当然,答案是修改的
@Entity
public class MetaAbastecimento {
  .....

    //If check fails you can throw exception, thus rollback will occur, and commit will not be made
    @PrePersist
    @PreUpdate
    public void check() {
      if(abastecimentoMinimo == 0f || abastecimentoMaximo == 0f && abastecimentoMinimo > abastecimentoMaximo) 
        throw new RuntimeException("Failed!");
    }
}