java 没有外键但有反向外键的休眠多对一关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30448259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate Many-To-One Relationship without Foreign Key but with reverse foreign key
提问by GingerHead
I have the following DB:
我有以下数据库:
CREATE TABLE car_owner (
car_owner_id int(11) NOT NULL,
car_id_fk int(11) DEFAULT NULL,
PRIMARY KEY (car_owner_id),
KEY car_owner_car_fk_idx (car_id_fk),
CONSTRAINT car_owner_car_fk FOREIGN KEY (car_id_fk) REFERENCES car (car_id) ON DELETE NO ACTION ON UPDATE NO ACTION,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE car (
car_id int(11) NOT NULL AUTO_INCREMENT,
car_type varchar(45) DEFAULT NULL,
car_plates varchar(25) DEFAULT NULL,
PRIMARY KEY (car_id),
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
And In the java model:
而在java模型中:
For CarOwner I have:
对于 CarOwner,我有:
@Entity
@Table(name="car_owner")
@NamedQuery(name="CarOwner.findAll", query="SELECT co FROM CarOwner co")
public class CarOwner implements Serializable {
@Id
@GeneratedValue
@Column(name="car_owner_id")
private Integer carOwnerId;
.....
//bi-directional many-to-one association to Car
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "car_id_fk", referencedColumnName = "car_id")
private List<Car> cars;
And for Car:
对于汽车:
@Entity
@Table(name="car")
@NamedQuery(name="Car.findAll", query="SELECT c FROM Car c")
public class Car implements Serializable {
@Id
@GeneratedValue
@Column(name="car_id")
private Integer carId;
......
//bi-directional many-to-one association to car_owner
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "car_owner_id")
private CarOwner carOwner;
The problem here is that Hibernate can't relate the tables and goes creating new car_id
and car_owner_id
columns in car table automatically.
这里的问题是 Hibernate 无法关联表并自动在 car 表中创建新列car_id
和car_owner_id
列。
Can anybody help in finding the right combination in the model to relate the tables appropriately.
任何人都可以帮助在模型中找到正确的组合以适当地关联表格。
采纳答案by Shahzeb
@JoinColumn
should be in owner of relationship (in a one to many it's the many side that's regarded the owner).
@JoinColumn
应该在关系的所有者中(在一对多中,多方被视为所有者)。
So I will modify this in car
所以我会修改这个 car
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "car_owner_id",insertable=false, updatable=false)
private CarOwner carOwner;
And this in CarOwner
而这在 CarOwner
@OneToMany(fetch = FetchType.EAGER,mappedBy = "carOwner")
private List<Car> cars;
As a side note I would also not be using EAGER
but that has got nothing to do with question.
作为旁注,我也不会使用,EAGER
但这与问题无关。
Both tables knowing about each other is called Bi-directional relationship.This happens when each table has a key to other table. This is what your java code is expecting.Your tables in database however have a uni-directional relationship. Meaning one table knows about the other but not both. Your car_owner
knows about car
because of foriegn key CONSTRAINT car_owner_car_fk FOREIGN KEY
but your car
does not have any idea about car_owner
both are perfectly valid.
两个表相互了解称为双向关系。当每个表都有另一个表的键时会发生这种情况。这是您的 java 代码所期望的。然而,您在数据库中的表具有单向关系。意思是一张桌子知道另一张桌子,但不知道两者。你car_owner
知道car
因为外键,CONSTRAINT car_owner_car_fk FOREIGN KEY
但你car
不知道car_owner
两者都是完全有效的。
Now the problem is that in your Java code you are treating it as a bi-directional relationship.
现在的问题是,在您的 Java 代码中,您将其视为双向关系。
//bi-directional many-to-one association to car_owner
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "car_owner_id")
private CarOwner carOwner;
but car does not have car_owner_id
why are you treating it a Bi-directional relationship.
但汽车没有car_owner_id
你为什么把它当作双向关系。
Now either update database to make them bi or change java code.
现在要么更新数据库使它们成为 bi 要么更改 java 代码。