bob-cac
bob-cac

Reputation: 1302

jpa creating new entity while deleting old one that has the same unique key

I have entity zika1

public class zika1 {
@Id
int id;
String sika;
@OneToMany(cascade=CascadeType.ALL,orphanRemoval=true)
@JoinColumn
List<zika2> zikas=new ArrayList<zika2>();}

and entity zika2

@Table(name = "zika2", uniqueConstraints = @UniqueConstraint(columnNames = { "b" }))

public class zika2 {

@Id
String a;
@Column(unique=true)
String b;}

and this method:

    public void testuniqu()
{

        zika1 zik=super.find(1);

        zika2 zik22=new zika2();

        zik22.setA("karkar");
            zik22.setB(zik.getZikas().get(0).getB());
            zik.getZikas().remove(0);
            zik.getZikas().add(zik22);
            super.save(zik);        
    }

this throw a unique constraint exception but actually i am deleting the first entity . so jpa(eclipse link) not running this operation in the same db transaction. is there a way to tell jpa to run the delete before the insert operation for the new zika2 entity?

Upvotes: 1

Views: 326

Answers (1)

00Enthusiast
00Enthusiast

Reputation: 103

I think that is a normal flow, because EclipseLink doesn't execute commands in order you write it. So you need to tell EclipseLink some hint, that "delete" operation should be the first one. According to : http://wiki.eclipse.org/Using_Advanced_Unit_of_Work_API_%28ELUG%29#How_to_Use_the_setShouldPerformDeletesFirst_Method_of_the_Unit_of_Work, you should use setShouldPerformDeleteFirst.

Upvotes: 2

Related Questions