我有一个实体 Course 和一个实体 User。类(class)和用户之间存在多对多关系,因为一个类(class)可以有很多用户,一个用户可以注册很多类(class)。在这两个实体中,我都在特定字段上放置了 @ManyToMany 注释,也就是说,在 Course 中,我有:
@ManyToMany
private List<RegisteredUser> members;
在 User 中我有:
@ManyToMany
private List<Course> coursesTaken;
现在,我知道这种多对多关系通常由第三个表表示。我还知道有注释 @JoinTable 允许我们这样做。我不知道是否应该在两个不同实体的两个字段上添加此注释 @JoinTable。顺便说一句,如果我需要同时添加两者,名称需要匹配吗?
最佳答案
这实际上是一个很好的问题,它有助于理解“拥有”实体的概念,因为双方都不需要 @JoinTable 注释。如果你想防止双方都有 join tables,这是一个好主意,那么你需要在一侧有一个 mappedBy= 元素。 @JoinTable 注释用于指定表名或映射关联的列。
Specifies the mapping of associations. It is applied to the owning side of an association.
是否有join table 由@ManyToMany 注释的mappedBy="name" 元素控制。 Javadoc for mappedBy for the ManyToMany annotation says :
The field that owns the relationship. Required unless the relationship is unidirectional.
对于您在 Hibernate (5.0.9.Final) 中的(双向)示例,如果只有两个 @ManyToMany 注释并且没有 mappedBy= 元素,默认将有两个 Entity 表和两个 Join Tables:
Hibernate: create table Course (id bigint not null, primary key (id))
Hibernate: create table Course_Member (Course_id bigint not null, members_id bigint not null, primary key (Course_id, members_id))
Hibernate: create table Member (id bigint not null, primary key (id))
Hibernate: create table Member_Course (Member_id bigint not null, courses_id bigint not null, primary key (Member_id, courses_id))
虽然这是说每个实体“拥有”它的ManyToMany 关系,但额外的join table 在典型用例中是多余的。但是,如果我决定让 Member 实体“拥有”关系,则我将 mappedBy= 元素添加到 Course 实体以指定它不拥有关系:
@ManyToMany(mappedBy="courses")
Set<Member> members;
将 @JoinTable(name="Member_Course") 添加到 Member 实体不会改变任何东西:它只是将表命名为与它本来命名的相同反正。
由于 Course 实体不再拥有它的 ManyToMany 关系,因此不会创建额外的 JoinTable:
Hibernate: create table Course (id bigint not null, primary key (id))
Hibernate: create table Member (id bigint not null, primary key (id))
Hibernate: create table Member_Course (members_id bigint not null, courses_id bigint not null, primary key (members_id, courses_id))
这对开发人员很重要,因为他或她必须明白,除非将关系添加到拥有实体(在本例中为 Member 实体),否则任何关系都不会持久。但是,由于这是双向关系,开发人员应该向 Member.courses 添加 both Course 和 Member 到 Course.members 无论如何。
因此,如果您有一个双向 ManyToMany 关系,这意味着您在涉及的两个实体上都有ManyToMany,那么您应该添加一个mappedBy="name" 在其中之一上,以避免出现冗余的 join table。因为它是双向的,所以我认为将 owning 实体放在哪一边并不重要。与往常一样,启用 sql 日志并查看数据库中发生的情况始终是个好主意:
引用资料:
What is the difference between Unidirectional and Bidirectional associations? .
What does relationship owner means in bidirectional relationship? .
What is the “owning side” in an ORM mapping? .
Most efficient way to prevent an infinite recursion in toString()? .
关于java - @JoinTable 是否应该在@ManyToMany 关系的两边指定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36803306/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/