• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Mapping beans

 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am trying to set all the data I have in an object st66 in another object st96 . But there is a cast error


and in the other bean I have



When I try to make this I get the next error



Any idea?

Thanks

 
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ApplicantDetails and ApplicantBag are two different types so you can not use "details" instead of a 'bag".
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but there any way to map it?
 
Ivan Jozsef Balazs
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally speaking no.

Let us image one type stands for, say, an XML document, the other one for a thread of the OS. How could one be used instead of the other?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes but there any way to map it?


It really depends on what these two classes represent.
If one is a sub class of the other or they have a common super class and the variable type is of the common type then you can make the assignment.
If there is no common super type and the classes represent totally disparate things (eg one is a colour and then other is an animal) then it makes no sense to map one to the other.
However, If there is no common super type but they contain common data types (eg one is a List of String objects and the other is a Set of String objects) then yes you could write code to extract the data from one type and put it into the other type.
 
Marshal
Posts: 5222
323
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Apache Commons library have a Bean Copy type utility feature you could use.

http://commons.apache.org/proper/commons-beanutils//api/org/apache/commons/beanutils/BeanUtils.html#copyProperties(java.lang.Object, java.lang.Object)

For example, given these two classes:

You could use the Apache Commons library to copy the value of beanName from one object to the other. Notice that apart from their similarities these two classes are completely unrelated.

A point to note is that this is apparently done through Reflection. I am not sure whether this is through obtaining the method names and matching up the setX with a getX or whether it's done through hoking directly into the class members. Worth being aware of this should your getX method actually refer to class member Y.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

but the content of both types is exactly the same

just with different name

Regards,

 
Tim Cooke
Marshal
Posts: 5222
323
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The content of both Types may be the same, but they are not the same Java Type.

On the line st96.setApplicantBag(st66.getApplicantDetails()); you are saying that st96 is expecting an ApplicantBag but is being given an ApplicantDetails. The type mismatch is the error.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic