Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

extends DTO

Posted on 2014-01-15
13
424 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
How our DevOps Teams Maximize Uptime

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

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 56
more than one jdk and one jre 1 50
Custom Java Application connects to database: Which one of the 20? 4 35
null output 3 35
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

809 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