hibernate一对一的映射示例

时间:2009-07-04 12:44:04

标签: hibernate

我有两张桌子 表1)cid jobtitle       2)pid jobspecif

我希望pid显示为table1中的forign键 任何人都可以为我提供hibernate mappping

2 个答案:

答案 0 :(得分:1)

您也可以使用注释

@Entity
@Table(name = "jobtitle")
public class jobtitle implements Serializable {

    @Id
    @Column(name = "cid ")
    @GeneratedValue
    private int cid ;    
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @OnDelete(action=OnDeleteAction.CASCADE)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    @JoinColumn(name = "jobspecif_fk", nullable=false)
    private jobspecif jobspe;


@Entity
@Table(name = "jobspecif")
public class jobspecif implements Serializable {

    @Id
    @GeneratedValue
    private int pid;   
    @OneToOne(mappedBy = "jobspecif", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    private jobtitle jobtit;

答案 1 :(得分:0)

由于我不知道你的数据模型我能给你的是这个。

<many-to-one name=„pid"
    column="pid"
    unique="true"
    not-null=„true" />

您应该将它放在代表第一个表的类的映射文件中。如果你想让它成为双向映射,你可以在第二类的映射文件中加入这样的东西。

<one-to-one name="name of the reference field for the first class in the second class"
    property-ref="pid"/>
相关问题