Hi,
I wish to retrieve a user record with all its associated order records by passing username and password parameters to the user class. As I have specified a set attribute with lazy=true, the issue I am having is where to place my Hibernate.Initialize line within the syntax. What I want to do is something like the following:
Code:
query = session.createQuery("from test.User as user where user.UserName=:username and user.UserPassword=:password")
.setString("username",username)
.setString("password",password);
user = (User) query.uniqueResult();
Hibernate.initialize(user.getUserOrders());
In the above code I know that the initialize line is incorrect, as when I run this code without it everything is fine minus the fact that I don't get any oredr records back.
How can I do this in this intance? In a non query based format I would do it as follows:
Code:
userSet = (User)session.load(User.class, password);
Hibernate.initialize(userSet.getUserOrders());
Hibernate version: JBoss 4.0
Mapping documents:Code:
<class name="User" table="usertbl">
<cache usage="read-write"/>
<id name="ID" column="user_id" type="integer" unsaved-value="0">
<generator class="sequence">
<param name="sequence">seq_user_mytable</param>
</generator>
</id>
<property name="FirstName" column="first_name" type="string" not-null="true"/>
<property name="LastName" column="last_name" type="string" not-null="true"/>
<property name="Address1" column="address1" type="string"/>
<property name="Address2" column="address2" type="string"/>
<property name="Address3" column="address3" type="string"/>
<property name="PostCode" column="postcode" type="string"/>
<property name="Country" column="country" type="string"/>
<property name="Email" column="email" type="string" not-null="true"/>
<property name="HomeTel" column="home_telephone" type="string"/>
<property name="HomeMob" column="mobile_telephone" type="string"/>
<property name="DateJoined" column="date_joined" type="timestamp"/>
<property name="UserName" type="string">
<column name="username" unique-key="UserPassKey"/>
</property>
<property name="UserPassword" type="string">
<column name="userpassword" unique-key="UserPassKey"/>
</property>
<set name="ArtistChoice" lazy="true" inverse="true" cascade="all-delete-orphan">
<cache usage="read-write"/>
<key column="user_id"/>
<one-to-many class="Artist"/>
</set>
<set name="UserOrders" lazy="true" inverse="true" cascade="all-delete-orphan">
<cache usage="read-write"/>
<key column="user_id"/>
<one-to-many class="Order"/>
</set>
</class>
Code between sessionFactory.openSession() and session.close():Code:
transaction = session.beginTransaction();
//Get User details
query = session.createQuery("from test.User as user where user.UserName=:username and user.UserPassword=:password")
.setString("username",username)
.setString("password",password);
user = (User) query.uniqueResult();
Hibernate.initialize(user.getUserOrders());
transaction.commit();