jpa实体复杂的关键错误

时间:2013-01-02 21:21:47

标签: exception jpa entity

我需要你帮助解决我的JPA问题。 请查看以下实体和主要复杂密钥。

    @Embeddable     公共类BookingPK实现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;
}
}

当我运行我的应用程序来创建数据库表时,我得到以下异常:

线程中的异常" main" javax.persistence.PersistenceException:Exception [EclipseLink-0](Eclipse Persistence Services - 2.3.2.v20111125-r10461):org.eclipse.persistence.exceptions.IntegrityException

描述符例外:

异常[EclipseLink-48](Eclipse Persistence Services - 2.3.2.v20111125-r10461):org.eclipse.persistence.exceptions.DescriptorException 异常描述:字段[FLIGHT.CONNID]存在多个可写映射。只有一个可以定义为可写,所有其他必须以只读方式指定。 映射:org.eclipse.persistence.mappings.OneToOneMapping [bookedFlight] 描述符:RelationalDescriptor(testik.Flight - > [DatabaseTable(FLIGHT)])

异常[EclipseLink-48](Eclipse Persistence Services - 2.3.2.v20111125-r10461):org.eclipse.persistence.exceptions.DescriptorException 异常描述:字段[FLIGHT.CARRID]存在多个可写映射。只有一个可以定义为可写,所有其他必须以只读方式指定。 映射:org.eclipse.persistence.mappings.OneToOneMapping [bookedFlight] 描述符:RelationalDescriptor(testik.Flight - > [DatabaseTable(FLIGHT)])

运行时异常:

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)

引起:异常[EclipseLink-0](Eclipse Persistence Services - 2.3.2.v20111125-r10461):org.eclipse.persistence.exceptions.IntegrityException

描述符例外:

请告知错误。我已经尝试了所有的事情但没有成功。

此致 斯拉维克。

1 个答案:

答案 0 :(得分:0)

发生错误是因为您有多个属性映射相同的列。 你的模特似乎很困惑。如果连接和运营商是唯一的,那么为什么预订需要另一个ID?或者一个航班怎么能有一个独特的预订?

我个人不建议使用EmbeddedId,而是使用IdClass,并在你的关系中使用@Id。 您也可以在连接列上使用insertable / updateable = false,但您的模型看起来很奇怪。

请参阅,

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