Hibernate代码生成外键引用对象

Hibernate代码生成外键引用对象,hibernate,foreign-keys,pojo,Hibernate,Foreign Keys,Pojo,我正在使用Hibernate反向工程。。。 我有一个表用户。 我有带有Recipe_FK-to User.userId的表Recipe 运行代码生成时,我会为用户获得一个POJO文件,其中包含: private Set<?> recipeses = new HashSet<Object>(0); private Set recipes=new HashSet(0); 现在-几个问题: 1.当我执行session.load(User.class,userId)时; 它真

我正在使用Hibernate反向工程。。。 我有一个表用户。 我有带有Recipe_FK-to User.userId的表Recipe

运行代码生成时,我会为用户获得一个POJO文件,其中包含:

private Set<?> recipeses = new HashSet<Object>(0);
private Set recipes=new HashSet(0);
现在-几个问题: 1.当我执行session.load(User.class,userId)时; 它真的会加入吗?然后从配方表中获取配方?我不想。。。。 我可以手动删除它吗? 如何在不获取此“设置”参考参数的情况下反转表格

(当需要提高性能时,我更喜欢手动执行连接。)

以下是.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">
<!-- Generated Aug 26, 2011 10:56:44 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
 <class catalog="yoavby2_icdb" name="com.icdb.data.Recipe" table="recipes">
  <id name="recipeid" type="int">
   <column name="recipeid"/>
   <generator class="identity"/>
  </id>
  <many-to-one class="com.icdb.data.User" fetch="select" name="users">
   <column name="ownerid" not-null="true"/>
  </many-to-one>
  <property generated="never" lazy="false" name="releasedate" type="timestamp">
   <column length="19" name="releasedate" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="preparationtime" type="int">
   <column name="preparationtime" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="name" type="string">
   <column length="50" name="name" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="description" type="string">
   <column length="200" name="description"/>
  </property>
  <property generated="never" lazy="false" name="lastupdated" type="timestamp">
   <column length="19" name="lastupdated" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="servecount" type="java.lang.Integer">
   <column name="servecount"/>
  </property>
  <property generated="never" lazy="false" name="complete" type="boolean">
   <column name="complete" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="ratingOneStar" type="int">
   <column name="ratingOneStar" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="ratingTwoStar" type="int">
   <column name="ratingTwoStar" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="ratingThreeStar" type="int">
   <column name="ratingThreeStar" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="ratingFourStar" type="int">
   <column name="ratingFourStar" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="ratingFiveStar" type="int">
   <column name="ratingFiveStar" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="totalRating" type="float">
   <column name="totalRating" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="category" type="int">
   <column name="category" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="numOfViews" type="int">
   <column name="numOfViews" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="indexOfRecipeOfUser" type="int">
   <column name="indexOfRecipeOfUser" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="picUrl" type="string">
   <column length="200" name="picUrl" not-null="false"/>
  </property>
  <set fetch="select" inverse="true" lazy="true"
   name="recipeingredientses" sort="unsorted" table="recipeingredients">
   <key>
    <column name="recipeid" not-null="true"/>
   </key>
   <one-to-many class="com.icdb.data.RecipeIngredient"/>
  </set>
  <set fetch="select" inverse="true" lazy="true" name="recipereviewses"
   sort="unsorted" table="recipereviews">
   <key>
    <column name="recipeid" not-null="true"/>
   </key>
   <one-to-many class="com.icdb.data.RecipeReviews"/>
  </set>
  <set fetch="select" inverse="true" lazy="true"
   name="recipeinstructionses" sort="unsorted" table="recipeinstructions">
   <key>
    <column name="recipeid" not-null="true"/>
   </key>
   <one-to-many class="com.icdb.data.RecipeInstruction"/>
  </set>
  <property name="recipedifficulty" type="int">
   <column name="recipedifficulty" not-null="true" sql-type="INTEGER"/>
  </property>
 </class>
</hibernate-mapping>




<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20:18:59 15/02/2012 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.icdb.data.User" table="users" catalog="yoavby2_icdb">
        <id name="userid" type="java.lang.Integer">
            <column name="userid" />
            <generator class="identity" />
        </id>
        <property name="birthdate" type="string">
            <column name="birthdate" length="50" not-null="true" />
        </property>
        <property name="password" type="string">
            <column name="password" length="100" not-null="true" />
        </property>
        <property name="firstname" type="string">
            <column name="firstname" length="50" not-null="true" />
        </property>
        <property name="lastname" type="string">
            <column name="lastname" length="50" not-null="true" />
        </property>
        <property name="country" type="string">
            <column name="country" length="100" not-null="true" />
        </property>
        <property name="email" type="string">
            <column name="email" length="100" not-null="true" />
        </property>
        <property name="numOfRecipes" type="int">
            <column name="numOfRecipes" not-null="true" />
        </property>
        <property name="picUrl" type="string">
            <column name="picUrl" length="200" />
        </property>
        <set name="usermessagesesForSenderUserId" table="usermessages" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="senderUserId" not-null="true" />
            </key>
            <one-to-many class="com.icdb.data.Usermessages" />
        </set>
        <set name="usermessagesesForUserId" table="usermessages" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="userId" not-null="true" />
            </key>
            <one-to-many class="com.icdb.data.Usermessages" />
        </set>
        <set name="recipeses" table="recipes" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="ownerid" not-null="true" />
            </key>
            <one-to-many class="com.icdb.data.Recipe" />
        </set>
        <set name="generaltipses" table="generaltips" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="authorid" not-null="true" />
            </key>
            <one-to-many class="com.icdb.data.GeneralTip" />
        </set>
        <set name="usersesForFavUserId" table="userfavchefsync" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="userId" not-null="true" />
            </key>
            <many-to-many entity-name="com.icdb.data.User">
                <column name="favUserId" not-null="true" />
            </many-to-many>
        </set>
        <set name="friendshiptablesForUserBId" table="friendshiptable" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="userB_Id" not-null="true" />
            </key>
            <one-to-many class="com.icdb.data.Friendshiptable" />
        </set>
        <set name="usersesForUserId" table="userfavchefsync" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="favUserId" not-null="true" />
            </key>
            <many-to-many entity-name="com.icdb.data.User">
                <column name="userId" not-null="true" />
            </many-to-many>
        </set>
        <set name="friendshiptablesForUserAId" table="friendshiptable" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="userA_Id" not-null="true" />
            </key>
            <one-to-many class="com.icdb.data.Friendshiptable" />
        </set>
        <set name="userrecipessyncs" table="userrecipessync" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="userId" not-null="true" />
            </key>
            <one-to-many class="com.icdb.data.Userrecipessync" />
        </set>
        <set name="recipereviewses" table="recipereviews" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="reviewerid" not-null="true" />
            </key>
            <one-to-many class="com.icdb.data.RecipeReviews" />
        </set>
    </class>
</hibernate-mapping>

Yoav

见这一行:

<set name="recipeses" table="recipes" inverse="true" lazy="true" fetch="select">
这是必须知道的。在使用Hibernate之前请阅读,否则您的应用程序将是一场灾难。你必须了解它是如何工作的,以避免犯错误

我强烈建议您在映射中使用注释,而不是XML。它更标准,不太冗长,更容易理解。

请看这一行:

<set name="recipeses" table="recipes" inverse="true" lazy="true" fetch="select">
这是必须知道的。在使用Hibernate之前请阅读,否则您的应用程序将是一场灾难。你必须了解它是如何工作的,以避免犯错误


我强烈建议您在映射中使用注释,而不是XML。它更标准、更简洁、更易于理解。

请显示在Recipe和User类上标记的完整注释请显示在Recipe和User类上标记的完整注释