• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException Problem

 
Mark O' Sullivan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a business interface for data access: For example

In RMI, I have the same interface, say

Then I try to connect client across the network when server running and I try and cast and use the data layer interface

I get following error:
"java.lang.ClassCastException: Proxy cannot be cast to suncertify.db.HotelReservationDB"
Any reason why I can't cast, even though interfaces are same apart from remote exception?
 
Johnny Barbosa
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

Look, HotelReservationDB Interface is totally different of HotelReservationDBRemote interface.
You cannot to cast like this: "(HotelReservationDB)hotelReservation". To do it, is the same you want to transform "orange" in "apple". You can't to do this. Think about it. What do you really want to do?
 
Mark O' Sullivan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers, thanks. Sorry, if I give impression I'm clueless, I was hoping to have cleaner code when calling the direct or network client connection by having the 1 interface reference. Is the only approach to this referencing to use a separate reference for each of the direct client and network client connection? Thanks very much for kind replies.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Mark!

Cheers, thanks. Sorry, if I give impression I'm clueless


Champion, we are all here to discuss and help each other. So... no need to worry, partner! We are glad to help and discuss

Now, please take a look at this sample code provided by my good buddy Roel to see if it helps.
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto Perillo wrote:Now, please take a look at this sample code provided by my good buddy Roel to see if it helps.

That's what I have been dreaming about for many weeks and months: being referenced by the Great Roberto
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hahahaha

That's my good friend Roel!
 
Mark O' Sullivan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers, thanks for all your help. Just wondering 1 final point, in my set-up I have, package-wise say:
BusinessService and LocalBusinessServiceImpl both in the db package.
BusinessRemote and NetworkBusinessServiceImpl in the remote package.
How can I justify throwing the remoteException for the methods in the BusinessService interface while these classes are supposed to be accessed locally? I hope one understands that I'm asking.
Or would I be better packaging the application differently to support throwing the RemoteException from the methods?
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, for everything related to the business/services layer, I'd create a package called business or services.

Now, about the exception, I see your point... but if a method is defined in an interface and has the RemoteException in its throws clause, it means that it can throw it. The implementation doesn't need to include it in its throws clause if it won't be thrown.
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

It is also shown in the provided: the method in the LocalBusinessServiceImpl is not throwing a RemoteException.

Regarding your package structure. I defined 3 packages: service (contains my business interface + service-exceptions), direct (for the local business implementations) and rmi (contains all classes to do the RMI-stuff).

Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic