持久化对象而不调用Session对象上的save()方法

时间:2014-05-02 17:08:02

标签: java hibernate

我遇到了Hibernate 4.2.3和JPA 2.0的问题。这些对象是持久化的,无需从Hibernate调用Session对象的save()方法。

这是我的Bean:

package com.elver.ichante.business.beans;

import com.elver.ichante.business.items.Collateral;
import com.elver.ichante.business.items.Status;
import com.elver.ichante.business.items.LoanType;
import com.elver.ichante.business.util.LoanUtil;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by Elver on 3/26/14.
 */
@Entity
@Table
public class Loan implements Serializable{

    private static final long serialVersionUID = 43847384358452043L;
//Todo Function to calculate the amount needed to complete the full payment.
    //Constructor

    /**
     * Deafult Construtor
     */
    public Loan(){}

    public Loan(Double amount, LoanType tipo, Double amountPago)
    {
        this.amount = amount;
        this.loanType = tipo;
        this.creationDate = new GregorianCalendar();
        this.finishedDate = new GregorianCalendar();
        finishedDate.add(GregorianCalendar.DAY_OF_MONTH, 45);
        this.status = Status.ALDIA;
        payments = LoanUtil.getPagos(this, amountPago);
        collaterals = new ArrayList<Collateral>();
    }

    public Loan(Double amount, LoanType tipo, Double amountPago, GregorianCalendar creationDate)
    {
        this.amount = amount;
        this.loanType = tipo;
        this.creationDate = creationDate;
        this.finishedDate = new GregorianCalendar();
        finishedDate.add(GregorianCalendar.DAY_OF_MONTH, 45);
        this.status = Status.ALDIA;
        payments = LoanUtil.getPagos(this, amountPago);
        collaterals = new ArrayList<Collateral>();
    }

    /* Fields */
    private Integer id;
    private Client client;
    private Double amount;
    private Calendar creationDate;
    private Calendar finishedDate;
    private List<Payment> payments = new ArrayList<Payment>();
    private Status status;
    private Boolean wasDelayed;
    private Integer timesDelayed;
    private Boolean guaranteed;
    private List<Collateral> collaterals;
    private LoanType loanType;
    private Employee conductor;

    @OneToOne(cascade = {CascadeType.MERGE})
    public Employee getConductor()
    {
        return conductor;
    }

    public void setConductor(Employee conductor)
    {
        this.conductor = conductor;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name ="Loan_Id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "Client_Id")
    public Client getClient() {
        return client;
    }

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

    @Column
    @NotNull
    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    @Column
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    public Calendar getCreatedDate() {
        return creationDate;
    }

    public void setCreatedDate(Calendar creationDate) {
        this.creationDate = creationDate;
    }

    @Column
    @Temporal(TemporalType.TIMESTAMP)
    public Calendar getFinishedCalendar() {
        return finishedDate;
    }

    public void setFinishedCalendar(Calendar finishedCalendar) {
        this.finishedDate = finishedCalendar;
    }

    /**
     * Return a list of payments.
     * @return A list containing all the Payment objects.
     */
    @LazyCollection(LazyCollectionOption.FALSE)
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "loan")
    public List<Payment> getPayments() {
        return payments;
    }

    public void setPayments(List<Payment> payments) {
        this.payments = payments;
    }

    /**
     * Add a payment.
     * @param payment
     */
    public void addPayment(Payment payment)
    {
        if(payment != null)
        {
            payment.setLoan(this);
            payments.add(payment);
        }
    }


    @Column
    @NotNull
    @Enumerated(EnumType.STRING)
    /**
     *
     * @return the status of this object, null otherwise.
     */
    public Status getStatus()
    {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public Boolean getWasDelayed() {
        return wasDelayed;
    }

    public void setWasDelayed(Boolean wasDelayed) {
        this.wasDelayed = wasDelayed;
    }

    public Integer getTimesDelayed() {
        return timesDelayed;
    }

    public void setTimesDelayed(Integer timesDelayed) {
        this.timesDelayed = timesDelayed;
    }

    @Column
    public Boolean getGuaranteed() {
        return guaranteed;
    }

    public void setGuaranteed(Boolean guaranteed) {
        this.guaranteed = guaranteed;
    }

    @LazyCollection(LazyCollectionOption.FALSE)
    @OneToMany(cascade = CascadeType.MERGE)
    public List<Collateral> getCollaterals()
    {
        return collaterals;
    }

    public void addGarantiaPrendaria(Collateral garantia)
    {
        if(garantia != null)
        {
            collaterals.add(garantia);
        }
    }

    public void setCollaterals(List<Collateral> collaterals)
    {
        this.collaterals = collaterals;
    }

    @Column
    @NotNull
    @Enumerated(EnumType.STRING)
    public LoanType getLoanType() {
        return loanType;
    }

    public void setLoanType(LoanType loanType) {
        this.loanType = loanType;
    }    


}

当我运行以下代码时,该对象将保持不变。

public class Test {
public static void main(String[] args){
     Loan loan = new Loan(130000d, LoanType.DIARIO, 130d);
  }

}

如果您需要更多信息,请索取。感谢...

1 个答案:

答案 0 :(得分:1)

我只看到一种可能性。这是LoanUtil.getPagos方法将您的对象持久化到数据库。检查一下。