?
Solved

extends DTO

Posted on 2014-01-15
13
Medium Priority
?
481 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
[X]
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
  • 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 36

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 36

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 1000 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 36

Assisted Solution

by:mccarl
mccarl earned 600 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:
Suggested Courses

719 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