我正在尝试使用Play2中的EBean建立多对多的关系,我遇到了一个问题,即EBean发出一个错误,说我的类没有注册。 继承我的映射类:
@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从包models.ids转移到模型,但同样的错误发生了)
答案 0 :(得分:0)
由于您的ComissionId类中包含@ManyToOne注释,因此出现此错误。要使此代码工作,您必须将这些关系移动到StoreComission类。在ComissionId类中,您应该只保留标识符。在StoreComission类中,@ ManyToOne关系映射到与复合键中的字段相同的列。但是它们将“可插入”和“可更新”属性设置为false以防止列重复 以下是针对上述情况的更正工作代码:
StoreComission类:
@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;
}
}
ComissionId课程:
@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);
}
}