简体   繁体   English

一对一的单向hibernate映射不会在创建表时创建外键

[英]one-to-one unidirectional hibernate mapping is not creating foreign key while table creation

anyone tell me where I am wrong. 有谁告诉我哪里错了。 Already had wasted a day. 已经浪费了一天。 Problem is why hibernate(v 3.5.0 FINAL) is not creating foreign key in cdl_group_module table? 问题是为什么hibernate(v 3.5.0 FINAL)没有在cdl_group_module表中创建外键? It just create table having only single column ie id(PK). 它只创建只有单列即id(PK)的表。 I want one-to-one unidirectional mapping. 我想要一对一的单向映射。 I can not use bi-directional mapping because cdl_group is also used by other tables same as for cdl_module. 我不能使用双向映射,因为cdl_group也被其他表使用,与cdl_module相同。

Below is my POJO. 以下是我的POJO。

public class CDLGroupModule extends AbstractDomainObject {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private Group group;
    private CDLModule cdlModule;

    /**
     * @return the group
     */
    public Group getGroup() {
        return group;
    }

    /**
     * @param group
     *            the group to set
     */
    public void setGroup(Group group) {
        this.group = group;
    }

    /**
     * @return the cdlModule
     */
    public CDLModule getCdlModule() {
        return cdlModule;
    }

    /**
     * @param cdlModule
     *            the cdlModule to set
     */
    public void setCdlModule(CDLModule cdlModule) {
        this.cdlModule = cdlModule;
    }

}


public class Group extends AbstractDomainObject{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String name;
    private String description;

    public Group() {
    }

    public Group(String name, String description) {
        this.name = name;
        this.description = description;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }
    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "[Group : { id : " + id + ", name : " + name + ", description :" + description + " }]";
    }

}


public class CDLModule extends AbstractDomainObject {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private CdlModuleEnum module;

    /**
     * @return the module
     */
    public CdlModuleEnum getModule() {
        return module;
    }

    /**
     * @param module
     *            the module to set
     */
    public void setModule(CdlModuleEnum module) {
        this.module = module;
    }

}

Below is my hbm.xml 下面是我的hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.qait.cdl.commons.domain">
    <class name="CDLGroupModule" table="cdl_group_module">

        <id name="id" type="long" column="id">
            <generator class="native" />
        </id>

         <!-- <many-to-one name="group" cascade="all" fetch="join"
            lazy="false" class="Group" not-null="true" unique="true"/>

        <many-to-one name="cdlModule" cascade="all" fetch="join"
            lazy="false" class="CDLModule" not-null="true" unique="true"/> -->

        <one-to-one name="group" cascade="all-delete-orphan" fetch="join"
            lazy="false" class="Group" />

        <one-to-one name="cdlModule" cascade="all-delete-orphan" fetch="join"
            lazy="false" class="CDLModule" />


    </class>
</hibernate-mapping>

Hibernate created cdl_group and cdl_module table already successfully. Hibernate已经成功创建了cdl_groupcdl_module表。 My id property is present in AbstractDomainObject class which all POJO's are extending. 我的id属性存在于所有POJO都在扩展的AbstractDomainObject类中。 If I tried many-to-one mapping using unique=true instead of one-to-one mapping than hibernate doesn't even create table. 如果我尝试使用unique = true而不是一对一映射进行多对一映射 ,那么hibernate甚至不会创建表。 What is the reason behind that? 这背后的原因是什么? Any kind of help will be appreciable. 任何形式的帮助都会很明显。 You can ask if you want more info from my side. 您可以询问是否需要我方提供更多信息。

It seems that hibernate for one to one will try to map on the PK. 似乎hibernate for one to one会尝试映射PK。 When fetching cdl_group_module it will try to populate the relationship with any cdl_group with the same PK as the PK of cdl_group_module. 在获取cdl_group_module时,它将尝试使用与cdl_group_module的PK相同的PK填充与任何cdl_group的关系。 This gets pretty bogey when you remove and add new cdl_group enities. 当您删除并添加新的cdl_group enities时,这会变得很糟糕。

Try setting this property to create a FK: "property-ref (optional): the name of a property of the associated class that is joined to the primary key of this class. If not specified, the primary key of the associated class is used." 尝试设置此属性以创建FK:“property-ref(可选):连接到此类主键的关联类的属性名称。如果未指定,则使用关联类的主键“。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM