Search code examples
hibernateforeign-keyspojo

Hibernate code generation foreign keys reference objects


I am using Hibernate reverse eng... I have a table User. I have table Recipe with Recipe_FK - to User.userId.

When I run the code generation I get a POJO file for the user with:

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

Now - few questions: 1. When I do session.load( User.class, userId ); Will it do Join? and get the recipes from the Recipe table? I do not want that.... Can I manually delete it? How can I reverse eng. the tables without getting this "Set" reference parameters?

(I prefer to do the joins manually when needed to improve performances.)

Here are the .hbm.xml files:

<?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


Solution

  • See this line:

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

    The lazy="true" attribute means that the set will be loaded lazily, when you access it:

    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
    

    This is a must-know. Read the Hibernate documentation before using Hibernate, else your app will be a disaster. You HAVE to understand how it works to avoid making mistakes.

    I stronly suggest you use annotations for your mapping rather than XML. It's more standard, less verbose, and much easier to udnerstand.