How do I set up a column to be composed in Mysql with Hibernate (Java)?

Asked

Viewed 98 times

0

I’m doing a CRUD and in creating my model I’ve reached a point where I can’t get out. I have a table "Plan" and each plan can have several "Modalities", as well as several "Extras", but I do not know how to create these two compound fields using Hibernate.

I was creating the two fields as a "Modality" and "Extra" Object, but I noticed that only 1 Modality and 1 Extra would be registered in the table "Plan", and if the Plan were more than one modality, the second would not be in the table "Plan".

Segue um rascunho da estrutura

public class Plano extends GenericDomain{

@Column(length = 50, nullable = false)
private String nome;

@Column(nullable = false)
private Short quanModalidade;

@Column(nullable = false)
private Short quanExtra;

@Column(nullable = false)
private String duracao; //Mensal,Semestre ou Anual

@Column(nullable = false, precision = 6, scale = 2)
private BigDecimal valor;

@Column(nullable = false)
private Short adquirentes;

@JoinColumn(nullable = false)
@ManyToOne
private Extra extra;

@JoinColumn(nullable = false)
@ManyToOne
private Modalidade modalidade;

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public Short getQuanModalidade() {
    return quanModalidade;
}

public void setQuanModalidade(Short quanModalidade) {
    this.quanModalidade = quanModalidade;
}

public Short getQuanExtra() {
    return quanExtra;
}

public void setQuanExtra(Short quanExtra) {
    this.quanExtra = quanExtra;
}

public String getDuracao() {
    return duracao;
}

public void setDuracao(String duracao) {
    this.duracao = duracao;
}

public BigDecimal getValor() {
    return valor;
}

public void setValor(BigDecimal valor) {
    this.valor = valor;
}

public Short getAdquirentes() {
    return adquirentes;
}

public void setAdquirentes(Short adquirentes) {
    this.adquirentes = adquirentes;
}

public Extra getExtra() {
    return extra;
}

public void setExtra(Extra extra) {
    this.extra = extra;
}

public Modalidade getModalidade() {
    return modalidade;
}

public void setModalidade(Modalidade modalidade) {
    this.modalidade = modalidade;
 }
}

1 answer

0


I believe that its mapping is wrong, if a "plan" can have several "modalities" and "extras" should use the mapping @oneToMany in the "plane" where the mode and the extra would store the id of the reference plan in the simple example sql table

inserir a descrição da imagem aqui

If it is @Manytomany, that is, The same "plan" for various "modality" and the same "modality" for various "plans" must be created an intermediate table to record this relation.

So there has to be an intermediate table for each relationship (Plano_modality) and (Plano_extra) simple example:

inserir a descrição da imagem aqui

Note: Tips from @Manytomany using unidirectional or bidirectional

As an example for @Manytomany following the idea of this Link: https://www.baeldung.com/hibernate-many-to-many Would look like this:

@Entity
@Table(name = "Plano")
public class Plano { 
    // ...

    @ManyToMany(cascade = { CascadeType.ALL })
    @JoinTable(
        name = "Plano_Modalidade", 
        joinColumns = { @JoinColumn(name = "id_plano") }, 
        inverseJoinColumns = { @JoinColumn(name = "id_modalidade") }
    )
    List<Modalidade> modalidades;

    // getters/setters
}

@Entity
@Table(name = "Modalidade")
public class Modalidade {    
    // ...  

    @ManyToMany(mappedBy = "modalidades")
    private List<Plano> Planos;

    // getters/setters   
}
  • I’m using the 2 option you suggested, in the same table I registered the relationship between "Plan","Modality" and "Extra", so I have to insert @Manytomany in this intermediate table, right?

  • This intermediate table will exist only in the database. In the code the "Plan" will have @Manytomany for "Modality" and another for "Extra" And the "Modality" and "Extra", will have a "Manytomany for the "Plan". This Mapping can have some variations being Unidirectional (Only one class knows the mapping) and Bidirectional (The two classes know the mapping). I suggest you take a look at this link (https://www.devmedia.com.br/manytomany-hibernate-variacoes-unidirectionl-bidirectionl/29535) have some examples of how to apply both cases

  • I updated the answer with more information on using @Manytomany

Browser other questions tagged

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