我无法使用Spring-data Jpa(hibernate)从我的数据库中删除实体。当我从JpaRepository调用hibernate的方法删除时,没有错误或异常,但我的FactureSortie实体没有被删除。 这是我的模特:
Facture,FactureSortie的超类:
package be.afelio.GEDFacture.entities;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
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.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author johan
*/
@Entity
@Table(name = "facture")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Facture.findAll", query = "SELECT f FROM Facture f")
, @NamedQuery(name = "Facture.findByIdFacture", query = "SELECT f FROM Facture f WHERE f.idFacture = :idFacture")
, @NamedQuery(name = "Facture.findByMontantHTVA", query = "SELECT f FROM Facture f WHERE f.montantHTVA = :montantHTVA")
, @NamedQuery(name = "Facture.findByMontantTVAC", query = "SELECT f FROM Facture f WHERE f.montantTVAC = :montantTVAC")
, @NamedQuery(name = "Facture.findByMontantTVA", query = "SELECT f FROM Facture f WHERE f.montantTVA = :montantTVA")
, @NamedQuery(name = "Facture.findByDateFacturation", query = "SELECT f FROM Facture f WHERE f.dateFacturation = :dateFacturation")
, @NamedQuery(name = "Facture.findByNumerocomptable", query = "SELECT f FROM Facture f WHERE f.numerocomptable = :numerocomptable")
, @NamedQuery(name = "Facture.findByUuid", query = "SELECT f FROM Facture f WHERE f.uuid = :uuid")
, @NamedQuery(name = "Facture.findByNumeroPiece", query = "SELECT f FROM Facture f WHERE f.numeroPiece = :numeroPiece")
, @NamedQuery(name = "Facture.findByValidee", query = "SELECT f FROM Facture f WHERE f.validee = :validee")})
public class Facture implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_Facture")
private Integer idFacture;
@Basic(optional = false)
@Column(name = "Montant_HTVA")
private double montantHTVA;
@Basic(optional = false)
@Column(name = "Montant _TVAC")
private double montantTVAC;
@Basic(optional = false)
@Column(name = "Montant_TVA")
private double montantTVA;
@Basic(optional = false)
@Column(name = "Date_Facturation")
@Temporal(TemporalType.DATE)
private Date dateFacturation;
@Column(name = "Numero_comptable")
private Integer numerocomptable;
@Basic(optional = false)
@Column(name = "uuid")
private String uuid;
@Basic(optional = false)
@Column(name = "numero_piece")
private int numeroPiece;
@Column(name = "validee")
private Boolean validee;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "facture")
private FactureSortie factureSortie;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "facture")
private FactureEntree factureEntree;
@JoinColumn(name = "imputation", referencedColumnName = "Imputation")
@ManyToOne
private Projet imputation;
public Facture() {
}
public Facture(Integer idFacture) {
this.idFacture = idFacture;
}
public Facture(Integer idFacture, double montantHTVA, double montantTVAC, double montantTVA, Date dateFacturation, String uuid, int numeroPiece) {
this.idFacture = idFacture;
this.montantHTVA = montantHTVA;
this.montantTVAC = montantTVAC;
this.montantTVA = montantTVA;
this.dateFacturation = dateFacturation;
this.uuid = uuid;
this.numeroPiece = numeroPiece;
}
public Integer getIdFacture() {
return idFacture;
}
public void setIdFacture(Integer idFacture) {
this.idFacture = idFacture;
}
public double getMontantHTVA() {
return montantHTVA;
}
public void setMontantHTVA(double montantHTVA) {
this.montantHTVA = montantHTVA;
}
public double getMontantTVAC() {
return montantTVAC;
}
public void setMontantTVAC(double montantTVAC) {
this.montantTVAC = montantTVAC;
}
public double getMontantTVA() {
return montantTVA;
}
public void setMontantTVA(double montantTVA) {
this.montantTVA = montantTVA;
}
public Date getDateFacturation() {
return dateFacturation;
}
public void setDateFacturation(Date dateFacturation) {
this.dateFacturation = dateFacturation;
}
public Integer getNumerocomptable() {
return numerocomptable;
}
public void setNumerocomptable(Integer numerocomptable) {
this.numerocomptable = numerocomptable;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getNumeroPiece() {
return numeroPiece;
}
public void setNumeroPiece(int numeroPiece) {
this.numeroPiece = numeroPiece;
}
public Boolean getValidee() {
return validee;
}
public void setValidee(Boolean validee) {
this.validee = validee;
}
public FactureSortie getFactureSortie() {
return factureSortie;
}
public void setFactureSortie(FactureSortie factureSortie) {
this.factureSortie = factureSortie;
}
public FactureEntree getFactureEntree() {
return factureEntree;
}
public void setFactureEntree(FactureEntree factureEntree) {
this.factureEntree = factureEntree;
}
public Projet getImputation() {
return imputation;
}
public void setImputation(Projet imputation) {
this.imputation = imputation;
}
@Override
public int hashCode() {
int hash = 0;
hash += (idFacture != null ? idFacture.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Facture)) {
return false;
}
Facture other = (Facture) object;
if ((this.idFacture == null && other.idFacture != null) || (this.idFacture != null && !this.idFacture.equals(other.idFacture))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Models.Facture[ idFacture=" + idFacture + " ]";
}
}
FactureSortie:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package be.afelio.GEDFacture.entities;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
/**
*
* @author johan
*/
@Entity
@Table(name = "facture_sortie")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "FactureSortie.findAll", query = "SELECT f FROM FactureSortie f")
, @NamedQuery(name = "FactureSortie.findByIdFacture", query = "SELECT f FROM FactureSortie f WHERE f.idFacture = :idFacture")})
public class FactureSortie implements Serializable {
private static final long serialVersionUID = 1L;
@Id
// @GeneratedValue(strategy = GenerationType.IDENTITY)
// @Basic(optional = false)
@GeneratedValue(generator="sharedKeyFactureSort")
@GenericGenerator(name="sharedKeyFactureSort", strategy = "foreign", parameters = @Parameter(name="property", value="facture"))
@Column(name = "id_facture")
private Integer idFacture;
@JoinColumn(name = "id_client", referencedColumnName = "id_client")
@ManyToOne(optional = false)
private Client idClient;
@JoinColumn(name = "id_commande", referencedColumnName = "id_commande")
@ManyToOne(optional = false)
private Commande idCommande;
@JoinColumn(name = "id_facture", referencedColumnName = "id_Facture", insertable = false, updatable = true)
@OneToOne(optional = false)
private Facture facture;
@JoinColumn(name = "id_prestation", referencedColumnName = "id_prestation")
@ManyToOne
private Prestation idPrestation;
@JoinColumn(name = "id_type_vente", referencedColumnName = "id")
@ManyToOne(optional = false)
private TypeVente idTypeVente;
public FactureSortie() {
}
public FactureSortie(Integer idFacture) {
this.idFacture = idFacture;
}
public Integer getIdFacture() {
return idFacture;
}
public void setIdFacture(Integer idFacture) {
this.idFacture = idFacture;
}
public Client getIdClient() {
return idClient;
}
public void setIdClient(Client idClient) {
this.idClient = idClient;
}
public Commande getIdCommande() {
return idCommande;
}
public void setIdCommande(Commande idCommande) {
this.idCommande = idCommande;
}
public Facture getFacture() {
return facture;
}
public void setFacture(Facture facture) {
this.facture = facture;
}
public Prestation getIdPrestation() {
return idPrestation;
}
public void setIdPrestation(Prestation idPrestation) {
this.idPrestation = idPrestation;
}
public TypeVente getIdTypeVente() {
return idTypeVente;
}
public void setIdTypeVente(TypeVente idTypeVente) {
this.idTypeVente = idTypeVente;
}
@Override
public int hashCode() {
int hash = 0;
hash += (idFacture != null ? idFacture.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof FactureSortie)) {
return false;
}
FactureSortie other = (FactureSortie) object;
if ((this.idFacture == null && other.idFacture != null) || (this.idFacture != null && !(this.idFacture == other.idFacture))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Models.FactureSortie[ idFacture=" + idFacture + " ]";
}
}
这是我的FactureSortieService,它叫FactureSortieDAO:
package be.afelio.GEDFacture.business.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import be.afelio.GEDFacture.business.ClientService;
import be.afelio.GEDFacture.business.CommandeService;
import be.afelio.GEDFacture.business.FactureService;
import be.afelio.GEDFacture.business.FactureSortieService;
import be.afelio.GEDFacture.business.ProjectService;
import be.afelio.GEDFacture.dao.FactureSortieDAO;
import be.afelio.GEDFacture.dto.ClientDto;
import be.afelio.GEDFacture.dto.FactureSortieDto;
import be.afelio.GEDFacture.dto.ProjetDto;
import be.afelio.GEDFacture.entities.Client;
import be.afelio.GEDFacture.entities.FactureSortie;
import be.afelio.GEDFacture.entities.Projet;
import be.afelio.GEDFacture.tools.DtoConverter;
@Component
public class FactureSortieServiceImpl implements FactureSortieService {
@Autowired
private FactureSortieDAO factureSortieDAO;
@Autowired
private ProjectService projetService;
@Autowired
private FactureService factureService;
@Autowired
private CommandeService commandeService;
@Autowired
private ClientService clientService;
private Mapper mapper = new DozerBeanMapper();
/**
* @param facture
*
* call the add method of different services to add data in
* database. call createProjet to add a project in database and
* get the new projectid call add of factureService to add a
* facture in database and get the new factureid call addCommande
* of the commandeService to add a commande and get the new
* commandeid Set the id of the facture to null because hibernate
* doesn't handle the non-null id to add save the facture and get
* it.
*/
@Override
@Transactional
public FactureSortieDto addFacture(FactureSortieDto facture) {
try {
FactureSortie sortie = DtoConverter.convertFactureDto(facture);
sortie.getFacture().setImputation(projetService.createProjet(sortie.getFacture().getImputation()));
sortie.setIdClient(clientService.createClient(sortie.getIdClient()));
sortie.setFacture(factureService.add(sortie.getFacture()));
sortie.setIdCommande(commandeService.addCommande(sortie.getIdCommande()));
// facture.setIdFacture(fa.getFacture().getIdFacture());
sortie = factureSortieDAO.save(sortie);
return DtoConverter.convertFacture(sortie);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Transactional
@Override
public List<FactureSortieDto> FindAll(int page, int size) {
int numeroPage = 0;
int taille = size == 0 ? 10 : size;
numeroPage = page / taille;
List<FactureSortie> factures = factureSortieDAO.findAll(new PageRequest(numeroPage, taille)).getContent();
List<FactureSortieDto> facturesDto = new ArrayList<>();
for (FactureSortie facture : factures) {
facturesDto.add(DtoConverter.convertFacture(facture));
}
return facturesDto;
}
@Override
public List<FactureSortieDto> find(Date dateStart, Date dateEnd, Integer pieceNumberStart, Integer pieceNumberEnd,
ClientDto idClient, ProjetDto projet, Double montantTVACStart, Double montantTVACEnd, List<String> UUIDs,
int first, int max, String sortField, String sortOrder) {
Mapper mapper = new DozerBeanMapper();
List<FactureSortie> factures;
Client client=null;
if(idClient!=null)
client= mapper.map(idClient, Client.class);
Projet project=null;
if(projet!=null)
project = mapper.map(projet, Projet.class);
factures = this.factureSortieDAO.findWithCriterias(dateStart, dateEnd, pieceNumberStart, pieceNumberEnd, client,
project, montantTVACStart, montantTVACEnd, UUIDs, first, max, sortField, sortOrder);
List<FactureSortieDto> facturesDto = new ArrayList<>();
for (FactureSortie facture : factures) {
facturesDto.add(DtoConverter.convertFacture(facture));
}
return facturesDto;
}
@Override
public long count(Date dateStart, Date dateEnd, Integer pieceNumberStart, Integer pieceNumberEnd,
ClientDto idClient, ProjetDto projet, Double montantTVACStart, Double montantTVACEnd, List<String> UUIDs) {
Client client=null;
if(idClient!=null)
client= mapper.map(idClient, Client.class);
Projet project=null;
if(projet!=null)
project = mapper.map(projet, Projet.class);
return this.factureSortieDAO.Count(dateStart, dateEnd, pieceNumberStart, pieceNumberEnd, client, project,
montantTVACStart, montantTVACEnd, UUIDs);
}
@Override
public long countAll() {
return this.factureSortieDAO.count();
}
@Override
@Transactional(readOnly=false)
public void deleteFacture(FactureSortieDto f) {
FactureSortie facture = DtoConverter.convertFactureDto(f);
facture=this.factureSortieDAO.findOne(facture.getIdFacture());
this.factureSortieDAO.delete(facture);
this.factureService.delete(facture.getFacture());
}
}
这是我的FactureSortieDAO:
package be.afelio.GEDFacture.dao;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional;
import be.afelio.GEDFacture.entities.FactureSortie;
public interface FactureSortieDAO extends JpaRepository<FactureSortie, Integer>,FactureSortieDAOCustom{
List<FactureSortie> findAll(Sort sort);
}