[英]@ManyToOne field not working in Playframework 2
I'm struggling with some persistance issues in the playframework. 我在游戏框架中遇到一些持久性问题。 I don't think what I'm trying to do should be to hard so I can't understand why what I've done just isn't working.
我不认为我要努力做的事情很难,所以我不明白为什么我所做的只是行不通。 I've created an entity which references another entity through a ManyToOne mapping (cv in example below).
我创建了一个实体,该实体通过ManyToOne映射(在下面的示例中为cv)引用另一个实体。 The main entity is being persisted and the reference entity also appears to be, being persisted.
主要实体被持久化,而参考实体也似乎被持久化。 However whenever I load the main entity through a Entity.
但是,每当我通过实体加载主实体时。
findAll()
method the main entity loads but all the values on the referenced entity are null. 主实体加载的
findAll()
方法,但引用实体上的所有值均为null。
What's even weirder is the main entity appears to have the correct id for the referenced entity it's just not loading. 甚至更奇怪的是,主要实体似乎对于刚刚加载的引用实体具有正确的ID。 I know this as I'm doing a find.all on both the main entity (
CrossTrade
) and the referenced entity ( CurVal
) when I start up my application and it's finding all the referenced entities with values and the main entity with values. 我知道这一点,因为我在启动应用程序时同时在主要实体(
CrossTrade
)和被引用实体( CurVal
)上都进行了find.all操作,它正在查找具有值的所有引用实体和具有值的主要实体。 It just for some reason doesn't seem to be picking up the referenced entity values from the CrossTrade entity when I load it. 出于某种原因,当我加载它时,似乎并没有从CrossTrade实体中获取引用的实体值。 Any help would be much appreciated.
任何帮助将非常感激。
@Entity
@Table(name="CrossTrade")
public class CrossTrade extends Model {
@Id
@GeneratedValue
public Long id;
@Transient
public Double quantity = 1.00;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
public CurVal cv;
}
@Entity
@Table(name="CurVal")
public class CurVal extends Model{
@Id
@GeneratedValue
public Long id;
public Double high;
public Double low;
public Double fee;
}
You're not telling Ebean where the ManyToOne
reference is mapped to. 您不是在告诉Ebean
ManyToOne
引用映射到的位置。
Try updating your CurVal
class to: 尝试将您的
CurVal
类更新为:
@Entity
@Table(name="CurVal")
public class CurVal extends Model{
@Id
@GeneratedValue
public Long id;
@OneToMany(mappedBy="cv")
public Set<CrossTrade> trades;
public Double high;
public Double low;
public Double fee;
}
Another thing is that having CascadeType.ALL
on your ManyToOne
is rather broken, since if you delete one CrossTrade
, it will also delete the CurVal
attached to it, but leaves all other CrossTrades
attached to that CurVal
as orphans. 另一件事是,
ManyToOne
上具有CascadeType.ALL
情况相当ManyToOne
,因为如果删除一个CrossTrade
,它也会同时删除附加的CurVal
,但将所有其他CrossTrades
CurVal
为孤立的CurVal
。 By all means it should be the other way around, ie, putting the CascadeType.ALL
on the OneToMany
field. 绝对应该相反,即将
CascadeType.ALL
放在OneToMany
字段上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.