0
votes

I need your help with my JPA problem. Please look at the following entities and primary complex keys.

@Embeddable public class BookingPK implements Serializable { private static final long serialVersionUID = 1L;

private String carrid;
private String connid;
private String bookid;


public String getcarrid()
{
    return this.carrid;
}

public void setcarrid(String carrid)
{
    this.carrid = carrid;
}

public String getconnid()
{
    return this.connid;
}

public void setconnid(String connid)
{
    this.connid = connid;
}

public String getbookid()
{
    return this.bookid;
}

public void setbookid(String bookid)
{
    this.bookid = bookid;
}

 public int hashCode() 
 {
    return (int) ( this.carrid.hashCode())
            +(int) ( this.connid.hashCode())
            +(int) ( this.bookid.hashCode());

 }

 public boolean equals(Object obj) 
 {
    if (obj == this) return true;
    if (!(obj instanceof Booking)) return false;
    BookingPK pk = (BookingPK) obj;
    return  pk.carrid.equals(this.carrid)
            && pk.connid.equals(this.connid)
            && pk.bookid.equals(this.bookid);
 }



@Entity
public class Booking 
{
@EmbeddedId
private BookingPK bookingPrimaryKey;
private String CANCELLED;


public BookingPK getbookingPrimaryKey()
{
    return this.bookingPrimaryKey;
}

public void setbookingPrimaryKey(BookingPK key)
{
    this.bookingPrimaryKey = key;
}

public String getCANCELLED()
{
    return this.CANCELLED;
}

public void setCANCELLED(String CANCELLED)
{
    this.CANCELLED = CANCELLED;
}
}



@Embeddable
public class FlightPK implements Serializable 
{
private static final long serialVersionUID = 1L;

private String carrid;
private String connid;


public String getcarrid()
{
    return this.carrid;
}

public void setcarrid(String carrid)
{
    this.carrid = carrid;
}

public String getconnid()
{
    return this.connid;
}

public void setconnid(String connid)
{
    this.connid = connid;
}

  public int hashCode() 
  {
    return (int) ( this.carrid.hashCode())
            +(int) ( this.connid.hashCode());
  }

  public boolean equals(Object obj) 
  {
    if (obj == this) return true;
    if (!(obj instanceof Flight)) return false;
    FlightPK pk = (FlightPK) obj;
    return  pk.carrid.equals(this.carrid)
            && pk.connid.equals(this.connid);
 }
}



@Entity
public class Flight 
{
@EmbeddedId
private FlightPK flightPrimaryKey; 

private Booking bookedFlight;


public Booking getbookedFlight()
{
    return this.bookedFlight;
}

public void setbookedFlight(Booking flight)
{
    this.bookedFlight = flight;
}

public FlightPK getflightPrimaryKey()
{
    return this.flightPrimaryKey;
}

public void setPRICE(FlightPK key)
{
    this.flightPrimaryKey = key;
}
}

Whei i run my application to create DB tables i get the following exception:

Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.IntegrityException

Descriptor Exceptions:

Exception [EclipseLink-48] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DescriptorException Exception Description: Multiple writable mappings exist for the field [FLIGHT.CONNID]. Only one may be defined as writable, all others must be specified read-only. Mapping: org.eclipse.persistence.mappings.OneToOneMapping[bookedFlight] Descriptor: RelationalDescriptor(testik.Flight --> [DatabaseTable(FLIGHT)])

Exception [EclipseLink-48] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DescriptorException Exception Description: Multiple writable mappings exist for the field [FLIGHT.CARRID]. Only one may be defined as writable, all others must be specified read-only. Mapping: org.eclipse.persistence.mappings.OneToOneMapping[bookedFlight] Descriptor: RelationalDescriptor(testik.Flight --> [DatabaseTable(FLIGHT)])

Runtime Exceptions:

at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:517)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getMetamodel(EntityManagerFactoryDelegate.java:591)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getMetamodel(EntityManagerFactoryImpl.java:506)
at org.odata4j.producer.jpa.JPAEdmGenerator.generateEdm(JPAEdmGenerator.java:95)
at org.odata4j.producer.jpa.JPAProducer.<init>(JPAProducer.java:91)
at com.mockservice.MockService.<init>(MockService.java:34)
at com.mockservice.MockService.main(MockService.java:51)

Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.IntegrityException

Descriptor Exceptions:

Please advise what is wrong. I have already tried every thing but without success.

Regards, Slavik.

1

1 Answers

0
votes

The error occurs because you have multiple attributes mapping the same columns. You model seems very confused. If connection and carrier are unique, then why does booking need another id? Or how can a flight have a single unique booking?

Personally I would not recommend using EmbeddedId, but an IdClass instead, and use @Id on your relationship. You may also be able to use insertable/updateable=false on your join columns, but your model seems odd.

See,

http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#JPA_2.0