JPA manytomany transient collection

半世苍凉 提交于 2020-01-04 16:25:28

问题


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;
}

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?


回答1:


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

@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.

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 also supports ManyToManyQueryKeys that allow you to join/query for a relationship, even if it does not exist in the object model.

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




回答2:


Transient annotation means that there is no database column (or joined table) belonging to the annotated field of the Java object. So adding both @Transient and @ManyToMany annotations to a field is contradictory.




回答3:


I found the answer. It is to declare the private element not as private but as transient. Then I can retain the ManyToMany with the fetch type declared.

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


来源:https://stackoverflow.com/questions/14618486/jpa-manytomany-transient-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!