Solved

extends DTO

Posted on 2014-01-15
13
418 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
ID: 39782598
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
ID: 39784316
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
ID: 39784515
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:cofactor
ID: 39784528
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
ID: 39784545
@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
ID: 39785974
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 39786014
I think it's time to post some code
0
 

Author Comment

by:cofactor
ID: 39786142
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
ID: 39787456
comments please
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 39787920
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
ID: 39788099
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
ID: 39793224
Thanks.  this works ,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39793903
:)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 79
Weekend adv creator 3 36
eclipse with Angularjs2.0 3 34
type mismatch (Object[] to double[] 4 23
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

813 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

18 Experts available now in Live!

Get 1:1 Help Now