Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

extends DTO

Posted on 2014-01-15
13
Medium Priority
?
501 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
A solution for Fortify Path Manipulation.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

972 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