Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cascade persist if object not in database, otherwise merge?

@Entity
public class Auction{
    @ManyToOne(cascade=CascadeType.MERGE)
    private Member seller;

    @OneToOne(cascade=CascadeType.PERSIST)
    private Question question;
}

@Entity
public class Member{

}

@Entity
public class Question{
    @ManyToOne(cascade=CascadeType.MERGE)
    private Member personAsking;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
}



em.persist(auction);

How do I persist member if it doesn't exist, otherwise merge (update) it?

like image 364
Tim Avatar asked Dec 19 '25 01:12

Tim


1 Answers

Add to your Auction.seller field CascadeType.PERSIST. Like this:

@Entity
public class Auction{
    @ManyToOne(cascade={CascadeType.MERGE, CascadeType.PERSIST})
    private Member seller;

    .....
}
like image 122
V G Avatar answered Dec 20 '25 22:12

V G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!