映射仅包含复合PK而没有

映射仅包含复合PK而没有

本文介绍了映射仅包含复合PK而没有@IdClass或@EmbeddedId的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个带有简单PK的表AB.

I've got two tables A and B with simple PK's.

@Entity
public class A {
    @Id
    public int idA;
}

@Entity
public class B {
    @Id
    public int idB;
}

我想映射一个新的关联类AB,它简单地存储了AB之间的关系,并带有复合PK idA + idB. AB没有任何额外的列,只有idAidB之间的关系.

I want to map a new association class AB that simply stores the relations between A and B, with composite PK idA+idB. AB doesn't have any extra columns, just the relation between idA and idB.

是否可以使用单个类映射AB?我想避免创建新的ABId类,而只是将其用作AB中的@IdClass@EmbeddedId,并且我不想将其与A上的@ManyToMany关联映射B.

Is it possible to map AB using a single class? I want to avoid having to create a new ABId class just to use it as @IdClass or @EmbeddedId in AB, and I don't want to map this with a @ManyToMany association on A or B.

推荐答案

为什么要映射这样的联接表?只需使用A和B之间的ManyToMany关联.当您向A中包含的B列表中添加B或从中删除B时,该连接表将被自动处理.

Why do you want to map such a join table? Just use a ManyToMany association between A and B. This join table will then be handled automatically when you'll add/remove a B to/from the list of Bs contained in A.

请参见 http://docs.jboss. org/hibernate/core/3.6/reference/zh-CN/html_single/#d0e11402

如果您确实想这样做,则只需将两个ID映射为@Id即可.主要类将是实体类本身(必须可序列化),如休眠参考文档.请注意,这是特定于Hibernate的.

If you really want to do that, then just map the two IDs with the @Id notation. The primary class will be the entity class itself (which must be serializable), as explained in the hibernate reference documentation. Note that this is Hibernate-specific.

这篇关于映射仅包含复合PK而没有@IdClass或@EmbeddedId的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:24