Hibernate代码生成外键引用对象

时间:2012-02-21 16:41:37

标签: hibernate foreign-keys pojo

我正在使用Hibernate反向引擎...... 我有一个表用户。 我有Recipe_FK表格配方 - 到User.userId。

当我运行代码生成时,我得到了一个用户的POJO文件:

private Set<?> recipeses = new HashSet<Object>(0);

现在 - 几个问题: 1.当我做session.load(User.class,userId); 它会加入吗?并从食谱表中获取食谱?我不要那个.... 我可以手动删除它吗? 我怎么能倒转eng。表没有得到这个“Set”参考参数?

(我更喜欢在需要时手动进行连接以提高性能。)

以下是.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>

约阿夫

1 个答案:

答案 0 :(得分:3)

见这一行:

<set name="recipeses" table="recipes" inverse="true" lazy="true" fetch="select">

lazy="true"属性意味着当您访问该集时,将懒惰地加载该集:

User u = session.find(User.class, someId); // only load the data from the User table
Set<Recipe> recipes = u.getRecipes(); // returns the set, which is not loaded yet
int size = recipes.size(); // loads the recipes linked to the user from the database, and returns the size

这是必须知道的。在使用Hibernate之前阅读Hibernate documentation,否则您的应用将是一场灾难。你必须要了解如何避免犯错误。

我强烈建议您使用注释来映射而不是XML。它更标准,更简洁,也更容易使用。