@ManyToMany带注释的字符串列表?有可能吗? [英] @ManyToMany annotated list of strings? Is it ever possible?

查看:122
本文介绍了@ManyToMany带注释的字符串列表?有可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用户,每个用户都有多个角色.角色和用户之间存在多对多关系.但是角色只是一个名字.我已经陷在这个阶段了:

I have users and each user has several roles. Roles and users are in many-to-many relationship. But the role is just a name. I've got stuck in this phase:

@ManyToMany
@JoinTable(name="users2roles")
private Collection<String> roles;

缺少目标表的名称.我在Internet上搜索,但未找到任何内容.我不想创建特殊的实体类来包装一个字符串值.还有其他方法吗?

There is missing the name of target table. I was searching on Internet, but nothing have found. I don't wannt to create special entity class to wrap one string value. Is there some another way, isn't?

有什么方法可以使它工作或让JPA我创建一个新实体?

Is there any way to make it work or makes JPA me create a new entity?

推荐答案

字符串集合不是将一个对象与另一个对象关联,因此不能是@OneToMany或@ManyToMany-应该是@ ElementCollection.这是字符串的集合,仅此而已.是的,它可以存储在联接表中,但可以使用@CollectionTable.

A Collection of Strings isn't relating one object to another so cannot be a @OneToMany or @ManyToMany - ought to be @ElementCollection. It's a collection of Strings, nothing more. Yes, it can be stored in a join table, but using @CollectionTable.

这篇关于@ManyToMany带注释的字符串列表?有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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