当前位置: 首页 > 知识库问答 >
问题:

spring boot javax.persistence.EntityNotFoundException在多对一中,无论数据是否存在

陈君之
2023-03-14

考虑code bellow

受益人类别:

@Entity
@Table(uniqueConstraints=
@UniqueConstraint(name="u_benef_noBenef", columnNames="noBenef"))
public class Beneficiaire implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer idBenef;

@Column(nullable=false)
private Integer noApBenef;

@Column(nullable=false)
private Integer noBenef;

@Column(columnDefinition="character varying(100) not null")
private String nomBenef;

@DateTimeFormat(pattern="dd/MM/yyyy")
@Column(nullable=false)
private LocalDate dateNaissanceBenef;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="id_cat_beneficiaire",
            foreignKey=@ForeignKey(name="fk_benef_idCatBenef"),
            nullable=false)
private CategorieBeneficiaire categorieBeneficiaire;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="id_sexe",
            foreignKey=@ForeignKey(name="fk_benef_idSexe"),
            nullable=false)
private Sexe sexe;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="id_assure",
            foreignKey=@ForeignKey(name="fk_benef_idAssure"),
            nullable=false)
private Assure assure;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="id_attributaire",
            foreignKey=@ForeignKey(name="fk_benef_idAttrib"),
            nullable=false)
private Attributaire attributaire;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="id_prestation",
            foreignKey=@ForeignKey(name="fk_benef_idPrestation"),
            nullable=false)
private Prestation prestation;

@DateTimeFormat(pattern="dd/MM/yyyy")
@Column(nullable=false)
private LocalDate dateEffetPrestation;

@Column(precision=8, nullable=false) 
private BigDecimal mntMensuelPrestation;

private Integer trimestreDerniereEmission;

private Integer anneeDerniereEmission;

@Column(columnDefinition="integer default 1 not null", insertable=false)
private Integer estActifBenef;

@Column(columnDefinition="timestamp default current_timestamp not null", insertable=false)
private LocalDateTime dateheureCreationBenef;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="id_utilisateur",
        foreignKey=@ForeignKey(name="utilisateur_id_fk"),
        nullable=true)
private Utilisateur utilisateur;

/*@OneToMany(mappedBy="beneficiairePj", cascade=CascadeType.ALL, orphanRemoval=true)
private List<PieceJustificative> pieceJustificatives = new ArrayList<PieceJustificative>();*/



public Beneficiaire() {
    //For JPA
}


public Beneficiaire(Integer noBenef, String nomBenef, Integer noApBenef,
        CategorieBeneficiaire categorieBeneficiaire, Sexe sexe, Assure assure, Attributaire attributaire,
        Prestation prestation) {
    super();
    this.noBenef = noBenef;
    this.nomBenef = nomBenef;
    this.noApBenef = noApBenef;
    this.categorieBeneficiaire = categorieBeneficiaire;
    this.sexe = sexe;
    this.assure = assure;
    this.attributaire = attributaire;
    this.prestation = prestation;
}



public Integer getIdBenef() {
    return idBenef;
}

public void setIdBenef(Integer idBenef) {
    this.idBenef = idBenef;
}

public Integer getNoBenef() {
    return noBenef;
}

public void setNoBenef(Integer noBenef) {
    this.noBenef = noBenef;
}

public String getNomBenef() {
    return nomBenef;
}

public void setNomBenef(String nomBenef) {
    this.nomBenef = nomBenef;
}

public Integer getNoApBenef() {
    return noApBenef;
}

public void setNoApBenef(Integer noApBenef) {
    this.noApBenef = noApBenef;
}

public LocalDate getDateNaissanceBenef() {
    return dateNaissanceBenef;
}

public void setDateNaissanceBenef(LocalDate dateNaissanceBenef) {
    this.dateNaissanceBenef = dateNaissanceBenef;
}

public CategorieBeneficiaire getCategorieBeneficiaire() {
    return categorieBeneficiaire;
}

public void setCategorieBeneficiaire(CategorieBeneficiaire categorieBeneficiaire) {
    this.categorieBeneficiaire = categorieBeneficiaire;
}

public Sexe getSexe() {
    return sexe;
}

public void setSexe(Sexe sexe) {
    this.sexe = sexe;
}

public Assure getAssure() {
    return assure;
}

public void setAssure(Assure assure) {
    this.assure = assure;
}

public Attributaire getAttributaire() {
    return attributaire;
}

public void setAttributaire(Attributaire attributaire) {
    this.attributaire = attributaire;
}

public Prestation getPrestation() {
    return prestation;
}

public void setPrestation(Prestation prestation) {
    this.prestation = prestation;
}

public LocalDate getDateEffetPrestation() {
    return dateEffetPrestation;
}

public void setDateEffetPrestation(LocalDate datteEffetPrestation) {
    this.dateEffetPrestation = datteEffetPrestation;
}

public BigDecimal getMntMensuelPrestation() {
    return mntMensuelPrestation;
}

public void setMntMensuelPrestation(BigDecimal mntMensuelPrestation) {
    this.mntMensuelPrestation = mntMensuelPrestation;
}

public Integer getTrimestreDerniereEmission() {
    return trimestreDerniereEmission;
}


public void setTrimestreDerniereEmission(Integer trimestreDerniereEmission) 
{
    this.trimestreDerniereEmission = trimestreDerniereEmission;
}


public Integer getAnneeDerniereEmission() {
    return anneeDerniereEmission;
}


public void setAnneeDerniereEmission(Integer anneeDerniereEmission) {
    this.anneeDerniereEmission = anneeDerniereEmission;
}


public Integer getEstActifBenef() {
    return estActifBenef;
}

public void setEstActifBenef(Integer estActifBenef) {
    this.estActifBenef = estActifBenef;
}

public LocalDateTime getDateheureCreationBenef() {
    return dateheureCreationBenef;
}

public void setDateheureCreationBenef(LocalDateTime dateheureCreationBenef) 
{
    this.dateheureCreationBenef = dateheureCreationBenef;
}

public Utilisateur getUtilisateur() {
    return utilisateur;
}

public void setUtilisateur(Utilisateur utilisateur) {
    this.utilisateur = utilisateur;
}





//For Test Equal, override equals() and hashCode()
@Override
public boolean equals(Object o) {

    if (this == o)
        return true;
    if (o == null || getClass() != o.getClass())
        return false;
    Beneficiaire beneficiaire = (Beneficiaire) o;

    return noBenef.equals(beneficiaire.getNoBenef());
}

@Override
public int hashCode() {
    return Objects.hash(noBenef);
}

}

PieceJustificative类:

@Entity
@Table(uniqueConstraints=
@UniqueConstraint(name="u_pj_noPj", columnNames="noPj"))
public class PieceJustificative implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer idPj;

@Column(nullable=false)
private Integer noPj;

@DateTimeFormat(pattern="dd/MM/yyyy")
@Column(nullable=false)
private LocalDate dateDebutValiditePj;

@DateTimeFormat(pattern="dd/MM/yyyy")
@Column(nullable=false)
private LocalDate dateFinValiditePj;

@DateTimeFormat(pattern="dd/MM/yyyy")
@Column(nullable=false)
private LocalDate dateSignaturePj;

@Column(columnDefinition="timestamp default current_timestamp not null", insertable=false)
private LocalDateTime dateHeureSaisiePj;

private LocalDateTime dateHeureValidationPj;

@Column(columnDefinition="integer default 0 not null", insertable=false)
private Integer estValidePj;

/*@ManyToOne(optional=false, fetch=FetchType.LAZY)
*/
@ManyToOne
@JoinColumn(name="id_type_pj",
        foreignKey=@ForeignKey(name="fk_pj_id_type_pj"),
        nullable=false)
private TypePj typePj;

/*@ManyToOne(optional=false, fetch=FetchType.LAZY)
@JoinColumn(name="id_beneficiaire",
        foreignKey=@ForeignKey(name="fk_pj_id_beneficiaire"))
*/
/*@ManyToOne(optional=false)*/
@ManyToOne
@JoinColumn(name="idBenef",
        foreignKey=@ForeignKey(name="fk_pj_id_beneficiaire"),
        nullable=false)
private Beneficiaire beneficiairePj;

@ManyToOne
@JoinColumn(name="id_utilisateur_saisie",
        foreignKey=@ForeignKey(name="fk_pj_id_utilisateur_saisie"))
private Utilisateur utilisateurSaisiePj;

@ManyToOne
@JoinColumn(name="id_utilisateur_valide",
        foreignKey=@ForeignKey(name="fk_pj_id_utilisateur_valide"))
private Utilisateur utilisateurValidationPj;



protected PieceJustificative() {
    //For JPA
}

public Integer getIdPj() {
    return idPj;
}

public void setIdPj(Integer idPj) {
    this.idPj = idPj;
}

public Integer getNoPj() {
    return noPj;
}

public void setNoPj(Integer noPj) {
    this.noPj = noPj;
}

public LocalDate getDateDebutValiditePj() {
    return dateDebutValiditePj;
}

public void setDateDebutValiditePj(LocalDate dateDebutValiditePj) {
    this.dateDebutValiditePj = dateDebutValiditePj;
}

public LocalDate getDateFinValiditePj() {
    return dateFinValiditePj;
}

public void setDateFinValiditePj(LocalDate dateFinValiditePj) {
    this.dateFinValiditePj = dateFinValiditePj;
}

public LocalDate getDateSignaturePj() {
    return dateSignaturePj;
}

public void setDateSignaturePj(LocalDate dateSignaturePj) {
    this.dateSignaturePj = dateSignaturePj;
}

public LocalDateTime getDateHeureSaisiePj() {
    return dateHeureSaisiePj;
}

public void setDateHeureSaisiePj(LocalDateTime dateHeureSaisiePj) {
    this.dateHeureSaisiePj = dateHeureSaisiePj;
}

public LocalDateTime getDateHeureValidationPj() {
    return dateHeureValidationPj;
}

public void setDateHeureValidationPj(LocalDateTime dateHeureValidationPj) {
    this.dateHeureValidationPj = dateHeureValidationPj;
}

public Integer getEstValidePj() {
    return estValidePj;
}

public void setEstValidePj(Integer estValidePj) {
    this.estValidePj = estValidePj;
}



/**
 * @return the typePj
 */
public TypePj getTypePj() {
    return typePj;
}

/**
 * @param typePj the typePj to set
 */
public void setTypePj(TypePj typePj) {
    this.typePj = typePj;
}

public Beneficiaire getBeneficiairePj() {
    return beneficiairePj;
}

public void setBeneficiairePj(Beneficiaire beneficiairePj) {
    this.beneficiairePj = beneficiairePj;

}

public Utilisateur getUtilisateurSaisiePj() {
    return utilisateurSaisiePj;
}

public void setUtilisateurSaisiePj(Utilisateur utilisateurSaisiePj) {
    this.utilisateurSaisiePj = utilisateurSaisiePj;
}

public Utilisateur getUtilisateurValidationPj() {
    return utilisateurValidationPj;
}

public void setUtilisateurValidationPj(Utilisateur utilisateurValidationPj) {
    this.utilisateurValidationPj = utilisateurValidationPj;
}

//For Test Equal, override equals() and hashCode()
@Override
public boolean equals(Object o) {

    if (this == o)
        return true;
    if (o == null || getClass() != o.getClass())
        return false;
    PieceJustificative pieceJustificative = (PieceJustificative) o;

    return noPj.equals(pieceJustificative.getNoPj());
}

@Override
public int hashCode() {
    return Objects.hash(noPj);
}

}

PieceJustificative列表得My thymeleaf呈现视图:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
  layout:decorator="layouts/layout">
<head>
<meta charset="UTF-8" />
<title>Liste des pièces justificatives</title>
<!-- <link th:href="@{/css/form.css}" rel="stylesheet"/> -->
</head>
<body>
<div id="content" layout:fragment="content">


    <div class="w3-container">
        <h2>Liste des pièces justificatives</h2>  

        <!-- <table class="w3-table-all w3-hoverable"> -->
        <table id="customers">
            <thead>
                <!-- <tr class="w3-light-grey"> -->
                <tr >
                    <th>N° pièce jointe</th>
                    <th>Type pièce jointe</th>
                    <th>N° bénéficiaire</th>
                    <th>Nom bénéficiaire</th>
                    <th>Date début validité</th>
                    <th>Date fin validité</th>
                    <th>Date signature</th>
                    <th>Modifier</th>
                    <th>Supprimer</th>
                </tr>
            </thead>
            <tr th:each="pieceJustificative : ${pieceJustificatives}">

                <td th:text="${pieceJustificative.noPj}">N°</td>
                <td th:text="${pieceJustificative.typePj.libelleTypePj}">libelleTypePj</td>
                <td th:text="${pieceJustificative.beneficiairePj.noBenef}">noBenef</td>
                <td th:text="${pieceJustificative.beneficiairePj.nomBenef}">nomBenef</td>
                <td th:text="${#temporals.format(pieceJustificative.dateDebutValiditePj, 'dd/MM/yyyy')}">dateDebutValiditePj</td>
                <td th:text="${#temporals.format(pieceJustificative.dateFinValiditePj, 'dd/MM/yyyy')}">dateFinValiditePj</td>
                <td th:text="${#temporals.format(pieceJustificative.dateSignaturePj, 'dd/MM/yyyy')}">dateSignaturePj</td>

                <td><a href="listepiecesjustificatives.html" th:href="@{/listepiecesjustificatives/modifier/{idPj}(idPj=${pieceJustificative.idPj})}">Modifier</a></td>
                <td><a href="listepiecesjustificatives.html" th:href="@{/listepiecesjustificatives/supprimer/{idPj}(idPj=${pieceJustificative.idPj})}">Supprimer</a></td>
            </tr>
        </table>
    </div>



    <p>
        <a href="/ajouterpiecejustificative">Ajouter pièce justificative</a>
    </p>

</div><!-- end div content --> 

当我试图呈现这个PieceJustificative列表时,我得到了这个异常,但是id为57271的受益人存在于我的数据库中:

null

共有1个答案

高泳
2023-03-14

尝试FETCH=FETCHTYPE。EAGER默认值为LAZY。应该管用。

 类似资料:
  • 问题内容: 尝试通过Invoice对象获取用户时出现javax.persistence.EntityNotFoundException错误 invoice.getUser()。getId() 错误如下 实体类如下(不包括getter和setter) 问题答案: 问题可能是直接实体不存在,也可能是该实体中的引用实体,通常用于EAGER访存类型,或optional = false。 尝试这个:

  • 问题内容: 我想先测试Firebase中是否存在数据,然后再添加它。但是我的方法有问题:我尝试使用此Javascript代码列出所有数据: 但是,如果该用户不存在,则会在之前添加该用户,然后我的代码会说该用户存在。您会说这是正常的,因为仅在“ child_added”时才调用我的警报,但是我不知道该怎么做。 我也尝试使用“值”事件,但我的“ message.name”为空。 我该如何解决? 问题答

  • 问题内容: 我的数据库中有两个表,分别用于我的两个用户(图书馆员和学生)的登录详细信息。我已将用户的详细信息分为两个单独的表tblUserLibrarian和tblUserStudent;一个给图书馆员,另一个给学生。每个表中的主键是用户名或ID(LibrarianID和StudentID)。 这些表的概要如下: tblUserStudent tblUserLibrarian 我有一个问题,当任何

  • 在具有管道和转发功能的MIPS体系结构上: add指令将在步骤3(执行操作)准备好结果,但我假设sw指令希望在步骤2(指令解码)得到结果 David A. Patterson的《计算机组织与设计》一书中有一个已解决的练习:在以下代码段中找到危险并重新排序指令以避免任何管道停滞: 解决方案: 在解决方案中,它正确识别加载使用危险并相应地重新排列代码,但是否也存在执行存储危险?

  • 类似于内置方法existsById,我想用Spring数据JPA创建一个existsAllById方法。为了实现这一点,我向repository界面添加了以下方法。 然而,Hibernate似乎不喜欢我对大小的使用,并向我报告了以下错误: 引起:org.hibernate.hql.internal.ast.QuerySyntaException:意外的表达式?发现集合函数大小[选择大小写当(cou

  • 问题内容: 在MySQL中,您可以像这样插入多行: 但是,尝试执行此类操作时出现错误。是否可以一次在SQLite数据库中插入多行?这样做的语法是什么? 问题答案: 更新 正如BrianCampbell在此处指出的那样,SQLite 3.7.11及更高版本现在支持原始文章的更简单语法 。但是,如果您希望在旧数据库之间实现最大兼容性,则所示的方法仍然适用。 原始答案 如果有特权,River的回答:您可