hibernate 同时保留两个对象

hibernate persist two objects simultaneously

我有两个对象,它们之间具有 OneToOne 关系:ProjectDonationProject 对象将一些逻辑传递给位于 Project 对象( 内部的 Donation 对象donations table should be filled when projects is filled. 我的目标是让 Donation 对象被持久化当 Project 被持久化时。我的 类:

Project.java

@Entity
@Table(name="projects")
public class Project{

    private int id;
    private String name;

    public Donation donation;

    public Project() {}

    public Project(String name, Donation donation) {
        this.name = name;
        this.donation = donation;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="project_id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    @Column(name="project_name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @OneToOne
    @JoinColumn(name = "project_id")
    public DonationLogic getDonation() {
        return donation;
    }

    public void setDonation(DonationLogic donation) {
        this.donation = donation;
    }
}

Donation.class

@Entity
@Table(name="donations")
public class Donation {
    private int totalAmount;
    private int daysLeft;
    private double collectAmount;
    private int id;
    private Project project;

    public Donation() {}

    public Donation(int totalAmount, int daysLeft) {
        if(totalAmount <= 0 || daysLeft <= 0) {
            throw new IllegalArgumentException();
        }
        this.totalAmount = totalAmount;
        this.daysLeft = daysLeft;
        this.collectAmount = 0.0;
    }

    public Donation(int totalAmount, int daysLeft, double collectedamount) {
        if(totalAmount <= 0 || daysLeft <= 0) {
            throw new IllegalArgumentException();
        }
        this.totalAmount = totalAmount;
        this.daysLeft = daysLeft;
        this.collectAmount = collectedamount;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="donation_id")
    public int getId() {
        return id;
    }

    @OneToOne
    @JoinColumn(name="donation_project_id")
    public Project getProject() {
        return project;
    }

public void setProject(Project project) {
        this.project = project;
    }
//other getters/ setters
}

在这里,我想坚持 Project,但也要坚持 Donation。但是目前它不起作用(((只有 projects table 被填充。不是 donations table。是吗可以同时保留它们 - donations table filled when projects filled?

Main.java

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
        SessionFactory sessionfactory = (SessionFactory) context.getBean("sessionFactory");
        Session session = sessionfactory.getCurrentSession();
        Transaction tx = session.beginTransaction();
        Donation donation = new Donation(10000, 50);
        Project project1 = new Project("sport arena", "arena in Kiev",  donation);
        session.persist(project1);//only projects table is persisted. Not donations. 
}

donations table 中,外键是列 donation_project_id。在 projects - 主键 project_id.

您需要像这样将级联类型添加到注释中:

@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "project_id")
public DonationLogic getDonation() {
    return donation;
}

Hibernate documentation about it

更新:

在双向关联中,关系的一侧必须拥有该关联。 This is a good example