如何在一对多关系中减去两个字段

时间:2016-02-17 09:57:36

标签: java jpa entitymanager

我想在每次添加新记录时从另一个字段中减去一个值,这是我的情况:示例

soldeJours(来自实体合约)= nbjours(来自实体合约) - dureeAssistance(来自实体协助):

例如:

nbjours = 20当我添加新援助dureeassistance = 5 soldeJours = 15时添加其他帮助duréeadistance= 3 soldeJours = 12

每次我在帮助中添加一条新记录时,soldejours会自动递减并持久保存到db并在界面中显示。

Ps:我在合同和协助之间有一对多的关系(合同可以有多个协助)

这是我的实体Contrat

package model.entitie;

import java.io.Serializable;

import java.math.BigDecimal;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SecondaryTable;
import javax.persistence.SequenceGenerator;

@Entity
@NamedQueries({ @NamedQuery(name = "Contrat.findAll", query = "select o from Contrat o") })
@SequenceGenerator(name = "Contrat_Id_Seq_Gen", sequenceName = "CONTRAT_SEQ", allocationSize = 1, initialValue = 50)
public class Contrat implements Serializable {
    @SuppressWarnings("compatibility:-6627285401934383829")
    private static final long serialVersionUID = 1L;
    @Column(length = 4000)
    private String annee;
    @Column(length = 4000)
    private String client;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Contrat_Id_Seq_Gen")
    @Column(name = "ID_CONTRAT", nullable = false, length = 4000)
    private String idContrat;
    @Column(length = 4000)
    private String information;
    @Column(name = "NB_JOURS")
    public BigDecimal nbJours;
    @Column(name = "SOLDE_JOURS")
    public BigDecimal soldeJours;
    @Column (name="ETATCON")
    private String etatCon;

    public void setEtatCon(String etatCon) {
        this.etatCon = etatCon;
    }

    public String getEtatCon() {
        return etatCon;
    }
    @OneToMany(mappedBy = "contrat"  ,cascade=CascadeType.PERSIST)
    private List<Assistance> assistanceList;



    public Contrat() {
    }

    public Contrat(String annee, String client, String idContrat, String information, BigDecimal nbJours,
                   BigDecimal soldeJours, String etatCon) {
        this.annee = annee;
        this.client = client;
        this.idContrat = idContrat;
        this.information = information;
        this.nbJours = nbJours;
        this.soldeJours = soldeJours;
        this.etatCon = etatCon;
    }

    public String getAnnee() {
        return annee;
    }

    public void setAnnee(String annee) {
        this.annee = annee;
    }

    public String getClient() {
        return client;
    }

    public void setClient(String client) {
        this.client = client;
    }

    public String getIdContrat() {
        return idContrat;
    }

    public void setIdContrat(String idContrat) {
        this.idContrat = idContrat;
    }

    public String getInformation() {
        return information;
    }

    public void setInformation(String information) {
        this.information = information;
    }

    public BigDecimal getNbJours() {
        return nbJours;
        }

    public void setNbJours(BigDecimal nbJours) {

        this.nbJours = nbJours;
    }
   /*
    public void setSoldeJours(BigDecimal soldeJours) {
        this.soldeJours = soldeJours.subtract(nbJours);
    }*/

    public BigDecimal getSoldeJours() {

        return soldeJours;
    }

    public void setSoldeJours(BigDecimal soldeJours) {
        this.soldeJours = soldeJours;
    }

    public List<Assistance> getAssistanceList() {
        return assistanceList;
    }

    public void setAssistanceList(List<Assistance> assistanceList) {
        this.assistanceList = assistanceList;
    }

    public Assistance addAssistance(Assistance assistance) {
        getAssistanceList().add(assistance);
        assistance.setContrat(this);
        return assistance;
    }

    public Assistance removeAssistance(Assistance assistance) {
        getAssistanceList().remove(assistance);
        assistance.setContrat(null);
        return assistance;
    }
}

这是我的援助实体:

package model.entitie;

import java.io.Serializable;

import java.math.BigDecimal;

import java.util.Date;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@NamedQueries({ @NamedQuery(name = "Assistance.findAll", query = "select o from Assistance o") })
@SequenceGenerator(name = "Assistance_Id_Seq_Gen", sequenceName = "ASSISTANCE_SEQ", allocationSize = 1,
                   initialValue = 50)
public class Assistance implements Serializable {
    private static final long serialVersionUID = 7916354262572083045L;
    @Column(length = 4000)
    private String client;
    @Temporal(TemporalType.DATE)
    @Column(name = "DATE_CREATION")
    private Date dateCreation;
    @Column(name = "DUREE_ASSISTANCE")
    private BigDecimal dureeAssistance;
    @Column(name = "ETAT_ASSISTANCE", length = 4000)
    private String etatAssistance;
    @Id
    @Column(name = "ID_ASSISTANCE", nullable = false, length = 4000)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Assistance_Id_Seq_Gen")
    private String idAssistance;
    @Column(name = "ING_AFF", length = 4000)
    private String ingAff;
    @Column(name = "OUVERT_PAR", length = 4000)
    private String ouvertPar;
    @Column(name = "SUJET_ASS", length = 4000)
    private String sujetAss;
    @Column(name = "ETATASS", length = 4000)
    private String etatAss;
    @Column(name="DEP", length=4000)
    private String dep;

    public void setDep(String dep) {
        this.dep = dep;
    }

    public String getDep() {
        return dep;
    }

    public void setEtatAss(String etatAss) {
        this.etatAss = etatAss;
    }

    public String getEtatAss() {
        return etatAss;
    }
    @ManyToOne
    @JoinColumn(name = "CONTRAT_ID", updatable = true, insertable = true)
    private Contrat contrat;
    @OneToMany(mappedBy = "assistance", cascade =CascadeType.PERSIST)
    private List<Intervention> interventionList;

    public Assistance() {
    }

    public Assistance(Contrat contrat, Date dateCreation, BigDecimal dureeAssistance, String etatAssistance,
                      String idAssistance, String ingAff, String ouvertPar, String sujetAss, String dep,String client) {
        this.client = client;
        this.contrat = contrat;
        this.dateCreation = dateCreation;
        this.dureeAssistance = dureeAssistance;
        this.etatAssistance = etatAssistance;
        this.idAssistance = idAssistance;
        this.ingAff = ingAff;
        this.ouvertPar = ouvertPar;
        this.sujetAss = sujetAss;
        this.etatAssistance = etatAssistance;
        this.dep =dep;

        this.contrat.soldeJours = this.contrat.soldeJours.subtract(this.dureeAssistance);
    }

    public String getClient() {
        return client;

    }

    public void setClient(String client) {
        this.client = client;
    }


    public Date getDateCreation() {
        return dateCreation;
    }

    public void setDateCreation(Date dateCreation) {
        this.dateCreation = dateCreation;
    }

    public BigDecimal getDureeAssistance() {
        return dureeAssistance;
    }

    public void setDureeAssistance(BigDecimal dureeAssistance) {


        // this.contrat.nbJours = this.contrat.soldeJours.subtract(dureeAssistance);
        this.dureeAssistance = dureeAssistance;

    }

    public String getEtatAssistance() {
        return etatAssistance;
    }

    public void setEtatAssistance(String etatAssistance) {
        this.etatAssistance = etatAssistance;
    }

    public String getIdAssistance() {
        return idAssistance;
    }

    public void setIdAssistance(String idAssistance) {
        this.idAssistance = idAssistance;
    }

    public String getIngAff() {
        return ingAff;
    }

    public void setIngAff(String ingAff) {
        this.ingAff = ingAff;
    }

    public String getOuvertPar() {
        return ouvertPar;
    }

    public void setOuvertPar(String ouvertPar) {
        this.ouvertPar = ouvertPar;
    }

    public String getSujetAss() {
        return sujetAss;
    }

    public void setSujetAss(String sujetAss) {
        this.sujetAss = sujetAss;
    }

    public Contrat getContrat() {
        return contrat;
    }

    public void setContrat(Contrat contrat) {
        this.contrat = contrat;
    }

    public List<Intervention> getInterventionList() {
        return interventionList;
    }

    public void setInterventionList(List<Intervention> interventionList) {
        this.interventionList = interventionList;
    }
    public Intervention addIntervention (Intervention intervention)
    {
        getInterventionList().add(intervention);
        intervention.setAssistance(this);
        return intervention;
        }

}

这就是我坚持我的实体援助的方式:

public void addAssistanceRecord(Date dateCreation,BigDecimal dureeAssistance,String etatAssistance,                   String idAssistance,String ingAff,String ouvertPar,String sujetAss,String dep,String client,String idContrat){

    Assistance assistance = new Assistance();
    assistance.setClient(client);
    assistance.setDateCreation(dateCreation);
    assistance.setDep(dep);
    assistance.setDureeAssistance(dureeAssistance);
    assistance.setIngAff(ingAff);
    assistance.setOuvertPar(ouvertPar);
    assistance.setSujetAss(sujetAss);
    assistance.setIdAssistance(idAssistance);
    assistance.setEtatAssistance(etatAssistance);

    //setting the contratid explicitly
      Contrat contrat = new Contrat();
         contrat.setIdContrat(idContrat);
             assistance.setContrat(contrat);

   //persist the assistance object data to ASS table
   em.persist(assistance);

              }

1 个答案:

答案 0 :(得分:0)

我找到了这个解决方案并且工作正常

 public void addAssistanceRecord(Date dateCreation, BigDecimal dureeAssistance, String etatAssistance,
                      String idAssistance, String ingAff, String ouvertPar, String sujetAss, String dep,String client,String idContrat) {


            Assistance assistance = new Assistance();
            assistance.setClient(client);
            assistance.setDateCreation(dateCreation);
            assistance.setDep(dep);
            assistance.setDureeAssistance(dureeAssistance);
            assistance.setIngAff(ingAff);
            assistance.setOuvertPar(ouvertPar);
            assistance.setSujetAss(sujetAss);
            assistance.setIdAssistance(idAssistance);
            assistance.setEtatAssistance(etatAssistance);

            //setting the contratid explicitly
              Contrat contrat = new Contrat();
                 contrat.setIdContrat(idContrat);
                     assistance.setContrat(contrat);

           //persist the assistance object data to ASS table
           em.persist(assistance);

                          Contrat con1 =assistance.getContrat();
                          String idCon= con1.getIdContrat();
                          System.out.println("id contrat "+ idCon);


         Query query =
             em.createQuery("SELECT a from Contrat a where a.idContrat =:id");

         query.setParameter("id", idCon);
         Contrat con2 = (Contrat) query.getSingleResult();


         System.out.println("id contrat 2 "+ con2.getIdContrat());
         System.out.println("nb jr contrat 2 "+ con2.getNbJours());

         BigDecimal nbJours = con2.getNbJours();
         BigDecimal sldj =con2.getSoldeJours();



         if(con2.getSoldeJours()==null){
             nbJours = nbJours.subtract(assistance.getDureeAssistance());

              con2.setSoldeJours(nbJours);
                     em.merge(con2);

         }
         else{
                 nbJours = sldj.subtract(assistance.getDureeAssistance());

                  con2.setSoldeJours(nbJours);
                         em.merge(con2);

         }
     }