ManyToMany

Isto está certo

News

@ManyToMany(fetch = FetchType.LAZY) @JoinTable(name= "NEWS_CATEGORY_NEWS", joinColumns= {@JoinColumn(name= "NEWS_ID")}) private Set<Category> category;

Category

@ManyToMany(fetch = FetchType.LAZY) @JoinTable(name= "NEWS_CATEGORY_NEWS", joinColumns= {@JoinColumn(name= "CATEGORY_ID")}) private Set<News> news;

Gostaria de saber se ese relacionamento está certo

uma noticia pode fazer parte de varias categorias e varias categorias podem ter varias noticias

Boa tarde,
Para classes que possuem relacionamentos ManyToMany entre si, lhe sugiro o seguinte

Classe CategoryNews.java

[code]@Entity
public class CategoryNews implements Serializable
{
@EmbeddedId
private CategoryNews.PK pk;

@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = “CATEGORY_ID”, referencedColumnName = “CATEGORY_ID”, insertable = false, updatable = false)
private Category category;

@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = “NEWS_ID”, referencedColumnName = “NEWS_ID”, insertable = false, updatable = false)
private News news;

private static final long serialVersionUID = 1L;

public CategoryNews.PK getPk()
{
return this.pk;
}

public void setPk(CategoryNews.PK pk)
{
this.pk = pk;
}

public Category getCategory()
{
return category;
}

public void setCategory(Category category)
{
this.category = category;
}

public News getNews()
{
return news;
}

public void setNews(News news)
{
this.news = news;
}

@Embeddable
public static class PK implements Serializable
{

@Column(name = "newsId", nullable = false)
private Integer newsId;

@Column(name = "categoryId", nullable = false)
private Integer categoryId;

private static final long serialVersionUID = 1L;

public PK()
{
  super();
}

public Integer getNewsId()
{
  return newsId;
}

public void setNewsId(Integer newsId)
{
  this.newsId = newsId;
}

public Integer getCategoryId()
{
  return categoryId;
}

public void setCategoryId(Integer categoryId)
{
  this.categoryId = categoryId;
}

}

}[/code]

Classe Category.java:

@OneToMany(mappedBy = "category") private Collection<CategoryNews> categoryNews;

Classe News.java:

@OneToMany(mappedBy = "news") private Collection<News> news;

Pelo menos é assim que eu utilizo…