-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 
Author Message
 Post subject: Lazy = true or false?? Many-to-one relationship...
PostPosted: Mon Jul 18, 2005 7:27 pm 
Newbie

Joined: Thu Jun 16, 2005 8:01 pm
Posts: 14
Hi...

I can't see it anymore. searched the forum and just can't find the right solution... that's why a new post.


I've got two database tables. User and UserGroup
Code:
+---------------+
|  User         |      +---------------+
+---------------+      | UserGroup     |
| userId        |      +---------------+
| userGroupId   | ---- | userGroupId   |
| userName      |      | userGroupName |
| usePassword   |      +---------------+
+---------------+


My mapping files:
Code:
    <class name="User" table="USER">
        <id name="userId" column="USER_ID" type="java.lang.Integer">
            <generator class="increment"/>
        </id>

        <property name="userName" column="USER_NAME" type="java.lang.String"  not-null="true" />
        <property name="userPassword" column="USER_PASSWORD" type="java.lang.String"  not-null="true" />

        <many-to-one name="userGroup" column="USER_GROUP_ID" class="UserGroup" />
    </class>
and
Code:
    <class name="UserGroup" table="USER_GROUP">
        <id name="userGroupId" column="USER_GROUP_ID" type="java.lang.Integer">
            <generator class="increment"/>
        </id>

        <property name="userGroupName" column="USER_GROUP_NAME" type="java.lang.String"  not-null="true" />
    </class>


I tried several things... to put constrainted="true" and outer-join="false" into the <many-to-one ...>

Put lazy="true" into the userGroup class... nothing worked.

When I set a userId via a pojo I want to get the userGroupName:

User user = new User(1);
System.out.println ("" + user.getUserGroup().getUserGroupName());

At this moment user.getUserGroup() returns null and should not be null...

I hope someone can point me into the correct direction.

Many thanks in advance
Statix


Top
 Profile  
 
 Post subject: Re: Lazy = true or false?? Many-to-one relationship...
PostPosted: Mon Jul 18, 2005 7:38 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Statix wrote:
Hi...

I can't see it anymore. searched the forum and just can't find the right solution... that's why a new post.
...
...
...

When I set a userId via a pojo I want to get the userGroupName:

User user = new User(1);
System.out.println ("" + user.getUserGroup().getUserGroupName());

At this moment user.getUserGroup() returns null and should not be null...

I hope someone can point me into the correct direction.

Many thanks in advance
Statix


User user = new User(1) does nothing with Hibernate unless you have code in your constructor that opens a Hibernate Session, and does a load(), get(), or other query using id=1 to fetch the user from the database.

As for the proper syntax for non-lazy loading of a many-to-one, use fetch="join" in Hibernate 3.x


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 3:39 am 
Newbie

Joined: Thu Jun 16, 2005 8:01 pm
Posts: 14
Ok. I will try it tonight because im on work now.

I currently use hibernate 3.x and there is a session started. I've query-ed the database with example.Create () and have a correct result... with user-id 1.

Hope your anwer will help. Tnx a lot


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 2:03 pm 
Newbie

Joined: Thu Jun 16, 2005 8:01 pm
Posts: 14
When I use the fetch="join" I get a large exception...

This is my mapping:
Code:
<hibernate-mapping package="com.gfs.pojo">

    <class name="User" table="USER" fetch="join">
        <id name="userId" column="USER_ID" type="java.lang.Integer">
            <generator class="increment"/>
        </id>

        <property name="userName" column="USER_NAME" type="java.lang.String"  not-null="true" />
        <property name="userPassword" column="USER_PASSWORD" type="java.lang.String"  not-null="true" />

        <many-to-one name="userGroup" column="USER_GROUP_ID" class="UserGroup" />
    </class>   
</hibernate-mapping>


My error:
Code:
20-jul-2005 19:57:25 org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(11) Attribute "fetch" must be declared for element type "class".

...bla bla bla and a lot more scary red words indicating something is wrong...


Anyway.... I searched and searched a lot at this website, google and a few other websites. Nothing can tell me exactly what I need to know.

I use Hibernate 3 so lazy="false" isn't necessary if i'm correct.

~I have usergroups and users.
~One usergroup can have more users
~A user must be in a usergroup

My piece of java code
Code:
      User criteria = new User();
      criteria.setUserName(username.toString());
      criteria.setUserPassword(password.toString());

      Session session = sessions.openSession();
      
      Transaction tx = session.beginTransaction();

      Criteria crit = session.createCriteria(User.class);
      
      crit.add(Example.create(criteria));
      System.out.println("Debug: HN: List()");
      List result = crit.list();


Why can't i call 'criteria.getUserGroup().getUserGroupName();' ??

getUserGroup returns Null so I need a sort of lazy loading right?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 2:21 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Statix wrote:
When I use the fetch="join" I get a large exception...

This is my mapping:
Code:
<hibernate-mapping package="com.gfs.pojo">

    <class name="User" table="USER" fetch="join">
        <id name="userId" column="USER_ID" type="java.lang.Integer">
            <generator class="increment"/>
        </id>

        <property name="userName" column="USER_NAME" type="java.lang.String"  not-null="true" />
        <property name="userPassword" column="USER_PASSWORD" type="java.lang.String"  not-null="true" />

        <many-to-one name="userGroup" column="USER_GROUP_ID" class="UserGroup" />
    </class>   
</hibernate-mapping>


My error:
Code:
20-jul-2005 19:57:25 org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(11) Attribute "fetch" must be declared for element type "class".

...bla bla bla and a lot more scary red words indicating something is wrong...


Anyway.... I searched and searched a lot at this website, google and a few other websites. Nothing can tell me exactly what I need to know.

I use Hibernate 3 so lazy="false" isn't necessary if i'm correct.

~I have usergroups and users.
~One usergroup can have more users
~A user must be in a usergroup

My piece of java code
Code:
      User criteria = new User();
      criteria.setUserName(username.toString());
      criteria.setUserPassword(password.toString());

      Session session = sessions.openSession();
      
      Transaction tx = session.beginTransaction();

      Criteria crit = session.createCriteria(User.class);
      
      crit.add(Example.create(criteria));
      System.out.println("Debug: HN: List()");
      List result = crit.list();


Why can't i call 'criteria.getUserGroup().getUserGroupName();' ??

getUserGroup returns Null so I need a sort of lazy loading right?

Thanks in advance.



Use fetch="join" on the association, not the class.
<many-to-one name="userGroup" column="USER_GROUP_ID" class="UserGroup" fetch="join"/>

The way you have the code set up, "criteria" is just an example User object. It only contains the values you set which are username and password.

The variable "result" should contain a java.lang.List of User Objects that match that username and password. These objects should be fully populated.

(The Transaction code isn't necessary for reading)

And, as long as you haven't closed your session, lazy-loading shouldn't even be an issue.

For example:
Code:
User exmpUser = new User();
exmplUser.setSomething(something);

Session session = sessions.openSession();
List results = session.createCriteria(User.class).add(Example.create(exmpUser);

for(Iterator i = results.iterator();i.hasNext();) {
   User user = (User)i.next();
   System.out.println(user.getUserGroup().getUserGroupName());
}

session.closeSession();

The UserGroup object would be lazily loaded when you access it unless A) The session is already closed (if session.closeSession was before for-loop) or B) if the association was mapped with fetch="join" as I've shown above.

The Example object, in your case the "criteria" object, never gets populated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 2:35 pm 
Newbie

Joined: Thu Jun 16, 2005 8:01 pm
Posts: 14
Alright I get it.

But if I put fetch="join" into the many-to-one I also get an error:
Code:
INFO: Mapping resource: com/gfs/pojo/User.hbm.xml
20-jul-2005 20:27:09 org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(19) Attribute "fetch" must be declared for element type "many-to-one".


:s I dont get it...

How do I get the userGroupName then??

At this moment I think it's like this.
Via the example.create I say it's user johndoe with it's password.
In the database the relation to UserGroup say's johndoe belongs to userGroupName programmer.

So I think that if the User is already set to johndoe that the relation also exists?

Probably isn't this the correct way of thinking.

Already tried some other things like:
UserGroup ug = criteria.getUserGroup();
ug.getUserGroupName() isn't working either.

I just tried to view criteria.getUserGroup().getUserGroupName() before the session is closed and not working either...

criteria.getUserGroup() stays null

im desperate, but not giving up...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 4:50 pm 
Newbie

Joined: Thu Jun 16, 2005 8:01 pm
Posts: 14
Anyway... I do not use anything like a set orso... maybe that's important?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 5:46 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Statix wrote:
Alright I get it.

But if I put fetch="join" into the many-to-one I also get an error:
Code:
INFO: Mapping resource: com/gfs/pojo/User.hbm.xml
20-jul-2005 20:27:09 org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(19) Attribute "fetch" must be declared for element type "many-to-one".


:s I dont get it...

How do I get the userGroupName then??

At this moment I think it's like this.
Via the example.create I say it's user johndoe with it's password.
In the database the relation to UserGroup say's johndoe belongs to userGroupName programmer.

So I think that if the User is already set to johndoe that the relation also exists?

Probably isn't this the correct way of thinking.

Already tried some other things like:
UserGroup ug = criteria.getUserGroup();
ug.getUserGroupName() isn't working either.

I just tried to view criteria.getUserGroup().getUserGroupName() before the session is closed and not working either...

criteria.getUserGroup() stays null

im desperate, but not giving up...


Your criteria variable will NEVER have any data from the database in it. You're using it as an example, Hibernate WILL NOT populate it.

Look at my example code in my last post. The results collection will have all User objects that match the criteria, returned by Hibernate from the Database. The criteria object will remain UNCHANGED by Hibernate!

As for the error using fetch=join on the <many-to-one> are you sure you've updated the DTD in your mapping document ?

For Hibernate 3, it should be
Code:
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 5:47 pm 
Newbie

Joined: Thu Jun 16, 2005 8:01 pm
Posts: 14
Ok for the ones who wants to know what the solution is :)

My login buttonpressed code:
Code:
         List lijst = LoginService.getInstance().checkUser(tfUsername.getText(), tfPassword.getText());
         Iterator it = lijst.iterator();
         while(it.hasNext()) {
            User usr = (User) it.next();
            UserGroup ug = (UserGroup) usr.getUserGroup();
            System.out.println("USERGROUP ID: "+ug.getUserGroupName());
            //doe iets met je user
         }   


My session + create code
Code:
      User criteria = new User();
      criteria.setUserName(username.toString());
      criteria.setUserPassword(password.toString());


      System.out.println("Debug: Username in object: " + criteria.getUserName());
      
      
      System.out.println("Debug: HIBERNATE: Open session");
      Session session = sessions.openSession();
      
      System.out.println("Debug: HIBERNATE: Begin transaction");
      Transaction tx = session.beginTransaction();

      System.out.println("Debug: HIBERNATE: Craeting criteria");
      Criteria crit = session.createCriteria(User.class);
      
      System.out.println("Debug: HIBERNATE: Creating example");
      crit.add(Example.create(criteria));
      System.out.println("Debug: HN: List()");
      List result = crit.list();


And the key of it all was to put lazy="false" to the class of the foreign key table

Noting special to do here :)
User mapping
Code:
<hibernate-mapping package="com.gfs.pojo">

    <class name="User" table="USER">
        <id name="userId" column="USER_ID" type="java.lang.Integer">
            <generator class="increment"/>
        </id>

        <property name="userName" column="USER_NAME" type="java.lang.String"  not-null="true" />
        <property name="userPassword" column="USER_PASSWORD" type="java.lang.String"  not-null="true" />

        <many-to-one name="userGroup" column="USER_GROUP_ID" class="UserGroup" />
    </class>
   
</hibernate-mapping>


Watch it! Put lazy="false" in the class tag to load the data in your object too.
UserGroup mapping
Code:
<hibernate-mapping package="com.gfs.pojo">

    <class name="UserGroup" table="USER_GROUP" lazy="false">
        <id name="userGroupId" column="USER_GROUP_ID" type="java.lang.Integer">
            <generator class="increment"/>
        </id>

        <property name="userGroupName" column="USER_GROUP_NAME" type="java.lang.String"  not-null="true" />
    </class>
   
</hibernate-mapping>


Loop through the list and everything is fine !

Hope it is usefull for someone :D


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 5:50 pm 
Newbie

Joined: Thu Jun 16, 2005 8:01 pm
Posts: 14
Just saw you've posted another post.

I did not update the DTD orso at all... All my mappings are generated by MyEclipse database tool...

Thanks for all your help and time. I really appreciate it!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 5:51 pm 
Newbie

Joined: Thu Jun 16, 2005 8:01 pm
Posts: 14
Checked it to be sure... .my dtd is version 2.0
Code:
<!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >


Shall I change it now to v3.0 ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 5:53 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Statix wrote:
Checked it to be sure... .my dtd is version 2.0
Code:
<!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >


Shall I change it now to v3.0 ?


If you're using the h3 jars, you should definitely change it. Using the wrong dtd will slow down processing considerably.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 6:13 pm 
Newbie

Joined: Thu Jun 16, 2005 8:01 pm
Posts: 14
tnx. i've given you your credits :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.