0

我正在使用 Hibernate 创建我的数据库方案。有一个名为“tbl_articles”的表和另一个名为“tbl_categories”的表。要建立 nn 关系,需要一个辅助表(“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。

4

1 回答 1

0

我已经通过添加和解决了@JoinColumn这个ArticleCategoryPK.article问题ArticleCategoryPK.category

于 2012-06-30T15:12:11.357 回答