Solved

extends DTO

Posted on 2014-01-15
13
405 Views
Last Modified: 2014-01-20
I have these classes...

class MoreVAdto  extends VAdto{
//additional fields which are not in VAdto
}

class  VAdto{
//fields mapped to DB columns. generated by a ORM tool
}



I populate MoreVAdto.


Now  I would like to retrieve populated VAdto out of MoreVAdto  instance.

What is the way ?


(I need this because I have a DAO method which takes input VAdto instance to persist field data into DB .
0
Comment
Question by:cofactor
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Now  I would like to retrieve populated VAdto out of MoreVAdto  instance.

What is the way ?
You can really only do that by access to the members of MoreVAdto. If, because it's a DTO you can't do this, then you'd have to break into it with reflection, although resorting to this is a sure sign there's something fundamentally wrong with the design
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
Now  I would like to retrieve populated VAdto out of MoreVAdto  instance.

What is the way ?


(I need this because I have a DAO method which takes input VAdto instance to persist field data into DB .
If your DAO takes a VAdto as an argument, you can simply pass your MoreVAdto object to it directly.   Since it extends VAdto, it can be used anywhere where a VAdto object is expected.

If the above is not the case, then you need to explain further what you are doing because something from you have said, it is that simple.
0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
Comment Utility
If you pass instance of MoreVAdto to your function which has a argument of VAdto,  it should accept as MoreVAdto extends VAdto. Should not be a problem.

By crud way, You can typecast instance of MoreVAdto as,
VAdto result =  (VAdto) instanceOfMoreVAdto;

As you are using ORM so even though you have more # of fields inside your dto, it will persists only those attributes which you have given inside your mapping.
0
 

Author Comment

by:cofactor
Comment Utility
OK. I'll  give  these  solutions a try.

In the mean time , why I have made such a design ?  well,  you know in my GUI interface I have some form fields required to be persisted and some are not.

fields which are to be persisted are in VAdto. ( this is generated by ORM tool...mapped to DB columns) and fields which are NOT to be persisted are in MoreVAdto  .

finally  .... In GUI interface , I am presenting MoreVAdto  instance to the user.


so  ...when a user submits form , in action class I  get MoreVAdto   instance .....and I really need now VAdto instance to pass to DAO layer  to persis in DB......here is the issue now ....how do I retrieve the VAdto instance out of  MoreVAdto  instance .


Well, I'll  give your solutions a try .  Lets see how it works.

Thanks
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
@bonzermahesh,

I have noticed now a couple of posts from you where you copy the general idea from a previous post (just changing the wording slightly). Please stop doing that otherwise further action will be taken.

And in regards to your statement about "typecasting" the instance, the typecast is unnecessary, again because MoreVAdto extends VAdto, a simple assignment will work fine. Or, as is the solution that I am proposing, simply passing the MoreVAdto instance to the DAO that is expecting a VAdto instance will work perfectly fine.
0
 

Author Comment

by:cofactor
Comment Utility
I am facing an issue here.

form data is not populating to VAdto fields.

In GUI  Struts 2 form fields is not populating data into inherited fields.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
I think it's time to post some code
0
 

Author Comment

by:cofactor
Comment Utility
code is bit bigger ...I'm posting relevant part only.  



Fahamaliexpensesettlementgcdetails  is  the  class generated by ORM and has DB column mapped fields


******DTO****************
Fahamaliexpensesettlementgcdetails{ //generated 
protected int gcid; 
protected BigDecimal calculatedExpense;
.................
..................




public class FahamaliexpensesettlementgcdetailsExt extends Fahamaliexpensesettlementgcdetails{
	
	private String gcNo;
	private String gcDate;
	private String actualWeight;
	............................
	............................









********Action class*************

public class HamaliExpenseSettlementAction extends AbstractVoucherAction {

private List<FahamaliexpensesettlementgcdetailsExt> gcList;
.............
gcList=//populated from DAO layer.



*************JSP************

<s:iterator value="gcList" var="gcListObj" status="rowstatus">
		<tr id='gcTableRow_<s:property value="%{#gcListObj.gcId}"/>' class='gcTableRow'>
			<td class="CENTER_ALIGN">
				
				<s:hidden name="gcList[%{#rowstatus.index}].gcId" value="%{#gcListObj.gcId}" />
			</td>


			

Open in new window


Error in server console :

Error setting expression 'gcList[5].gcId' with value '[Ljava.lang.String;@190f83a': ognl.NoSuchPropertyException: com.dto.ext.FahamaliexpensesettlementgcdetailsExt.gcId
0
 

Author Comment

by:cofactor
Comment Utility
comments please
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
Comment Utility
Fahamaliexpensesettlementgcdetails{ //generated 
protected int gcid; 

Open in new window

'Generated' horribly by the looks

Error setting expression 'gcList[5].gcId' with value '[Ljava.lang.String;@190f83a': ognl.NoSuchPropertyException: com.dto.ext.FahamaliexpensesettlementgcdetailsExt.gcId

Open in new window


gcid != gcId
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 150 total points
Comment Utility
Also...

protected int gcid;

and

Error setting expression 'gcList[5].gcId' with value '[Ljava.lang.String;@190f83a': ognl.NoSuchPropertyException: com.dto.ext.FahamaliexpensesettlementgcdetailsExt.gcId



int != String
0
 

Author Comment

by:cofactor
Comment Utility
Thanks.  this works ,
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 74
topping1 challenge 7 46
word0 challenge 3 55
wordmultiple challenge 12 87
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now