gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:36:39 26 4
gpt4 key购买 nike

我正在尝试使用 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;
}

当然非常简单,但这是基本思想。因此,我想要实现的目标是获取一个以 Bars 作为值,以名称作为 Foo 键的映射。

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

Foo(foo1):
name: Foo1

Foo(foo2):
name: Foo2

Bar(bar1):
name: Bar1
foo: foo1

Bar(bar2):
name: Bar2
foo: foo1

到目前为止没有问题,这很有效。现在,如果我尝试将 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

当然,我尝试用谷歌搜索它,但找不到适合我的情况的任何解决方案。对此有什么想法吗?有趣的是,之后我可以去更改数据库中的值并将 bar2 分配给 foo1 并且一切正常,所以我不会错的太...

帮助将不胜感激:)

最好的,卡拉佐

最佳答案

我今天试了你的代码,没有问题。您使用哪个 Play 版本?我使用 1.2.3。

也许是插件 association解决了这个问题,因为那是我在后台使用的。如果升级对您有帮助,请告诉我。

遵循标准 Play 模块安装程序:

play install associations

将以下行添加到您的 dependencies.yml 中,用所需的版本替换 1.0:

require:
- play -> associations 1.0

关于java - 在 Play 中使用 Map 作为模型的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6033314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com