简体   繁体   中英

ManyToOne & OneToMany & ManyToOne mapping JPA / Hibernate

I HAVE PROBLEMS WITH JPA I HAVE BEEN LEARNING FOR A LITTLE TIME AND I WANT TO MAKE RELATIONSHIPS OF 3 TABLES MY DATABASE RELATIONSHIP ENTITY IS (MANY DOCUMENTS HAVE ONE ENTITY) && (ONE ENTITY HAS MANY CONTRIBUTORS) I DON'T KNOW IF IT WILL BE CORRECT WHEN MAPPING THE CODES HELP enter image description here .....................................

@Entity
@Table(name = "tb_entidad")
public class Entidad {

    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    @Column(length = 11)
    private int id;

    @Column(name = "nro_documento", length = 25)
    private int nroDocumento;

    @Column(name = "razon_social", length = 100)
    private String razonSocial;

    @Column(name = "nombre_comercial", length = 100)
    private String nombreComercial;

    @Column(length = 250)
    private String direccion;

    @Column(length = 50)
    private String telefono;

    private Boolean estado;


      @OneToMany(fetch = FetchType.LAZY, mappedBy = "entidadc", cascade =
      CascadeType.ALL) private List<TipoContribuyente> contribuyente;

    @OneToMany(mappedBy = "entidades",fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<TipoDocumento> documentos;
    
    }

==

@Entity
@Table(name = "tb_tipo_contribuyente")
public class TipoContribuyente {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_tipo_contribuyente", length = 11)
    private int id;
    @Column(length = 50)
    private String nombre;

    private Boolean estado;

    /
      @ManyToOne(fetch = FetchType.LAZY)
      
      @JoinColumn(name = "id_tipo_contribuyente") 
    private Entidad entidadc;
     

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public Boolean getEstado() {
        return estado;
    }

    public void setEstado(Boolean estado) {
        this.estado = estado;
    }

@Entity
@Table(name = "tb_tipo_documento")
public class TipoDocumento {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(length = 11)
    private int id;

    @Column(length = 20)
    private String codigo;

    @Column(length = 100)
    private String nombre;

    @Column(length = 200)
    private String descripcion;

    private Boolean estado;

    

    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tipodocumento_id")
    private Entidad entidades;

}

You should add referencedColumnName in TipoContribuyente entity and TipoDocumento entity. It is the name of Entidad entity primary key. let's change as follows and don't keep space with annotations.

----- TipoContribuyente.class --------

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "id_tipo_contribuyente", referencedColumnName = "id") 
  private Entidad entidadc;

----- TipoDocumento.class ----------

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "tipodocumento_id", referencedColumnName = "id")
  private Entidad entidades;

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