使用ManyToMany的TomEE + OpenJPA不起作用 [英] TomEE + OpenJPA using ManyToMany does not work
问题描述
我正在撒谎。我不明白,它有多难?
I am throwing in the towel. I do not understand, how difficult can it be?
我有两个实体用户
和群组
,有多对多的关系。 组
管理关系。所以在组
我有:
I have two Entities User
and Group
, having a many to many relationship. Group
manages the relationship. So in Group
I have:
@Entity
@Table(name = "GROUPS", catalog = "", schema = "GROUPADMIN")
public class Group {
...
@ManyToMany
@JoinTable(
name = "GROUP_USERS",
joinColumns = {@JoinColumn(name = "GROUP_ID")},
inverseJoinColumns = {@JoinColumn(name = "USER_ID")}
)
private Set<User> users;
然后对于用户
我创建实体的东西如下:
Then for User
I create the entity something as follows:
@Entity
@Table(name = "USERS", catalog = "", schema = "GROUPADMIN")
public class User {
...
@ManyToMany(mappedBy="users")
private Set<Group> groups;
然后在我的支持bean中,实际上是 @Named(注册表)
,我将对检索到的用户
的引用存储为具有相同名称的属性。
Then in my backing bean, actually a @Named("registry")
, I store a reference to a retrieved user
as a property with the same name.
然后我在我的JSF中使用该支持bean
Then I use that backing bean royally in my JSF
Hello <h:outputLabel value="#{registry.user.firstName}"/>
<h:panelGroup>
<h:dataTable value="#{registry.user.groups}" var="g">
<f:facet name="header">Properties List</f:facet>
<h:column>
<f:facet name="header">Group</f:facet>
<h:outputText value="#{g.id}"/>
</h:column>
</h:dataTable>
</h:panelGroup>
对于那些对表感兴趣的人:
For those interested in the tables:
create table "GROUPADMIN".GROUPS
(
ID VARCHAR(15) not null primary key
);
create table "GROUPADMIN".USERS
(
ID VARCHAR(50) not null primary key,
PASSWORD VARCHAR(50),
FIRST_NAME VARCHAR(50),
LAST_NAME VARCHAR(50)
);
create table "GROUPADMIN".GROUP_USERS
(
GROUP_ID VARCHAR(15) not null,
USER_ID VARCHAR(50) not null,
primary key (GROUP_ID, USER_ID)
);
首先注意到的是,组似乎是空的,不是空的,而是空的(通过调试等验证) )。所以我明白默认情况下 @ManyToMany
会使用Lazy绑定,所以我将其更改为 fetch = FetchType.EAGER
(并不是说这应该重要)。在这样做之后,事情变得奇怪......
First thing noticed is that groups appears to be empty, not null, but empty (verified with debugging etc). So I understood that by default a @ManyToMany
would use Lazy binding, so I changed this to fetch = FetchType.EAGER
(not that this should even matter). After doing so, things really got bizarre ...
此时EL开始抱怨上的id属性#{ g.id}
:
Caused by:
javax.el.PropertyNotFoundException - Property 'id' not found on type org.apache.openjpa.util.java$util$HashSet$proxy
at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:266)
为什么要检索属性 id
在 HashSet
代理上,而不是组
?
Why is it trying to retrieve property id
on a HashSet
proxy, and not on a Group
?
所以我很好奇我实际上从< h:dataTable>
g 属性>,我试图输出它只需使用< h:outputText value =#{g}/>
...结果非常有趣...
So me being curious what I actually got into that g
attribute I got from the <h:dataTable>
, I tried to output it simply using <h:outputText value="#{g}"/>
... result was really interesting ...
打印出的 g
不仅仅是 Set< Group>中的一个元素
,但实际上是coll ection本身,它与之前显示的异常相匹配......这更能说明API或集成中的错误?
What g
printed out was not just one element out of the Set<Group>
, but was actually the collection itself, which matches the earlier shown exception ... that's more indicative for a bug in the API or integration?
所以基本上它看起来有些东西被打破了,虽然我怀疑这一切都与同一个问题有关。
So basically it looks like several things are broken, although I suspect it all relates to one an the same issue.
请注意,我使用的是基本的现成TomEE,最新版本,没有自定义(JPA没有增强器)另外)。
Note that I used a basic off-the-shelf TomEE, latest version, no customization (no enhancer for JPA also).
推荐答案
为什么它应该有效: JSR 338:JavaTM持久性2.1。
错误提交: Jira Issue OpenJPA-2546 (请对此问题进行投票决定。)
Bug submitted: Jira Issue OpenJPA-2546 (please vote on getting this resolved).
解决方法:使用列表< T>
替换设置< T>
作为解决方法。
Workaround: replace Set<T>
with List<T>
as a workaround.
这篇关于使用ManyToMany的TomEE + OpenJPA不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!