- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的项目中有一个非常重要的表 - 强制表。它有几列,但在本例中最重要的是主键。
Mandatory
1. id (Long Primary Key)
...
现在我必须添加两个新表 - 附加表和额外表。它们都只有两列。在附加中,两者都是主键。如您所见,其中之一也是外键。
Additional
1. mandatory_id (Long Primary Key FK -> Mandatory.id)
2. description (Varchar(512) Primary Key)
在 Extra 表中主键也是外键。
Extra
1. mandatory_id (Long Primary Key FK -> Mandatory.id)
2. text (Varchar(512)
我正在考虑如何使用 Hibernate 在我的项目中实现它。
我以这种方式声明了它们(这里没有 getter、setter 等),MandatoryEntity
类:
@Entity
@Table(name = "mandatory")
public class MandatoryEntity {
@OneToMany
@JoinColumn(name = "mandatory_id")
protected List<AdditionalEntity> additonals;
@OneToOne
@JoinColumn(name = "mandatory_id")
protected ExtraEntity extra;
}
AdditionalEntity
类:
@Entity
@Table(name = "additional")
public class AdditionalEntity {
@EmbeddedId
private AdditionalPK id;
@ManyToOne(fetch = FetchType.LAZY)
@Fetch(value = FetchMode.JOIN)
@JoinColumn(name = "mandatory_id", nullable = false, insertable = false, updatable = false)
protected MandatoryEntity mandatory;
}
AdditionalPK
类:
@Embeddable
public class AdditionalPK {
@Column(name = "mandatory_id")
private Long mandatoryId;
@Column(name = "description")
private String description;
}
ExtraEntity
类:
@Entity
@Table(name = "extra")
public class ExtraEntity {
@Id
@Column(name = "mandatory_id")
private Long id;
@OneToOne
@PrimaryKeyJoinColumn
protected MandatoryEntity mandatory
@Column(name = "text")
protected String text;
}
此时我正在使用的笔记本电脑坏了,所以我有时间考虑。我在 JPA 规范 @SecondaryTables
注释中找到了。我可以在我的案例中使用这个注释吗?这些只是两个表,每个表只有两列。第一个有复合主键,第二个没有。
也许还有另一种方法可以在一个 MandatoryEntity
中实现这些表?没有两个新实体:AdditionalEntity
和 ExtraEntity
?
最佳答案
首先,让我重新表述一下我从您的要求中了解到的内容。
您还有另外两个实体向强制实体指定附加信息。其中
a.可以指定多个附加信息(ManyToOne)
b.可以指定一个额外信息(OneToOne)
通常这是一个非常基本的用例。您必须在这里决定的一件事是您是否需要双向映射或单向映射(仅通过强制实体访问)就足够了。我希望单向映射对于您的用例来说是一个不错的选择,因为看起来 Mandatory 是您的根实体。在您的代码中,您尝试对双向关系进行建模,但忘记指定逆关系。这实际上会产生两个独立的关系。您应该像这样指定双向关系的目标:
@Entity
@Table(name = "mandatory")
public class MandatoryEntity {
// you need a mapped by to mark the relation as bidirectional
@OneToMany(mappedBy = "mandatory")
protected List additonals;
@OneToOne
protected ExtraEntity extra;
}<p></p>
<p>@Entity
@Table(name = "additional")
public class AdditionalEntity {
@EmbeddedId
private AdditionalPK id;
@ManyToOne(fetch = FetchType.LAZY)
protected MandatoryEntity mandatory;
}</p>
<p></p>
您还指定了示例中不需要的许多附加映射信息,这使得一切看起来都过于复杂。还为什么要使用嵌入式 key ?我认为自动生成的 key 也适合您。我对您的模型的建议如下:
@Entity
@Table(name = "mandatory")
public class MandatoryEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// you need a mapped by to mark the relation as bidirectional
@OneToMany(mappedBy = "mandatory")
protected List additonals;
@OneToOne
protected ExtraEntity extra;
}
@Entity
@Table(name = "additional")
public class AdditionalEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
protected MandatoryEntity mandatory;
}
@Entity
@Table(name = "extra")
public class ExtraEntity {
// you might also not use auto-value here and set the id to the id of the owning
// mandatory entity
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "text")
protected String text;
}
如果我遗漏了一些要求,并且我建议的映射不适用于您的情况,那么请添加有关您的要求的更多信息,我们可以进一步讨论这个问题。
关于java - 一个实体类映射几个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13139935/
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!