在Play中使用Map作为模型的属性

kal*_*rzo 7 java hibernate playframework

我正在尝试使用Map作为我的一个模型属性的类型.我们以这两个类为例:

@Entity
public class Foo extends Model {

    @OneToMany(mappedBy = "foo", cascade = CascadeType.ALL)
    @MapKey(name = "name")
    public Map<String, Bar> bars;

    public String name;

}

@Entity
public class Bar extends Model {

    @ManyToOne
    public Foo foo;

    public String name;
}
Run Code Online (Sandbox Code Playgroud)

当然非常简化,但这是基本的想法.所以我想要实现的是获取一个带有Bars作为值的地图,并将名称作为Foo的键.

现在我想利用Fixture从这个YAML文件加载一些数据:

Foo(foo1):
    name: Foo1

Foo(foo2):
    name: Foo2

Bar(bar1):
    name: Bar1
    foo: foo1

Bar(bar2):
    name: Bar2
    foo: foo1
Run Code Online (Sandbox Code Playgroud)

到目前为止没有任何问题,这就像一个魅力.现在,如果我尝试将bar2更改为foo: foo2,我会得到以下异常:

play.exceptions.JavaExecutionException: Cannot load fixture initial-data.yml: org.hibernate.HibernateException: Found two representations of same collection: models.Foo.bars
    at play.jobs.Job.call(Job.java:166)
    at Invocation.Job(Play!)
Caused by: java.lang.RuntimeException: Cannot load fixture initial-data.yml: org.hibernate.HibernateException: Found two representations of same collection: models.Foo.bars
    at play.test.Fixtures.loadModels(Fixtures.java:221)
    at jobs.Bootstrap.doJob(Bootstrap.java:18)
    at play.jobs.Job.doJobWithResult(Job.java:55)
    at play.jobs.Job.call(Job.java:157)
    ... 1 more
Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: Found two representations of same collection: models.Foo.bars
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1153)
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:798)
    at play.db.jpa.JPABase._save(JPABase.java:47)
    at play.test.Fixtures.loadModels(Fixtures.java:205)
    ... 4 more
Caused by: org.hibernate.HibernateException: Found two representations of same collection: models.Foo.bars
    at org.hibernate.engine.Collections.processReachableCollection(Collections.java:175)
    at org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.java:60)
    at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:122)
    at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:83)
    at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:77)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:165)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:240)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795)
    ... 6 more    

at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795)
... 6 more
Run Code Online (Sandbox Code Playgroud)

当然我试着谷歌但是找不到我的案子的任何解决方案.有什么想法吗?有趣的是,之后我可以去更改数据库中的值,并将bar2分配给foo1,这一切都很好,所以我不能错...

帮助将不胜感激:)

最好的,卡拉佐

Rob*_*e W -1

去除那个:

@MapKey(name = "name")
Run Code Online (Sandbox Code Playgroud)