0

我正在使用 spring 3.1、hibernate 4.0.1.FINAL 和 spring-data-jpa 1.0.2.RELEASE。所以假设我有一个many-to-manybetweenclass A并且class B我映射为ABMap. 我还有另一个多对多之间的class C 映射class DCDMap. 我有一个ABMapID带有 @Embeddable 注释的类

@Embeddable
public class ABMapID{
   private String aID;
   private String bID;
}
//... setters and getters

所以映射本身在下面

 @Entity
 public class ABMap {

 @Id
@AttributeOverrides({
  @AttributeOverride(name = "aID", column = @Column(name = "a_id",insertable = false, updatable = false)),
  @AttributeOverride(name = "bID", column = @Column(name = "b_id", insertable = false,updatable = false))
})
private ABMapID ID;
@ManyToOne(targetEntity = A.class)
@JoinColumn(name = "a_id",insertable = false,updatable = false)
private A a;

@ManyToOne(targetEntity = B.class)
@JoinColumn(name = "b_id", insertable = false, updatable = false)
private B b;
//.... setters and getters  
}

现在假设我们要更改ABMapACDMapaIDACDMapID和 CDMapID(它本身由 cID、dID 组成)组成的位置。所以我的新关联 ID 是 ACDMapID

 @Embeddable
 public class ACDMapID {
  private String aID;
  private CDMapID cdmapID;

} 

and the entity is mapped like so

@Entity
public class ACDMap {
   @Id
@AttributeOverrides({
  @AttributeOverride(name = "aID", column = @Column(name = "a_id",insertable = false, updatable = false)),
  @AttributeOverride(name = "cdmapID", column = @Column(name = "dcmap_id", insertable = false,updatable = false))
})
  private ACDMapID ID;

}

所以运行这个给我这个错误

原因:org.hibernate.AnnotationException:从 ACDMap 引用 CDMap 的外键具有错误的列数。应该是 2

有没有办法进行映射?更可取的是,我想保持 CDMap 原样。

4

2 回答 2

1

我想我会试一试,这是结果(没有尝试过,所以它是纯理论的)。

@Entity
public class A
{
    @Id
    private Long id;

    @ManyToMany
    @JoinTable(name = "A_B", joinColumns={
        @JoinColumn(name = "a_id")
    }, inverseJoinColumns = {
        @JoinColumn(name = "b_id")
    })
    private Collection<B> collectionOfB;

    @ManyToMany
    @JoinTable(name = "A_C_D", joinColumns={
        @JoinColumn(name ="a_id")
    }, inverseJoinColumns = {
        @JoinColumn(name = "c_id"),
        @JoinColumn(name = "d_id")
    })
    private Collection<ACD> collectionOfCD;
}

@Entity
public class B
{
    @Id
    private Long id;

    @ManyToMany(mappedBy = "collectionOfB")
    private Collection<A> collectionOfA;
}

@Entity
public class C
{
    @Id
    private Long id;

    @ManyToMany
    @JoinTable(name = "C_D", joinColumns={
        @JoinColumn(name = "c_id")
    }, inverseJoinColumns = {
        @JoinColumn(name = "d_id")
    })
    private Collection<D> collectionOfD;
}

@Entity
public class D
{
    @Id
    private Long id;

    @ManyToMany(mappedBy = "collectionOfD")
    private Collection<C> collectionOfC;
}

@Entity
@Table(name = "C_D")
public class CD
{
    @EmbeddedId
    private CDPK key;

    @MapsId("cId")
    @ManyToOne
    private C c;

    @MapsId("dId")
    @ManyToOne
    private D D;
}

@Embeddable
public class CDPK
{
    @Column(name = "c_id")
    private Long cId;
    @Column(name = "d_id")
    private Long dId;
}

我真的不确定这是否有效,因为有一个Entityusing the tableC_D和一个JoinTableusing the same table。

如果它有效,数据库将如下所示:

| A  |       | A_B         |      | B  |
| id |------<| a_id | b_id |>-----| id |
  |
  |
  |     | A_C_D              |
  \----<| a_id | c_id | d_id |
                 \/     \/
                 |      |
                 |      |
    | C  |     | C_D         |      | D  |
    | id |----<| c_id | d_id |>-----| id |
于 2012-06-11T23:12:35.807 回答
0

您似乎不了解 JPA 的概念。您不应该以编程方式处理 ID,而是处理对象。

您需要基于可嵌入对象中的 id 的动态关联。也许这样的构造可以用普通的 Hibernate,但它肯定不在 JPA 中。

JPA 的全部意义在于让您处理对象,而不是数据库关系。一旦你开始混合这两个方面,地狱就会崩溃,所以 JPA 的人决定不支持这种混合,我不能责怪他们。

于 2012-06-11T10:47:02.570 回答