2010-09-01 36 views
1

它可能有兩個類TemplateTemplateItem,映射到兩個數據庫表templatetemplate_item,它們使用Map<String, TemplateItem>?如果是這樣,可以使用註釋完成?存儲沒有映射表的JSP HashMap?

以下結果在三個表中,即添加一個不必要的映射表。

@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 |  | 
+----------------+------------+------+-----+---------+-------+ 
+0

使用連接表有什麼問題?這是規範化模式的一種非常標準的方式。 – 2010-09-01 22:48:55

+0

它是完全多餘的。自動創建的連接表有兩個字段,這兩個字段已存儲在'TemplateItem'類中。 – Jacob 2010-09-01 22:50:42

回答

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(); 
    ... 
}