Hibernate映射一个表中的一对多

时间:2021-01-03 09:58:05

We have the following (poorly designed?) table:

我们有以下(设计不佳?)表:

inputs:
keyword_id serial not null,
group_name string not null,
banned_term string not null

Keyword ID is the primary key. there are many banned terms per group_name. The data looks like this:

关键字ID是主键。每个group_name有许多禁止的条款。数据如下所示:

keyword_id | group_name | banned_term
1 | incentivization | free money
2 | inaccuracy | we're number one
3 | incentivization | win a free ipod!

There's no join table, and group_name isn't its own entity. I'd like a domain object something like this:

没有连接表,group_name不是它自己的实体。我想要一个像这样的域对象:

class BannedTermGroup {
  Integer id;
  String group_name;
  Set<String> banned_terms;

  // ... various getters and setters
}

The only examples on this one-to-many relationship between the group name and banned terms all involve some sort of join column or join table, while group_name would always be part of some other entity. Here neither is the case. Can this be mapped using Hibernate?

组名和禁用术语之间的这种一对多关系的唯一示例都涉及某种连接列或连接表,而group_name将始终是某个其他实体的一部分。这两种情况都不是。可以使用Hibernate映射吗?

1 个解决方案

#1


0  

As quoted in my previous comment, you can get the ID of the group from nowhere.

正如我在之前的评论中引用的那样,您可以从任何地方获取该组的ID。

Just to put aside this problem, you may look into Hibernate's reference, search for collections of basic types. It is what you need.

只是抛开这个问题,您可以查看Hibernate的参考,搜索基本类型的集合。这是你需要的。

However, you still need to have a table for the "group".

但是,您仍然需要有一个“组”表。

assume it is as simple as a one column table:

假设它像一列表一样简单:

KEYWORD_GROUP
--------------------
GROUP_NAME   STRING

Your original keyword table:

您原来的关键字表格:

KEYWORD
--------------------
GROUP_NAME   STRING
KEYWORD      STRING

I believe this is what you need:

我相信这就是你所需要的:

@Entity
@Table("KEYWORD_GROUP")
public class KeywordGroup {
   [.....]

   @ElementCollection
   @CollectionTable(name="KEYWORD", joinColumns=@JoinColumn(name="GROUP_NAME"))
   @Column(name="KEYWORD")
   private List<String> keywords;
}

For the keyword_group table, you probably don't need to create a new table if you are just using this model for read. Create a view from your original keyword table should be good enough.

对于keyword_group表,如果您只是使用此模型进行读取,则可能不需要创建新表。从原始关键字表创建视图应该足够好。

#1


0  

As quoted in my previous comment, you can get the ID of the group from nowhere.

正如我在之前的评论中引用的那样,您可以从任何地方获取该组的ID。

Just to put aside this problem, you may look into Hibernate's reference, search for collections of basic types. It is what you need.

只是抛开这个问题,您可以查看Hibernate的参考,搜索基本类型的集合。这是你需要的。

However, you still need to have a table for the "group".

但是,您仍然需要有一个“组”表。

assume it is as simple as a one column table:

假设它像一列表一样简单:

KEYWORD_GROUP
--------------------
GROUP_NAME   STRING

Your original keyword table:

您原来的关键字表格:

KEYWORD
--------------------
GROUP_NAME   STRING
KEYWORD      STRING

I believe this is what you need:

我相信这就是你所需要的:

@Entity
@Table("KEYWORD_GROUP")
public class KeywordGroup {
   [.....]

   @ElementCollection
   @CollectionTable(name="KEYWORD", joinColumns=@JoinColumn(name="GROUP_NAME"))
   @Column(name="KEYWORD")
   private List<String> keywords;
}

For the keyword_group table, you probably don't need to create a new table if you are just using this model for read. Create a view from your original keyword table should be good enough.

对于keyword_group表,如果您只是使用此模型进行读取,则可能不需要创建新表。从原始关键字表创建视图应该足够好。