存储没有映射表的JSP HashMap?

时间:2010-09-01 22:45:12

标签: java orm jpa annotations

是否可以有两个类TemplateTemplateItem,映射到两个数据库表templatetemplate_item,它们是使用Map<String, TemplateItem>连接的?如果可以的话,可以使用注释吗?

以下结果有三个表,即添加一个不必要的映射表。

@Entity
@Table(name = "template")
public class Template {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id")
    private long id = 0;

    @Column(name="name")
    private String name = "";

    // Left side of map maps to name field of the item on the right side of the map.
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    @MapKey(name = "name")
    private Map<String, TemplateItem> items = new HashMap<String, TemplateItem>();

}


@Entity
@Table(name = "template_item")
public class TemplateItem {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id")
    private long id = 0;

    // The name field is the unique key for the Template.items Map
    @Column(name="name")
    private String name = "";

    @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    private Template template;

    @Column(name="content")
    private String content = "";

}

在mysql中我们得到三个表,映射表包含从TemplateItem表中复制的两列:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| name        | varchar(100) | NO   | UNI | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| content     | longtext     | NO   |     | NULL    |                |
| name        | varchar(120) | NO   |     | NULL    |                |
| template_id | bigint(20)   | YES  | MUL | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

+----------------+------------+------+-----+---------+-------+
| Field          | Type       | Null | Key | Default | Extra |
+----------------+------------+------+-----+---------+-------+
| at_template_id | bigint(20) | NO   | PRI | NULL    |       |
| items_id       | bigint(20) | NO   | PRI | NULL    |       |
+----------------+------------+------+-----+---------+-------+

1 个答案:

答案 0 :(得分:2)

标准JPA使用连接表进行单向OneToMany。通过指定拥有方使关联成为双向,并且您不应该获得连接表:

@Entity
@Table(name = "template")
public class Template {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name="id")
  private long id = 0;

  @Column(name="name")
  private String name = "";

  // Left side of map maps to name field of the item on the right side of the map
  @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="template")
  @MapKey(name = "name")
  private Map items = new HashMap();
  ...
}