Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Migrating from OpenJPA - Non null constraints [PostgreSQL]

I tried recreating it following what you did, but I get no exception. I only get an exception if I don't set EntityB. Here are my entities:

@Entity
public class EntityA {
@Id
private int id;

@NotNull
@ManyToOne
private EntityB entityb;
public void setId(int id) {
this.id = id;
}

public void setEntityb(EntityB entityb) {
this.entityb = entityb;
}
}

@Entity
public class EntityB {
@Id
private int id;
public void setId(int id) {
this.id = id;
}
}

Then in my main class, I did the following:

....
EntityB entityb = new EntityB();
entityb.setId(1);
em.getTransaction().begin();
em.persist(entityb);
em.flush();
em.getTransaction().commit();

EntityA entitya = new EntityA();
entitya.setId(2);
entitya.setEntityb(entityb);
em.getTransaction().begin();
em.persist(entitya);
em.flush();
em.getTransaction().commit();


On Fri, Nov 14, 2014 at 9:10 AM, Dalia Abo Sheasha <daliasheasha@xxxxxxxxx> wrote:
Could you possibly attach your entities?

On Thu, Nov 13, 2014 at 6:27 PM, Hal Hildebrand <hal.hildebrand@xxxxxx> wrote:
Also, I should note that entity B has an ID that is already populated.  The id is assigned, not generated.

> On Nov 13, 2014, at 4:15 PM, Hal Hildebrand <hal.hildebrand@xxxxxx> wrote:
>
> So this is a weird one.  I have an entity A which has a FK to another entity B.  Entity A has a non null constraint on the FK to entity B.  The constraint is marked deferrable, initially deferred.  And in my test, I create entity B first, persist it. flush the entity manager.  Then I create entity A, set the value to entity B, and persist and flush.   I have a @ManyToOne annotation on the FK reference to entity B in entity A.
>
> The problem is that I get an exception informing me that the non null constraint has been violated.  And, indeed, the insert being performed has the value null for entity B, despite having explicitly set this in entity A.
>
> I’m kind of scratching my head here.  Surely, I must be doing something wrong because this seems like this would be an extremely common problem for any non trivial schema.

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/eclipselink-users



Back to the top