users@glassfish.java.net

TopLink-Essentials and "parent key not found'

From: <glassfish_at_javadesktop.org>
Date: Thu, 19 Jul 2007 14:36:43 PDT

I have got two entities (Batch and Fare) with a bidirectional relationship between them. Both entities use GeneratedValue annotation to generate primary keys and Fare entity is the owner of the relationship using a foreign key to Batch table.

My test method is like below where batchSrv is an instance of BatchSrvImpl class.

[i] Batch batch = new Batch();
        batch.setStatus('P');
        batch.addFare(new Fare("CX", "001", "F001", "LON", "HKG"));
        batch.addFare(new Fare("CX", "002", "F002", "LAX", "LON"));

        batchSrv.saveBatch(batch);
[/i]
Test result is something like this:

[i]Internal Exception: java.sql.SQLException: ORA-02291: integrity constraint (FARE_FK) violated - parent key not found
Error Code: 2291
Call:INSERT INTO FARE (FAREID, FARENO, ORIG, TARIFF, DEST, CARRIER, BATCHID) VALUES (?, ?, ?, ?, ?, ?, ?)
        bind => [76, F001, LON, 001, HKG, CX, 113]
Query:InsertObjectQuery(com.test.jpa.srv.Fare[fareid=76])
        at com.test.jpa.srv.BatchSrvImpl.persist(BatchSrvImpl.java:44)
        at com.test.jpa.srv.BatchSrvImpl.saveBatch(BatchSrvImpl.java:29)


[/i]

Does anybody know what the problem is? Below you can find the source code.

==============================================================
[i]@Entity
@Table(name = "BATCH")
public class Batch implements Serializable {

[b]
    @Id
    @SequenceGenerator(name="BATCH_SEQ_GENERATOR", sequenceName="BATCH_SEQ", initialValue=1, allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="BATCH_SEQ_GENERATOR")
    @Column(name = "BATCHID", nullable = false)
    private BigDecimal batchid;
[/b]

    @Column(name = "STATUS", nullable = false)
    private char status;

[b]
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "batch")
    private Set<Fare> fares;
[/b]
    
    public Batch() {
        this.fares = new HashSet<Fare>();
    }

    ........

    public void addFare(Fare fare) {
        fare.setBatch(this);
        this.fares.add(fare);
    }
    
    public void removeFare(Fare fare) {
        this.fares.remove(fare);
    }
}

==============================================================
@Entity
@Table(name = "FARE")
public class [b]Fare [/b]implements Serializable {

[b]
    @Id
    @SequenceGenerator(name="FARE_SEQ_GENERATOR", sequenceName="FARE_SEQ", initialValue=1, allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="FARE_SEQ_GENERATOR")
    @Column(name = "FAREID", nullable = false)
    private BigDecimal fareid;
[/b]

    @Column(name = "CARRIER", nullable = false)
    private String carrier;

    @Column(name = "TARIFF", nullable = false)
    private String tariff;

    @Column(name = "FARENO", nullable = false)
    private String fareno;

    @Column(name = "ORIG", nullable = false)
    private String orig;

    @Column(name = "DEST", nullable = false)
    private String dest;

[b]
    @JoinColumn(name = "BATCHID")
    @ManyToOne
    private Batch batch;
[/b]
    
    public Fare() {
    }

    public Fare(String carrier, String tariff, String fareno, String orig, String dest) {
        this.carrier = carrier;
        this.tariff = tariff;
        this.fareno = fareno;
        this.orig = orig;
        this.dest = dest;
    }

   ............
   
    public Batch getBatch() {
        return this.batch;
    }

    public void setBatch(Batch batch) {
        this.batch = batch;
    }
}

==============================================================

public class BatchSrvImpl implements BatchSrv {

    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaPU");
    
    public BatchSrvImpl() {
    }

    public void saveBatch(Batch batch) {
        persist(batch);
    }

    private void persist(Object object) {
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            em.persist(object);
            em.flush();
            em.refresh(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
}[/i]
[Message sent by forum member 'mohammadwrk' (mohammadwrk)]

http://forums.java.net/jive/thread.jspa?messageID=227354