如何使用SpringTVC使用SingleTableInheritance获取子类属性

时间:2017-07-09 15:14:23

标签: java spring hibernate spring-mvc inheritance

我正在使用Spring,Hibernate和SpringMVC开发Web应用程序, 我正面临使用SingleTable继承策略从子类表中检索值的问题,这里是我的实体

Client.java(超级)

    @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() {

    }
    }

在我的实现中,我正在使用此方法来获取具有其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);
    }

当我运行此代码时,我只选择了超类客户端的属性。

我想要做的是根据客户的类型或客户ID,为客户提供其子类,无论是 Societe 还是 Particulier

请帮助

2 个答案:

答案 0 :(得分:2)

由于您在查询之前并不知道客户端的类型并且只知道它的ID,因此您需要在检索记录后检查类型和强制转换;

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

根据您的使用情况,将客户端查询的结果映射到ClientDescriptor对象可能很有用,该对象包含所有客户端类型的所有字段,并返回空值或空白。这意味着您无需在任何地方继续检查客户端类型;

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.
}

答案 1 :(得分:1)

您可以修改getClientByID方法以接受另一个参数,该参数将说明您想要检索和返回的实体类型:

resultDict = {}  # = dict3
for key in dict1:
    multiplierList = dict2[key]
    sourceList = dict1[key]
    resultDict[key] = []
    for sourceElement in sourceList:
        if sourceElement == 0:        #in your question you described that in dict3 you would only have one 0 element.
            resultDict[key].append(0) #without these two lines you would have as many 0's as there are multipliers (in the example there would be two 0's)            
        for multiplier in multiplierList:
            resultDict[key].append(sourceElement * multiplier)

你可以用以下方式使用这个dao:

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);
    }
}