Java JPA和EBean中的多对多
我试图在Play2中使用Ebian建立多对多关系,我遇到了一个问题,Ebian trows错误地说我的类未注册。 以下是我的映射类:Java JPA和EBean中的多对多,java,jpa,playframework,ebean,Java,Jpa,Playframework,Ebean,我试图在Play2中使用Ebian建立多对多关系,我遇到了一个问题,Ebian trows错误地说我的类未注册。 以下是我的映射类: @Entity public class Booking extends Model { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; //... other fields } @Entity public class Store extends Mo
@Entity
public class Booking extends Model {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
//... other fields
}
@Entity
public class Store extends Model {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
//... other fields
}
@Embeddable
public class ComissionId {
@ManyToOne(targetEntity = Booking.class, optional = false)
@PrimaryKeyJoinColumn(name = "booking_id", referencedColumnName = "id")
private Booking booking;
@ManyToOne(targetEntity = Store.class, optional = false)
@PrimaryKeyJoinColumn(name = "store_id", referencedColumnName = "id")
private Store store;
}
@Entity
@AssociationOverrides({
@AssociationOverride(name = "id.booking", joinColumns = @JoinColumn(name = "booking_id")),
@AssociationOverride(name = "id.store", joinColumns = @JoinColumn(name = "store_id"))
})
public class StoreComission extends Model {
@EmbeddedId
private ComissionId id;
@Column(nullable = false)
private double value;
@Column(nullable = false)
private Date date;
}
错误:
java.lang.RuntimeException:
Error reading annotations for models.ids.ComissionId
Caused by: java.lang.RuntimeException:
Error with association to [class models.Booking] from [models.ids.ComissionId.booking].
Is class models.Booking registered?
在application.conf中,我放置了ebean.default=“models.*”这样所有这些类都应该注册,对吗?(我已尝试将ComissionId从package models.ids移动到models,但出现了相同的错误)您出现此错误是因为ComissionId类中有@manytone注释。要使此代码正常工作,必须将这些关系移动到StoreCommission类。在ComissionId类中,您应该只留下标识符。在StoreCommission类中@ManyTone关系映射到与复合键中的字段相同的列。但它们的属性“insertable”和“updateable”设置为false,以防止列重复。
以下是针对上述场景更正的工作代码: StoreCommission类别:
@Entity
public class StoreComission extends Model {
public StoreComission() {
id = new ComissionId();
}
@EmbeddedId
private ComissionId id;
@Column(nullable = false)
public double value;
@Column(nullable = false)
public Date date;
@ManyToOne
@JoinColumn(name = "booking_id", insertable = false, updatable = false)
private Booking booking;
@ManyToOne
@JoinColumn(name = "store_id", insertable = false, updatable = false)
private Store store;
public void setBooking(Booking aBooking) {
booking = aBooking;
id.booking_id = aBooking.id;
}
public Booking getBooking() {
return booking;
}
public void setStore(Store aStore) {
store = aStore;
id.store_id = aStore.id;
}
public Store getStore() {
return store;
}
}
CommissionID类:
@Embeddable
public class ComissionId {
public int booking_id;
public int store_id;
@Override
public int hashCode() {
return booking_id + store_id;
}
@Override
public boolean equals(final Object obj) {
return super.equals(obj);
}
}