hibernate同时持有两个对象

时间:2015-09-27 20:36:07

标签: hibernate postgresql persistence

我有两个在它们之间具有OneToOne关系的对象:项目捐赠项目对象将一些逻辑转发到位于项目对象内的捐赠对象(捐赠表shopuld时填充< em>项目已填写。我的目标是在项目持久化时保持捐赠对象。我的课程:

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
}

在这里,我想坚持项目,但也要坚持捐赠。但是现在它不起作用((只有项目表填充。不是捐赠表。是否可以同时保留它们 - 捐款 项目填充时填充的表格?

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

捐赠表中,外键是 donation_project_id 列。在项目 - 主键 project_id

1 个答案:

答案 0 :(得分:1)

您需要将注册类型添加到注释中,如下所示:

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

Hibernate documentation about it

更新:

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