Hibernate - 自动为子项分配外键?

时间:2011-10-11 11:26:58

标签: hibernate composite-key

好的,所以我有两个对象,父母和孩子。孩子基本上看起来像这样:

public class Child implements Serializable 
{

    // primary (composite) key
    private int parentId;
    private String name;

    // random value
    private String val;

    public Child(String name, String val) {
        this.name = name;
        this.val = val;
        // note: parentId is not assigned a value
    }

    public void setParentId(int id) {

    [...]
}

我想要的是基本上hibernate在创建Parent对象后自动设置Child的父id。这是可能的,还是在将父对象保存到数据库后,是否必须将子对象添加到父对象中?

父级的xml映射如下:

`<map name="children" inverse="true" lazy="true" cascade="all,delete-orphan">
    <cache usage="nonstrict-read-write"/>
    <key column="parent_id"/>
    <index column="child_name" type="string"/>
    <one-to-many class="myPack.Child"/>
</map>`

和孩子的xml:

<hibernate-mapping package="myPack">

    <class name="Child" table="child_tbl" lazy="true">

        <composite-id>
            <key-property name="ParentId" type="int" column="parent_id"/>
            <key-property name="Name" column="name" type="string"/>
        </composite-id>

        <property name="Val" blablabla
[...]

2 个答案:

答案 0 :(得分:0)

当你希望hibernate将父子与外键一起使用对象映射而不是id时。

让我们从here

中取一个例子
public class AMain {

 //Properties of A.
 List<ASub3> subList;
.....
}

public class ASub3 {
 .....  
 AMain parent;
 .....
}

在AMain add中的hbm.xml文件中,

    <list name="subList" inverse="true" cascade="all" lazy="false">
         <key column="a_id" />
         <list-index column="as3_id" />
         <one-to-many class="com.manu.hibernate.mappings.domain.ASub3" />
    </list>

在Asub3中添加,

<many-to-one name="parent" class="com.manu.hibernate.mappings.domain.AMain" 
column="a_id" unique="true" cascade="save-update" />

现在,在客户端代码中使用如下的bean,

     AMain a = new AMain("A");
     ASub3 as3a = new ASub3("List - 1");
     ASub3 as3b = new ASub3("List - 2");
     ASub3 as3c = new ASub3("List - 3");
     as3a.setParent(a);
     as3b.setParent(a);
     as3c.setParent(a);
     List<ASub3> subList = new ArrayList<ASub3>();
     subList.add(as3a);
     subList.add(as3b);
     subList.add(as3c);

     a.setSubList(subList);

现在,如果您保存AMain,AMain和ASub3都会更新(我的意思是相应的表格),并带有一个外键给AMain。

答案 1 :(得分:0)

嗨天前我也面临同样的问题。我在注释类中有解决方案。因为我没有你的父班,所以让我选择我的假设

public class Parent implements Serializable 
{
   // Id is generated by sequence so it is not assigned when parent will be created
    @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_APP")
    @SequenceGenerator(name="SEQ_APP",sequenceName="SEQ_APP")
    private Integer id;
    private String name;

    @ManyToMany
    @JoinTable(
        name = "CHILD_TABLE",
        joinColumns = { @JoinColumn(name = "PARENT_ID")},  
        inverseJoinColumns = {@JoinColumn(name = "NAME")}
    )
    @Cascade(CascadeType.DELETE_ORPHAN)
   private Set<Child> childs= new HashSet<Child>();

   // getters and setters
}



public class Child {
       // primary (composite) key

       private int parentId;
       private String name;    }