每次添加新记录时,在java中减去两个字段

时间:2015-08-18 11:14:34

标签: java jpa oracle10g ejb-3.0

我如何使用jpa减去两个fieldes; 我想得到soldejours(类contrat)= soldejours(classe contrat) - dureeassitance(classe assitance)

每次我坚持新的帮助时,我都要做这个操作 这是课堂援助

   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.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SecondaryTable;

@Entity
@NamedQueries({ @NamedQuery(name = "Contrat.findAll", query = "select o from Contrat o") })
public class Contrat implements Serializable {
    @Column(length = 4000)
    private String annee;
    @Column(length = 4000)
    private String client;
    @Id
    @Column(name = "ID_CONTRAT", nullable = false, length = 4000)
    private String idContrat;
    @Column(length = 4000)
    private String information;
    @Column(name = "NB_JOURS")
    private BigDecimal nbJours;
    @Column(name = "SOLDE_JOURS")
    private BigDecimal soldeJours;
    @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) {
        this.annee = annee;
        this.client = client;
        this.idContrat = idContrat;
        this.information = information;
        this.nbJours = nbJours;
        this.soldeJours = soldeJours;
    }

    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 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 javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@NamedQueries({ @NamedQuery(name = "Assistance.findAll", query = "select o from Assistance o") })
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)
    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;
    @ManyToOne(cascade= CascadeType.PERSIST)
    @JoinColumn(name = "CONTRAT_ID",updatable =true,insertable =true)
    private Contrat contrat;

    public Assistance() {
    }

    public Assistance(String client, Contrat contrat, Date dateCreation, BigDecimal dureeAssistance,
                      String etatAssistance, String idAssistance, String ingAff, String ouvertPar, String sujetAss) {
        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;
    }

    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.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;
    }
}

1 个答案:

答案 0 :(得分:0)

使用实体侦听器和回调方法。

@PrePersist and @PostPersist
@PreUpdate and @PostUpdate
@PreRemove and @PostRemove
@PostLoad

https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/listeners.html