-->
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.  [ 3 posts ] 
Author Message
 Post subject: [Struts] many-to-one relationships and the web-tier
PostPosted: Sat May 21, 2005 4:15 pm 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
Hibernate version: 2.1.8
Name and version of the database you are using: MySQL 4.0.18

I have a question about many-to-one mappings in a Struts webtier. Especially about how to approach this kind of implementations.

One thing I could do is load all the many-to-one collections in my Struts action and display them in a select-box, just with their ID's as value. Than, when I submit my form, I will read the ID and retrieve this Object first.. before I can commit the rest of the formdata.

I have a user, who is working for a company. What I do is fill in the user's form and select the company from a select-box. In my struts action I retrieve the selected ID of the company and pass this ID to the service. Later on I match the User object with the company object.

Mapping document:
<hibernate-mapping>
<class name="com.jtemplate.vo.User" table="useraccount">
<id name="id" column="id" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="date" column="date" not-null="false"/>
<many-to-one name="company" column="companyID" />
</hibernate-mapping>

My struts form-bean mapping is as follows:
<form-bean name="prescriptionForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="id" type="java.lang.String"/>
<form-property name="date" type="java.lang.String"/>
<form-property name="companyID" type="java.lang.Long"/>
</form-bean>

But I don't like this kind of approach, because it feels like I am misleading the OR mapper. This kind of mapping should be handled by the OR mapper?

What is the best strategy to do this kind of mapping??
Or how do you guys do this?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 22, 2005 4:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
change
Code:
<form-bean name="prescriptionForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="id" type="java.lang.String"/>
<form-property name="date" type="java.lang.String"/>
<form-property name="companyID" type="java.lang.Long"/>
</form-bean>

to
Code:
<form-bean name="prescriptionForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="pojo" type="com.jtemplate.vo.User"/> </form-bean>


then in your jsp / struts taglib you can hit pojo.id, pojo.date, pojo.company.xxx

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 22, 2005 6:56 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
anthony wrote:
change
Code:
<form-bean name="prescriptionForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="id" type="java.lang.String"/>
<form-property name="date" type="java.lang.String"/>
<form-property name="companyID" type="java.lang.Long"/>
</form-bean>

to
Code:
<form-bean name="prescriptionForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="pojo" type="com.jtemplate.vo.User"/> </form-bean>


then in your jsp / struts taglib you can hit pojo.id, pojo.date, pojo.company.xxx

I tried to do that, but there seems to be a problem with the data field and the DynaBean.

I could just do the following:
<form-property name="company" type="com.jtemplate.vo.Company"/>
and than in my taglib: company.xxx

But what I wanted to know is which strategy is the best for this kind of mappings. As mentioned I have a struts-form where I can input all the user fields (firstname, lastname, gender, ...) But I also have a combobox with all available companies. I have a select box:
<html:select property="companyID">
<html:options collection="companyCollection" property="id" labelProperty="name"/>
</html:select>
And when I submit the form, I need to retrieve the company object from the database (findCompanyByID) AGAIN and than I can link the user object to the retrieved company object. I think there must be better ways to do this kind of mapping?
Or is this THE way to do it?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.