jpa OneToMany&多对一 [英] jpa OneToMany & ManyToOne
问题描述
我的模型中有一个oneToMany和ManyToOne映射:
i have a oneToMany and ManyToOne mapping in my models:
班级用户
@OneToMany (cascade = { CascadeType.REFRESH, CascadeType.DETACH }, fetch = FetchType.EAGER)
private Set<Judgement> judgements;
班级判决
@ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name = "user_id")
private User judge;
在数据库中,我必须将表作为用户和判断",当我尝试运行代码时,它显示错误为:
and in DB, i have to tables as Users and Judgements, when i tried to run my code, it showed error as:
Caused by: org.postgresql.util.PSQLException: ERROR: relation "users_judgements" does not exist
这是否意味着我必须手动创建表users_judgements,jpa无法自动为我创建关系? RoR可以做到...谢谢.
does that mean i have to create the table users_judgements by hand, jpa cannot automatically create the relationship for me? RoR can do it...Thanks.
推荐答案
如果您在JUDGMENT表中有来自USER表user_id的外键,则不需要其他表.那就是@JoinColumn(name ="user_id").
If you have a foreign key from USER table, user_id, in JUDGMENT table then there is no need to have another table. That will be the @JoinColumn(name = "user_id").
查看您是否缺少某些东西;我看不到任何" mappedBy "属性,在您的情况下该属性将为用户".
See if you are missing something; I can not see any "mappedBy" attribute, which will be "user" in your case.
请看一下这篇文章;该链接将带您进入第4页,并提供有关一对多关系的详细信息.值得阅读整篇文章.
Please take a look at this article; page 4 that the link will take you, give details about one-to-many relationship. It will be worth reading the whole article.
http://www. javaworld.com/javaworld/jw-01-2008/jw-01-jpa2.html?page=4
此外,同一教程的第1部分非常适合基础知识;
Also, the part 1 of the same tutorial is good for basics;
http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html
这篇关于jpa OneToMany&多对一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!