是否可以将hibernate实体映射为id?

时间:2012-05-02 09:25:21

标签: hibernate jpa

我有ID的实体A APK有两个长字段,即其他实体的ID。所以我发现Hibernate缓存未命中,同时在实体A上执行添加/删除操作,为了避免这种情况,我想使用实体而不是长。

@Entity
@AccessType("field")
@Table(name = "A")
@XStreamAlias("A")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class A {

    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name = "x", column = @Column(name = "X_ID")),
            @AttributeOverride(name = "y", column = @Column(name = "Y_ID"))})
    private APK id;
   ...................
}


@AccessType("field")
@Embeddable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class APK implements Serializable {
    protected long x= 0;//this is id of another entity
    protected long y= 0;//this is id of another entity    
    ......
}

1 个答案:

答案 0 :(得分:1)

YES

@Entity
@AccessType("field")
@Table(name = "A")
@XStreamAlias("A")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class A implements SerializableObject {

    @Id
    @JoinColumn(name = "X_ID", nullable = false)
    @ManyToOne(targetEntity = X.class)
    protected X x;

    @Id
    @JoinColumn(name = "Y_ID", nullable = false)
    @ManyToOne(targetEntity = Y.class)
    protected y;
...........
}

P.S.但rhus没有解决缓存未命中