开发者

Mapping a map collection with JPA annotations

开发者 https://www.devze.com 2023-01-28 18:31 出处:网络
Hi guys I am migrating an application using hibernate from xml to JPA annotations. Currently I am stuck on mapping a map of objects with compound id using annotations

Hi guys I am migrating an application using hibernate from xml to JPA annotations. Currently I am stuck on mapping a map of objects with compound id using annotations This is how am trying to do it.



class A
{
  ...
  @OneToMany()
  @JoinColumn(name="A_ID")
  /*   B_ID should be the key in this开发者_StackOverflow中文版 map */
  private Map map = new HashMap();
}

class B
{
  @EmbeddedId
  private CompoundId id;
}

@Embeddable
class CompoundId
{
  @Column(name = "A_ID")
  String aId;

  @Column(name = "B_ID")
  long bId
}

I have also tried

@OneToMany()
@JoinColumn(name="A_ID", insertable=false, updatable=false)
@MapKeyColumn(name="B_ID")
private Map map = new HashMap();

Caused by: java.sql.SQLException: ORA-00904: "B1_"."ID": invalid identifier             

with no luck

in mapping XML it looks like this

<map name="map" inverse="true" cascade="all-delete-orphan" lazy="false">
<key>
    <column name="A_ID" />
</key>

<map-key type="long" column="B_ID"/>
<one-to-many class="B" />
</map>

hibernate 3.2.7.ga and annotations 3.4.0.GA

thx !


Give this a try:

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

    @OneToMany()
    @MapKeyColumn(name="B_ID")
    private Map<String, Long> associatedBs;
    // ...
}

I might be missing something, but the @MapKeyColumn is the important part. I have done this successfully before but my code is at the office so I can't double check it.


@OneToMany()
@JoinColumn(name="A_ID", insertable=false, updatable=false)
@MapKeyColumn(name="B_ID")
private Map map = new HashMap();

did work, the problem appeared to be in mapping of composite id of B class

Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消