我是数据库设计的新手.
我有个疑问.这个问题非常基础.但请帮帮我.我将尝试通过一个例子来证明它.
假设,我在一张桌子上拿到了书,在另一张桌子里拿到了他们的作者(假设一本书只由一位作者(一对多)写成,一位作者可以写多本书(多对一)).我没有得到如何准确链接表和什么应该自动递增?
tblBooks //Table 1 contains two entities
{
bookId // This field is auto-incremented
bookName
}
tblAuthors //Table 2
{
authorId // Should this field also be auto-incremented?
authorName
Field3 // What should be the 'Field3' then which acts as link between both the tables?
// What exactly is foreign key? Here 'Field3' would be foreign key or what?
}
Run Code Online (Sandbox Code Playgroud)
帮助赞赏
我想使用 cassandra,但我没有使用关系 @onetomany 或 @ManyToMant。我使用 java 和 cassandra-core 驱动程序。我如何使用这些关系
我有关于将 JPA 与 OneToMany 关系(双向)以及 CascadeType.ALL 一起使用的问题。基于vlad post(https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/),使用CascadeType.ALL坚持OneToMany关系,也应该坚持
Post post = new Post();
post.setName("Hibernate Master Class");
Comment comment1 = new Comment();
comment1.setReview("Good post!");
Comment comment2 = new Comment();
comment2.setReview("Nice post!");
post.addComment(comment1);
post.addComment(comment2);
session.persist(post);
Run Code Online (Sandbox Code Playgroud)
但是,就我而言:
//Entity SharedAdvertisementKey
@ManyToOne
@JoinColumn(name="SHARED_AD_ID", nullable=false)
private SharedAdvertisement sharedAdvertisement;
//Entity SharedAdvertisements
@OneToMany(mappedBy="sharedAdvertisement", cascade=CascadeType.ALL)
private List<SharedAdvertisementKey> sharedAdvertisementKey = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
仅当我在保留关系所有者之前链接实体的双方时,它才有效:
sharedAdvertisementKey.setSharedAdvertisement(sharedAdvertisement);
sharedAdvertisement.addSharedAdvertisementKey(sharedAdvertisementKey);
Run Code Online (Sandbox Code Playgroud)
所以主要问题是:即使关系方的所有者有 CascadeType.All,我是否应该始终照顾双方?
我想在nodejs中开发一个发布者->订阅者模型,其中有1个发布者和许多订阅者。
目前我的想法是使用普通的 websocket。这样做的问题是,如果通过互联网运行,每个订阅者都需要静态 IP和端口转发。这不符合要求。
这个问题的解决方案似乎是 MQTT,因为它应该适合该用例,但我看到它也运行在 websockets 上,这应该会导致相同的问题,还是 MQTT 处理它的方式不同?
本质上,我需要一个解决方案,其中发布者拥有静态 IP,而订阅者可以位于世界任何地方。这可以通过 MQTT 实现吗?还是我需要其他解决方案?
对于以下域:
\nclass Parent {\n @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")\n Set <Child> children;\n)\n\nclass Child {\n @ManyToOne\n Parent parent;\n ... // setters, getters, other properties \n}\n
Run Code Online (Sandbox Code Playgroud)\n我正在寻找一种添加子实体的方法,而无需在父对象中延迟初始化子实体。
\n前任:
\nchild.set(parent) // is fine, since Parent is pretty lightweight.\n\nparent.getChildren().add(child) // No good, since in this case it will attempt to \n//load all the children of a Parent object.\n
Run Code Online (Sandbox Code Playgroud)\n乍一看,这感觉应该是很容易实现的事情,因为为了添加子项,您所需要的只是父项 ID 和对子表的 SQL 插入语句。\n但是,当涉及到休眠时,没有什么是简单的(事实上,对于所有使用缓存的东西来说,\xe2\x80\x99 都是如此):-)
\n我们正在尝试扩展一个遗留应用程序,该应用程序最初是为支持数十个孩子而编写的,但随着时间的推移,现在已经发展到可以与数千个孩子一起运行,并且正如您可以想象的那样,\xe2\x80\x99t 无法很好地扩展,因为每次我们需要要添加子项,我们需要首先将整个组加载到内存中,这会导致 O(N) 时间复杂度和 O(N) 空间复杂度(其中 N 是子项总数)。
\n有人遇到过类似的问题吗?有没有办法在 hibernate 中解决这个问题,或者 hibernate …