-->
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.  [ 3 posts ] 
Author Message
 Post subject: inheritance question
PostPosted: Tue Aug 14, 2007 4:18 pm 
Newbie

Joined: Thu Jan 18, 2007 4:45 pm
Posts: 10
In my project i'm using a table per class Mapping. Everything is working fine, but I have a page tha shows all itens in a single table, and the user identifies the object type by one flag.
Like.....

for example, we have a table with N payments, Card, cash etc ...

all this itens card, cash extends the payment class and in that page the user can see all payments for that day, with a CS (For cash), CC (Credit Card)... etc.

The problem is

I use a discriminator collumn to identify the class of my instance, but I cannot use that collumn for nothing else.

How can I know what kind of class my instance is when I mounting the user's page from a load from my payment table since the discriminator collumn is inaccessible???? so i can show the user the kinds of payments in that day???

do I have to select all Card then all Cash then all bla-bla class-by-class?

like...
Criteria crit session.createCriteria(CreditCard.class);
Criteria crit session.createCriteria(Cash.class);

etc...

then flush the lists in my page

or there's a better way??

any aid is welcome
Thanks in advance!!!

_________________
Dan Mopont


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 8:36 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Because this functionality is closely tied to the user interface you should probably use a visitor for this (rather than an overridden method in your class hierarchy).
Code:

package test.visitor;
public abstract class Payment {
   interface Visitor {
      Object visit(CardPayment payment);
      Object visit(CashPayment payment);
   }
   public abstract Object accept(Visitor visitor);
}


package test.visitor;
public class CardPayment extends Payment {
   @Override
   public Object accept(Visitor visitor) {
      return visitor.visit(this);
   }
}


package test.visitor;
public class CashPayment extends Payment {
   @Override
   public Object accept(Visitor visitor) {
      return visitor.visit(this);
   }
}


package test.visitor;
public class PaymentDisplayStringVisitor implements Payment.Visitor {
   public Object visit(CardPayment payment) {
      return "CC";
   }
   public Object visit(CashPayment payment) {
      return "CS";
   }
}


package test.visitor;
import java.util.ArrayList;
import java.util.List;
public class Test {

   public static void main(String[] args) {
      List<Payment> payments = new ArrayList<Payment>();
      payments.add(new CashPayment());
      payments.add(new CardPayment());
      
      PaymentDisplayStringVisitor visitor = new PaymentDisplayStringVisitor();
      for (Payment payment : payments) {
         System.out.println(payment.accept(visitor));
      }
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 9:54 am 
Newbie

Joined: Thu Jan 18, 2007 4:45 pm
Posts: 10
Thanks, that was a very elucidating aproach. A friend suggested me to use another table with the payments types (only id and description, like CC, Credit Card,CS,Cash...) and make the discriminator column in the payment table a many-to-one with this table. The bad thing is to use a join...
Your solution more elegant. I'll give it a try

thanks again

_________________
Dan Mopont


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.