2012-02-21 8 views
0

私はHibernate reverse engを使用しています... 私はテーブルUserを持っています。 私はRecipe_FKを持つテーブルRecipeをUser.userIdに持っています。今Hibernateコード生成外部キー参照オブジェクト

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

- いくつかの質問:私は、コード生成を実行すると

は私が持つユーザーのためのPOJOファイルを取得 1.私はたSession.load(User.classは、ユーザーID)を行うと、 参加しますか?レシピテーブルからレシピを取得しますか?私はそれを望んでいない.... 私はそれを手動で削除することはできますか? どのように私は逆のことができます。この "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> 

ヨアフ

+0

レシピとユーザークラスの両方に記された完全な注釈を表示してください –

+0

編集 – user1136875

答えて

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ではなくマッピングにアノテーションを使用することを強くお勧めします。より標準的で、あまり冗長ではなく、はるかに簡単です。

+0

をご覧ください。 – user1136875

関連する問題