java - 无需调用 Session 对象上的 save() 方法即可持久保存对象

标签 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);
  }

}

如果您需要更多信息,请询问。谢谢...

最佳答案

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

关于java - 无需调用 Session 对象上的 save() 方法即可持久保存对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23433470/

相关文章:

java - Hibernate 方言未定义错误

java - Spring MVC + hibernate : a form with checkboxes for @manytomany relationship

java - 奇怪的 Java 类的 NoClassDefFoundError

java - 如何在 Netbeans 中包含 axet/vget 库

java - servlet doGet通过servlet注解调用启动两次

java - Spring 3 MVC : one-to-many within a dynamic form (add/remove on create/update)

java - Spring JPA @OneToMany 参数值[]与预期类型不匹配

java - 无法使用 Hibernate JPA 更新嵌套实体

java - 二叉搜索树结果中包含 0 (Java)

java - Spring Security 自定义登录错误