JPA manytomany瞬态集合 [英] JPA manytomany transient collection

查看:161
本文介绍了JPA manytomany瞬态集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的实体中拥有一个可查询的集合,该集合不会持久存在.换句话说,是一个短暂的ManytoMany关系.我已经尝试过:

I would like to have a queriable collection in my entity that does not persist. In other words, a transient ManytoMany relationship. I have tried:

@Transient
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name="QuestionSetClass_Link", schema = "SVY",
        joinColumns={@JoinColumn(name="QuestionSetID", referencedColumnName="QuestionSetID")},
        inverseJoinColumns={@JoinColumn(name="QuestionSetClassID", referencedColumnName="ID")})
private Collection<QuestionSetClass> questionSetClasses;
public Collection<QuestionSetClass> getQuestionSetClasses(){
    return questionSetClasses;
}
public void setQuestionSetClasses(Collection<QuestionSetClass> questionSetClasses){
    this.questionSetClasses = questionSetClasses;
}

但是EclipseLink不会部署它,并且给我以下错误:映射注释不能应用于指定了@Transient的字段或属性. [field questionSetClasses]违反了此限制.

But EclipseLink will not deploy it and gives me the error of: Mapping annotations cannot be applied to fields or properties that have a @Transient specified. [field questionSetClasses] is in violation of this restriction.

有人可以告诉我最好的解决方法吗?

Can anyone tell me the best way to handle this?

推荐答案

您似乎很困惑.您不能有关系,也没有关系.你想做什么?

You seem to be very confused. You cannot have a relationship, and not have it. What are you trying to do?

@Transient或Java瞬态意味着JPA将完全忽略该字段及其上的任何注释.

@Transient or Java transient means JPA will entirely ignore the field and any annotations on it.

您似乎想查询该字段,但不保留该字段吗?这很奇怪,然后将如何定义关系?请注意,如果您不更改/添加任何内容,则不会保留任何内容.强烈建议您使对象模型与数据库状态保持同步.

You seem to want to query the field, but not have it persisted? This is very odd, how will the relationships be defined then? Note that nothing will be persisted if you don't change/add anything. It is very strongly recommended that you keep your object model in synch with your database state.

如果您有双向的ManyToMany,则一侧必须使用mappingBy,并且实际上将是只读的,并且允许读取,但是在持久化时将被忽略(但仍应保持).在EclipseLink中,您还可以使用DescriptorCustomizer将ManyToMany标记为只读,并将映射设置为只读.

If you have a bi-directional ManyToMany then one side must use a mappedBy and will effectively be read-only, and allow reads, but will be ignored on persist (but should still be maintained). In EclipseLink you can also mark a ManyToMany as read-only using a DescriptorCustomizer and setting the mapping to be read-only.

EclipseLink还支持ManyToManyQueryKeys,即使您在对象模型中不存在关系,该链接也允许您加入/查询关系.

EclipseLink also supports ManyToManyQueryKeys that allow you to join/query for a relationship, even if it does not exist in the object model.

看, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development /Querying/Query_Keys

这篇关于JPA manytomany瞬态集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆