简体   繁体   中英

How to get subclasses attributes using SingleTableInheritance with SpringMVC

i am working on a web application using Spring, Hibernate and SpringMVC, i am facing a problem with retreiving values from a subclass table using SingleTable inheritance strategy, here are my entities

Client.java (Super class)

    @Entity
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name = "typeClient", discriminatorType = DiscriminatorType.STRING)
    public class Client implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int idClient;

        private String matricule;
        private String statut;

        private String secteurDactivite;
        private String nomClient;


        private String emailClient;

        private String numTelephone;

        private String adresse;
//constructor
//getter & setters
     }

Societe.java (subClass1)

@Entity
@DiscriminatorValue("Societe")
public class Societe extends Client implements Serializable{


    private String nomResponsable;


    private String emailResponsable;


    private String telResponsable;


    private String nomSuperieur;


    private String emailSuperieur;


    private String telSuperieur;

    private String commentaire;

    //constructeur sans parametre
    public Societe() {
        }
    }

Particulier.java (subclass2)

@Entity
@DiscriminatorValue("Particulier")
public class Particulier extends Client implements Serializable {

    private String cin;


    //constructeur sans parametres
    public Particulier() {

    }
    }

in my implementation i am using this methode to get a particular client with his ID

ClientDaoImpl.java

public class ClientDaoImpl implements ClientDao {
    @PersistenceContext
    private EntityManager em;

    @Override
    public Client getClientByID(int id_client) {
        return em.find(Client.class, id_client);
    }

When i ran this code i only selected the attributes of the superClass Client .

what i am trying to do is to get a client with its subclass whether it's a Societe or Particulier based on its type or clientID.

Please Help

As you don't know the type of client before querying and only it's ID, you will need to inspect the type and cast after you retrieve the record;

Client client1 = clientDao.getClientById(clientID);
if (client1 instanceof Societe) {
    ((Societe) client1).getCommentaire();
}

Depending on your use case, it may be useful to map the result of the client query to a ClientDescriptor object which contains all the fields for all client types and returns either nulls or blanks. This means you don't have to keep checking for client type everywhere;

public class ClientDTO {
    //client fields
    private String nomResponsable = "";
    ....
    //subclass 1 fields.... initialize to empty
    //subclass 2 fields .... initialize to empty

    public ClientDTO (Client client) {
        // set fields for client entity
    }

    public ClientDTO (Societe societe) {
        this (societe);
        // set societe fields.
    }
    // other constructors.
}

You can modify your getClientByID method to accept an additional argument which will say what type of entity your want to retrieve and get back:

public class ClientDaoImpl implements ClientDao {
    @PersistenceContext
    private EntityManager em;

    public <T extends Client> T getByID(int id_client, Class<T> klass) {
        return em.find(klass, id_client);
    }
}

And you can use this dao in the following manner:

Societe societe = clientDao.getByID(42, Societe.class);
Particulier particulier = clientDao.getByID(43, Particulier.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM