These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How to copy a POJO in a DTO
PostPosted: Thu Dec 30, 2004 6:18 am 
Regular
Regular

Joined: Sat May 15, 2004 4:27 am
Posts: 79
Hi all,

who knows how can we do for copying a POJO in a DTO. I am working with
web services too. In my DTO i can't use java object like Set. So i am using
arrays instead of Set.

public class MyPojo {
//all private variables here
Set foo = new HashSet();

//all the puiblic get/set methods here
}

public class MyDTO {
//all private variables here
Object foo[];

//all the puiblic get/set methods here
}

(b) is a MyPojo object and (m) is a MyDTO object

that piece of code doesn't work BeanUtils.copyProperties(b , m); It returns me some
exceptions

java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:1789)
at org.apache.commons.beanutils.BeanUtils.copyProperty(BeanUtils.java:450)
at org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:264)
at test.Test.main(Test.java:29)

Who knows how to solve that issue ?? Thanks a lot

----------
Sébastien


Top
 Profile  
 
 Post subject: Re: How to copy a POJO in a DTO
PostPosted: Thu Dec 30, 2004 6:49 am 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
smayemba wrote:
public class MyPojo {
//all private variables here
Set foo = new HashSet();

//all the puiblic get/set methods here
}

public class MyDTO {
//all private variables here
Object foo[];

//all the puiblic get/set methods here
}


BeanUtils doesn't know how to copy a set into an array, so your code needs to do that. One simple way would be to add a getter for 'foo' that returns an array, then tell BeanUtils to use that getter instead of the getter that returns the Set (perhaps rename the getter in MyPojo that returns the set).

But... if you have lots of Pojos and TOs, then you might want to make a factory that knows how to convert Pojos into TOs.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 30, 2004 8:42 am 
Regular
Regular

Joined: Sat May 15, 2004 4:27 am
Posts: 79
I have 31 tables and average 42 POJOs. U are talking about a Factory. Your
idea afraid me if i have to rewrite Jakarta BeansUtil API! Do u know any
other API/Framework/Librairie which can performs what i want ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 30, 2004 8:58 am 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
Customizing open source to do what you want is nothing to be afraid of. You can make a version of BeanUtil that works for you, or better yet, make a factory that uses BeanUtils to do the things it does and has code that handles the special cases that you want. I image that you might have a few other special cases in all those classes, not just turning sets into arrays.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.