gpt4 book ai didi

java - 在实体中保存 id 而不是相关实体的方法/技术的名称(或关键字)是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:21 25 4
gpt4 key购买 nike

实体持有映射到持久存储数据的数据。通常这是 DB。

你可以持有 ids:

class Book {
private String title;
public String getTitleId() { return title; }

private Long publisherId; // <== **it is**!
public Long getPublisherId() { return publisherId; }

private List<Long> authorIds;
public List<Long> getAuthorIds() { return authorIds; }
}

或解析的实体:

class Book {
private String title;
public String getTitleId() { return title; }

private Publisher publisher; // <== **it is**!
public Publisher getPublisher() { return publisher; }

private List<Author> authors;
public List<Author> getAuthors() { return authors; }
}

如果您使用像 Hibernate 这样的 ORM,那么处理已解析的实体就像 ORM 一样简单 *join*s 与相关实体是免费的。

但是,如果您关心性能或想使用数据库功能通过手动 SQL 查询将逻辑从 Java 移动到 SQL,那么具有 ID 的实体将胜出(它们速度更快,并且手动加入每个相关实体很烦人)!

因此,我决定维护两种类型的实体:具有 ID 和已解析的相关实体,以在数据类型(类型名称)中保存此知识。

所以我开始谷歌搜索以获取类名的最佳后缀,但找不到合适的关键字。请告诉我如何命名方法/技术以在实体中保存 ids vs 相关实体!

最佳答案

我认为您要查找的术语是

急切与延迟加载获取策略

编辑:

延迟加载的实体可以称为代理,一个尚未初始化的集合作为包装器

针对评论的回应:参见 Hibernate one-to-one: getId() without fetching entire object . Hibernate 可以为延迟加载的实体生成 ID 值。

来自Hibernate FAQ :

How can I retrieve the identifier of an associated object, without fetching the association?

Just do it. The following code does not result in any SELECT statement, even if the item association is lazy.

Long itemId = bid.getItem().getId();

This works if getItem() returns a proxy and if you mapped the identifier property with regular accessor methods. If you enabled direct field access for the id of an Item, the Item proxy will be initialized if you call getId(). This method is then treated like any other business method of the proxy, initialization is required if it is called.

关于java - 在实体中保存 id 而不是相关实体的方法/技术的名称(或关键字)是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16671362/

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