gpt4 book ai didi

java - Hibernate 1 :M relationship , 行顺序、常量值表和并发

转载 作者:行者123 更新时间:2023-12-02 00:52:06 24 4
gpt4 key购买 nike

表A和B需要有1:M关系

a 和 b 在应用程序运行时添加,因此创建了 A,然后说4 B 已创建。每个 B 实例都必须按顺序出现,以便我稍后可以按照我添加它们的顺序提取它们。

该应用程序将是一个在 Tomcat 上运行的 Web 应用程序,因此 10 个实例可以同时工作。

所以我的问题是:

1) 如何保留插入顺序,以便我可以提取 A 中引用的 B 实例与我坚持的顺序相同。这很棘手,因为我们添加到集合中,然后它被保存(我说得对吗?)。所以,这取决于Hibernate如何保存它,如果它改变了我们添加实例的顺序怎么办?在描述关系时,我看到了类似 LIST 而不是 SET 的东西,这是我需要的吗?

2) 如何向 B 添加第三列,以便我可以区分实例,例如B 表中的 SEX(M,F,U)。我是否需要一个特殊的表,或者有一种简单的方法来描述 Hibernate 中的常量。你有什么推荐?

3)谈到并发,您建议使用什么方法?不应该有碰撞在数据库中,如您所见,如果未插入行(添加 PK),则可能很容易出现一些行哪里可以无延迟地调用它?

最佳答案

How to preserve inserting order, so that I could extract B instances that A references in the same order as I persisted them. (...)

使用有序集合,例如List。请注意,列表可以通过两种不同的方式进行映射:

  • 作为有序列表,顺序不会在数据库中具体化
  • 作为索引列表,顺序在数据库中具体化

请参阅2.2.5.3.4. Indexed collections (List, Map)部分了解详情。

How to add a 3-rd column to B so that I could differentiate the instances, something like SEX(M,F,U) in B table. Do I need a special table, or there's and easy way to describe constants in Hibernate. What do you recommend?

在 B 上使用 enum 并使用 @Enumerated 进行映射。看这个previous answer .

Talking about concurrency, what methods do you recommend to use? There should be no collisions in the db and as you see, there might easily be some if rows are not inserted (PK added) right where it is invoked without delays ?

嗯...使用适合您需求的事务和锁定策略(但说实话,我不确定我是否理解这个问题)。

关于java - Hibernate 1 :M relationship , 行顺序、常量值表和并发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2584516/

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