Hibernate + Inheritance

Asked

Viewed 721 times

4

I’ve been trying for some time a solution to my problem. I’ve done a lot of research but nothing clearly explains what I need.

So I created an example application and am making it available from this link: Application

To run this application simply use the Netbeans IDE and create a Mysql DB with the following name: "testHeranca".

My question is this::

I have two entities, Participant and Issuer. Issuer class extends the participating class. For example:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "participante")
public class Participante implements Serializable{

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;
    @Column(name = "nome", nullable = false, length = 100)
    private String nome;
    @Column(name = "documento", nullable = false, length = 100)
    private String documento;
    ...
}



@Entity
@Table(name = "emitente")
@PrimaryKeyJoinColumn(name = "id")
public class Emitente extends Participante{

    @Column(name = "cargo", nullable = false, length = 100)
    private String cargo;
    ...
}

Suppose I have a participant already registered in my database, if I want to register a new issuer from this existing participant, how should I proceed? Does Hibernate do this for me? With the test I passed in the example this does not happen.

  • 2

    Hello Gilvan, your problem is quite interesting. From what I understand you seek to "promote" a superclass to one of its subtypes. I’m not sure if there is a solution for this in JPA (maybe there is a specific Hibernate solution). The work-arounds What I can think of are: 1) Insert manual in the table emitente reusing the id of participante with native sql or 2) Remove the participant and insert an issuer (which may be problematic due to Fks, mostly with auto-incrementable ids).

  • 1

    Exactly! At the moment I am creating a manual Index in the issuer table with Native Query. But I am thinking a lot about the possibility of abandoning the inheritance and using aggregation as suggested by one of the users who responded! Thank you for now, I will continue researching and studying which is the best solution. I count on the help of all! Thanks]

1 answer

3


Although I don’t know all the details of Hibernate and other JPA implementations, because each one has its particularities, I don’t think it is possible to include part of a legacy relationship.

Thinking that JPA works with a limited kind of polymorphism, this would be like changing the type of the object.

In that particular situation, a viable alternative would be to change the type of inheritance relationship to aggregation, i.e., where Emitente has an attribute of the type Participante. This way the tables do not need to be changed and you can include separate entities at any time.

Other alternatives exist, as already mentioned in Anthony Accioly’s comment:

  1. Include issuer via JDBC or Native Query and then read via JPA
  2. Remove the participant and re-include it as an issuer
  • 2

    Opa @utluiz, if you can amend a small observation (more semantic than anything, for someone who doesn’t know the JPA API) for item 1 the user doesn’t even need to use the JDBC API directly, just a entityManager.createNativeQuery("INSERT ...").executeUpdate();. Of course underneath the scenes JPA is using JDBC, but there’s no need to create a Statement or something like that.

  • 1

    @Anthonyaccioly Very good observation. It is always good to make these things clear.

  • I thank you for the answer, I believe I will follow the path of aggregation, inheritance works well as long as I do not come to use the parent entity! Thanks until then, I will continue my research and studies to define which will be the best option for my solution! Thank you very much!

  • 1

    ok, thank you for all your help. I have come to the conclusion that it will be best to use aggregation! Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.