gpt4 book ai didi

Java JPA @OneToMany 需要返回@ManyToOne?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:09:25 25 4
gpt4 key购买 nike

Create Table A (
ID varchar(8),
Primary Key(ID)
);

Create Table B (
ID varchar(8),
A_ID varchar(8),
Primary Key(ID),
Foreign Key(A_ID) References A(ID)
);

鉴于我已经使用上面的 SQL 语句创建了两个表,并且我想为它们创建 Entity 类,对于类 B,我有这些成员属性:

@Id
@Column(name = "ID", nullable = false, length = 8)
private String id;
@JoinColumn(name = "A_ID", referencedColumnName = "ID", nullable = false)
@ManyToOne(optional = false)
private A AId;

A 类中,我是否需要返回多对一关系?

@Id
@Column(name = "ID", nullable = false, length = 8)
private String id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "AId")
private List<B> BList; //<-- Is this attribute necessary?

@ManyToOne 设置一个互惠的 @OneToMany 是必要的还是一个好主意?如果我现在做出设计决定,省略 @OneToMany 注释属性,那会不会让我更上一层楼?

最佳答案

Is it a necessary or a good idea to have a reciprocal @OneToMany for the @ManyToOne?

不,这根本不是强制性的,这是一个纯粹的设计决定。整个问题是......你想要这个吗(即单向关联):

uni-directional

或者这个(即双向关联):

bi-directional

如果不需要从A获取B,那么可以跳过bs属性和A端的OneToMany

If I make the design decision to leave out the @OneToMany annotated attribute now, will come back to bite me further down.

没有,如果以后发现需要可以添加。

关于Java JPA @OneToMany 需要返回@ManyToOne?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2801011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com