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.
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 theid
ofparticipante
with native sql or 2) Remove the participant and insert an issuer (which may be problematic due to Fks, mostly with auto-incrementable ids).– Anthony Accioly
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]
– Gilvan André