使用compositeKey的Hibernate示例OneToMany [英] Hibernate Example OneToMany with compositeKey

查看:159
本文介绍了使用compositeKey的Hibernate示例OneToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以给我一个Hibernate映射的示例,用于以下情况:

Can you give me an example of a Hibernate mapping for the following situation:


  1. 父表( foo ),带有一个简单的主键( foo_id

  2. 子表( bar ),复合键包含

  1. Parent table(foo) with a simple primary key (foo_id)
  2. child table(bar) with a composite key consisting of

a)父表的外键( foo_id

b)类型为字符串的键( item

b) key(item) of type string


推荐答案

我还没有完全按照你的要求完成,但这可能会让你朝着正确的方向前进。我认为它应该工作。这个页面更详细地解释了这些注释。

I haven't done exactly what you are asking for but this might start you off in the right direction. I think it should work. This page explains these annotations in greater detail.

@Entity
public class Foo {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long getId(){

    }
    ...
    @OneToMany(mappedBy="foo")
    public Collection<Bar> getBars() {

    }


...
}


@Entity
@IdClass(BarPk.class)
public class Bar implements Serializable {
    @ManyToOne
    @JoinColumn(name="foo")
    @Id
    public Foo getFoo() {
        return foo;
    }

    @Column(name="key", length=255)
    @Id
    public String getKey(){

    }

}


@Embeddable
public class BarPk implements Serializable {
    public Foo getFoo() {
        return foo;
    }
    public void setFoo(Foo foo) {

    }
    public String getKey(){
    ...
    }   

    public void setKey(String key){
    ...
    }     

    //you must include equals() and hashcode()    
}

这篇关于使用compositeKey的Hibernate示例OneToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆