-->
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.  [ 2 posts ] 
Author Message
 Post subject: Using Interfaces as base persistent class
PostPosted: Fri Mar 31, 2006 7:02 am 
Newbie

Joined: Fri Mar 31, 2006 6:37 am
Posts: 1
Hi.
During my investigation of Nhibernate found one interesting line in documentation:
"It is perfectly acceptable for the named persistent class to be an interface. You would then declare implementing classes of that interface using the <subclass> element. You may persist any inner class. You should specify the class name using the standard form ie. Eg.Foo+Bar."

I tried it several times but nothing came out of it. In my solution i have several classes that override each other. They don't add any functionality, just override some of the properties and methods (i mean that its no need to use discriminator value since actual data is always in one table). Its would be nice to describe all them in configuration file as subclass of base class or interface as described in above topic and then persist needed class. Can anybody give me description or, even better, example of it.

Thank you in advance


Top
 Profile  
 
 Post subject: Re: Using Interfaces as base persistent class
PostPosted: Sat Apr 01, 2006 6:04 am 
Regular
Regular

Joined: Sat May 29, 2004 2:16 pm
Posts: 81
Hokmun wrote:
Can anybody give me description or, even better, example of it.

Thank you in advance

here's how i use it in my app:
Code:


package dominio;



import java.math.BigDecimal;

import java.util.Date;



public abstract class Contrato {
   
    protected Long id, idDoAnterior;

    private String nomeActoMedico, tipoContrato;
   
    private String obs = "";
   
    private Date data; // estabelece validade a partir desta data
   
    private Date fimDeVigencia;
   
    private BigDecimal valorContratado; // valor global
   
    private boolean emVigencia;
   
   
   
// getters and setters
   
}


Code:
package dominio;



import java.math.BigDecimal;

import bd.InterfacePersistencia;




public class ContratoConvencao extends Contrato {

    private BigDecimal pagoPelaConvencao; // comparticipaĆ§Ć£o
   
    private BigDecimal pagoPeloUtente; // recebido

    private Entidade entidade;

    private Convencao convencao;

    private Banco banco;
   
    private InterfacePersistencia facade;
   
    private ActoMedico actoMedico; // one to one
   


    private ContratoConvencao() {}



    public ContratoConvencao(String nomeActoMedico, String tipoContrato,
                   
                    Entidade entidade, Convencao convencao) {
       
        setNomeActoMedico(nomeActoMedico);
       
        setTipoContrato(tipoContrato);
       
        this.entidade = entidade;
       
        this.convencao = convencao;
       
    }
   
   
   
    public void add(InterfacePersistencia facade) {
       
        this.facade = facade;

    }
   
   

   
    public void actualiza() {
       
        facade.actualiza();
       
    }



//getters setters 
   
   
   
    @Override
   
    public BigDecimal getValorContratado() {
       
        return getPagoPeloUtente().add(getPagoPelaConvencao());
       
    }
   
   
   
    @Override
   
    public boolean equals(Object object) {       
       
//...

    }
   
   
   
    @Override
   
    public int hashCode() {

//...

    }
   
   
   
    @Override
   
    public String toString() {

//...

    }


}

Code:
package dominio;



public class ContratoTecnica extends Contrato {

    private Tecnica tecnica;
   
   
   
    private ContratoTecnica() {}
   
   
   
    public ContratoTecnica(String nomeActoMedico, String tipoContrato,
                   
                    Tecnica tecnica) {

        setNomeActoMedico(nomeActoMedico);

        setTipoContrato(tipoContrato);

        this.tecnica = tecnica;

    }
   
   
   
    @Override
   
    public boolean equals(Object object) {

//...

    }



    @Override
   
    public int hashCode() {

//...

    }



    @Override
   
    public String toString() {

//...

    }

}

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
   package = "dominio">
   
    <class
       name = "Contrato"
       table = "contrato"
       discriminator-value = "contrato">

      <id
         name = "id"
         column = "contrato_id">
         <generator
            class = "native" />
      </id>

      <discriminator
         column = "heranca_contrato"
         type = "string" />
           
        <property
           name = "nomeActoMedico"/>
           
        <property
           name = "tipoContrato"/>
           
        <property
           name = "obs"/>
       
        <property
            name = "data"
            type = "date" />
       
        <property
            name = "fimDeVigencia"
            type = "date" />          
           
        <property
           name = "valorContratado">
           <column
              name = "valorContratado"
              sql-type = "decimal(10,2)" />
      </property>

       <property
           name = "emVigencia"
           type = "boolean" />
           
       <property
           name = "idDoAnterior"
           type = "long" />

      <subclass
         name = "ContratoParticular"
         discriminator-value = "particular">
            
         <many-to-one
            name = "entidade"
            column = "entidade_id"
            class = "Entidade" />
      </subclass>   
         
      <subclass
         name = "ContratoTecnica"
         discriminator-value = "tecnica">
            
         <many-to-one
            name = "tecnica"
            column = "tecnica_id"
            class = "Tecnica" />
      </subclass>   
      
      <subclass
         name = "ContratoConvencao"
         discriminator-value = "convencao">
              
           <property
              name = "pagoPelaConvencao">
              <column
                 name = "pagoPelaConvencao"
                 sql-type = "decimal(10,2)" />
         </property>
         
           <property
              name = "pagoPeloUtente">
              <column
                 name = "pagoPeloUtente"
                 sql-type = "decimal(10,2)" />
         </property>
              
          <many-to-one
            name = "convencao"
            column = "convencao_id"
            class = "Convencao" />
            
         <many-to-one
            name = "entidade"
            column = "entidade_id"
            class = "Entidade" />   
            
          <one-to-one
            name = "actoMedico"
            class = "ActoMedico" />
                     
         <component
            name = "banco"
            class = "Banco">
            <property
               name = "nome"
               type = "string"
               column = "nome" />
            <property
               name = "nib"
               type = "string"
               column = "nib" />
         </component>
      
      </subclass>   
      
    </class>

</hibernate-mapping>


Hope this helps


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