我在使用hibernate进行一对多映射中更新连接表时遇到了一个hibernate问题。下面是我的两个实体类和连接表实体类。 ArticleCategoryMap.java
@Entity
@Table(name = "ARTICLECATEGORYMAP")
public class ArticleCategoryMap {
private static final long serialVersionUID = -5653708523600543988L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column ( name = "id")
Long id;
@ManyToOne(targetEntity = Article.class, fetch = FetchType.EAGER, optional = true, cascade = CascadeType.PERSIST)
@JoinColumn(name = "ARTICLE_ID", nullable = true, insertable = true, updatable = true)
private Article article;
@ManyToOne(targetEntity = Category.class, fetch = FetchType.EAGER, optional = true, cascade = CascadeType.PERSIST)
@JoinColumn(name = "CATEGORY_ID", nullable = true, insertable = true, updatable = true)
private Category category;
//setter and getter
}
Article.java
@Entity
@Table(name = "ARTICLE")
public class Article {
private long id;
private String title;
private String description;
private String keywords;
private String content;
@Id
@GeneratedValue
@Column(name = "ARTICLE_ID")
public long getId() {
return id;
}
//setter and getter
}
Category.java
@Entity
@Table(name = "CATEGORY")
public class Category {
private long id;
private String name;
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(
name = "ARTICLECATEGORYMAP",
joinColumns = @JoinColumn(name = "CATEGORY_ID"),
inverseJoinColumns = @JoinColumn(name = "ARTICLE_ID")
)
@CollectionId(
columns = @Column(name="id"),
type=@Type(type="long"),
generator = "sequence"
)
private Collection<Article> articles;
@Id
@GeneratedValue
@Column(name = "CATEGORY_ID")
public long getId() {
return id;
}
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(
name = "ARTICLECATEGORYMAP",
joinColumns = @JoinColumn(name = "CATEGORY_ID"),
inverseJoinColumns = @JoinColumn(name = "ARTICLE_ID")
)
@CollectionId(
columns = @Column(name="id"),
type=@Type(type="long"),
generator = "sequence"
)
// setter an getter
}
现在假设我第一次在article表中有2个元素,它们映射到类别表的一个条目。所以连接表看起来像
现在由于某种原因,我想更新文章条目将映射到新类别ID的条目。所以最终的DB应该是这样的
所以我的问题是如何更新此连接表。
答案 0 :(得分:0)
如果您想要一对多关系(1个类别包含多篇文章,1篇文章为1个类别),则不需要连接表。
实体类应如下所示:
类别实体:
包含一组文章:
@Entity
@Table(name = "CATEGORY")
public class Category {
private long id;
private String name;
@OneToMany(mappedBy="category")
private Set<Article> articles;
......
}
文章实体:
@Entity
@Table(name = "ARTICLE")
public class Article {
@ManyToOne
@JoinColumn(name="id", nullable=false)
private Category category;
private long id;
private String title;
private String description;
private String keywords;
private String content;
.......
}
有关详细信息,请查看hibernate-one-to-many。希望这会有所帮助。
同时将注释从方法移动到字段。这样:
private long id;
@Id
@GeneratedValue
@Column(name = "CATEGORY_ID")
public long getId() {
return id;
}
应该是:
@Id
@GeneratedValue
@Column(name = "CATEGORY_ID")
private long id;
public long getId() {
return id;
}
多对多的关系:
在您的数据库中,您有3个表:
- CATEGORY
- ARTICLE
- ARTICLECATEGORYMAP(连接表)
醇>
对于多对多的关系实体将是:
类别实体:
@Entity
@Table(name = "CATEGORY")
public class Category {
@Id
@GeneratedValue
@Column(name = "CATEGORY_ID")
private long id;
private String name;
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(
name = "ARTICLECATEGORYMAP",
joinColumns = { @JoinColumn(name = "CATEGORY_ID") },
inverseJoinColumns = { @JoinColumn(name = "ARTICLE_ID") }
)
Set<Article > articles = new HashSet<>();
.....
}
文章实体:
@Entity
@Table(name = "ARTICLE")
public class Article {
@Id
@GeneratedValue
@Column(name = "ARTICLE_ID")
private long id;
private String title;
private String description;
private String keywords;
private String content;
@ManyToMany(mappedBy = "articles")
private Set<Category> categories = new HashSet<>();
.......
}
有关详细信息,请查看many-to-many ralationship