我正在使用Hibernate创建我的数据库方案。有一个名为“tbl_articles”的表和另一个名为“tbl_categories”的表。要有一个n-n关系,需要一个辅助表(“tbl_articles_categories”)。
以下是所有必要的实体:
@Entity
@Table( name = "tbl_articles" )
public class Article implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column( nullable = false )
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Integer id;
// other fields...
public Integer getId()
{
return id;
}
public void setId( Integer id )
{
this.id = id;
}
// other fields...
}
@Entity
@Table( name = "tbl_categories" )
public class Category implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column( nullable = false )
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Integer id;
// other fields
public Integer getId()
{
return id;
}
public void setId( Integer id )
{
this.id = id;
}
// other fields...
}
@Entity
@Table( name = "tbl_articles_categories" )
@AssociationOverrides({
@AssociationOverride( name = "pk.article", joinColumns = @JoinColumn( name = "article_id" ) ),
@AssociationOverride( name = "pk.category", joinColumns = @JoinColumn( name = "category_id" ) )
})
public class ArticleCategory
{
private ArticleCategoryPK pk = new ArticleCategoryPK();
public void setPk( ArticleCategoryPK pk )
{
this.pk = pk;
}
@EmbeddedId
public ArticleCategoryPK getPk()
{
return pk;
}
@Transient
public Article getArticle()
{
return pk.getArticle();
}
public void setArticle( Article article )
{
pk.setArticle( article );
}
@Transient
public Category getCategory()
{
return pk.getCategory();
}
public void setCategory( Category category )
{
pk.setCategory( category );
}
}
@Embeddable
public class ArticleCategoryPK implements Serializable
{
private static final long serialVersionUID = 1L;
@ManyToOne
@ForeignKey( name = "tbl_articles_categories_fkey_article_id" )
private Article article;
@ManyToOne
@ForeignKey( name = "tbl_articles_categories_fkey_category_id" )
private Category category;
public ArticleCategoryPK( Article article, Category category )
{
setArticle( article );
setCategory( category );
}
public ArticleCategoryPK()
{
}
public Article getArticle()
{
return article;
}
public void setArticle( Article article )
{
this.article = article;
}
public Category getCategory()
{
return category;
}
public void setCategory( Category category )
{
this.category = category;
}
}
现在,我在我的文章表和我的类别表中得到了我想要的串行类型。但是查看我的帮助器表,没有预期的字段article_id和category_id,每个类型都是整数 - 而是有类型为bytea的文章和类别。
这里有什么问题?
编辑:对不起,忘了提到我正在使用PostgreSQL。答案 0 :(得分:0)
我通过将@JoinColumn
添加到ArticleCategoryPK.article
和ArticleCategoryPK.category