ManytoMany创建2个表

时间:2016-02-28 03:52:53

标签: mysql spring hibernate many-to-many

我有两个班级卡片和标签。我想要它们之间的多对多关系,并将cardId和tagId存储在一个表中。当我从代码创建数据库时,它创建了具有一对多关系的2个不同的表。 tag_cards和card_tags。我在这做错了什么?我想要一个有很多关系的单个表。

 import javax.faces.bean.ManagedBean;
   import javax.persistence.*;
 import java.util.Set;


  @ManagedBean(name = "addToCardBean")
@javax.persistence.Table(name = "cards")
@Entity
public class Card implements java.io.Serializable{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int id;

@Column(name="username",nullable = false)
private String username;
@Column(name="text",nullable = false)
private String text;
@Column(name="author")
private String author;
@Column(name="title")
private String title;
@Column (name="source")
private String source;
@Column(name="facebookID" ,columnDefinition="BigInt(20) default '0'")
private long facebookID;
@ElementCollection(targetClass = Tag.class)
private Set<Tag> tags;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getSource() {
    return source;
}

public void setSource(String source) {
    this.source = source;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public long getFacebookID() {
    return facebookID;
}

public void setFacebookID(long facebookID) {
    this.facebookID = facebookID;
}

@ManyToMany(mappedBy = "cards")
public Set<Tag> getTags() {
    return tags;
}

public void setTags(Set<Tag> tags) {
    this.tags = tags;
}
}

 import javax.faces.bean.ManagedBean;
 import javax.persistence.*;
import java.util.Set;


@Entity
 @Table(name="tag")
@ManagedBean(name="tagBean")
public class Tag implements java.io.Serializable{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;

@Column(name = "tagName", nullable = false)
private String tagName;
@ElementCollection(targetClass = Card.class)
private Set<Card> cards;
public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getTagName() {
    return tagName;
}

public void setTagName(String tagName) {
    this.tagName = tagName;
}


@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
        name = "cards_tags",
        joinColumns = @JoinColumn(name = "id"),
        inverseJoinColumns = @JoinColumn(name = "id")
)
public Set<Card> getCards() {
    return cards;
}

public void setCards(Set<Card> cards) {
    this.cards = cards;
}
}

1 个答案:

答案 0 :(得分:1)

您正面临这个问题,因为您已经在字段和方法(getter)级别使用了注释,即您已将两者都混合使用。

@Column(name = "tagName", nullable = false)
private String tagName;

@ManyToMany(cascade = CascadeType.ALL)
 @JoinTable(
    name = "cards_tags",
    joinColumns = @JoinColumn(name = "id"),
    inverseJoinColumns = @JoinColumn(name = "id")
)
public Set<Card> getCards() {
    return cards;
}

您必须仅在字段上使用它们或仅在方法(getter)上使用它们。不要混淆两者。

为什么你最终得到两个表是因为正在考虑字段注释,并且多对多的映射是在getter方法上,而不考虑。

一种方法是在现场级别使用。如果在字段处注释,请更改为以下内容(您还需要修改其他实体类):

@ManyToMany(cascade = CascadeType.ALL)
 @JoinTable(
    name = "cards_tags",
    joinColumns = @JoinColumn(name = "id"),
    inverseJoinColumns = @JoinColumn(name = "id")
)
@ElementCollection(targetClass = Card.class)
private Set<Card> cards;

您需要在两个班级中进行更改。如上所述,将注释从getter移动到field。或者你必须进行更改,以便所有注释都在getter而不是field。

其他链接: https://softwareengineering.stackexchange.com/questions/258541/where-to-put-jpa-annotations-field-or-getter

the difference between anotating a field and its getter method JPA