如何在Spring Boot和Java持久化中映射弱实体

huangapple go评论70阅读模式
英文:

How to map a weak entity in Spring Boot and Java persistence

问题

已编辑(2020年9月30日):MySQL中的表显示如下:

如何在Spring Boot和Java持久化中映射弱实体


我有一个名为UbicacionPlanta(英文中的UbicateFloor)的实体,它是Departamento(英文中的Department)的弱实体。其结构如下:

@Entity(name = "ubicaciones_plantas")
@Table
public class UbicacionPlanta implements Serializable {

    @Id
    @JoinColumn(name = "iddepartamento", referencedColumnName = "iddepartamento", nullable = false)
    @OneToMany
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Departamento departamento;

    @Id
    @Column(name = "idubicacion")
    private Long idubicacion;

    @Column(name = "abreviacion_ubicacion")
    private String abreviacion_ubicacion;

    @Column(name = "descripcion_ubicacion")
    private String descripcion_ubicacion;

    public Departamento getDepartamento() {
        return departamento;
    }
    //Getters and Setters

}

IntelliJ IDEA显示如下错误:

如何在Spring Boot和Java持久化中映射弱实体

关于DTO,该类的实现如下:

public class MUbicacionPlanta {

    private String departamentoId;

    private Long idubicacion;

    private String abreviacion_ubicacion;

    private String descripcion_ubicacion;

    public MUbicacionPlanta(UbicacionPlanta ubicacionPlanta) {
        this.departamentoId = ubicacionPlanta.getDepartamento().getIddepartamentos();
        this.idubicacion = ubicacionPlanta.getIdubicacion();
        this.abreviacion_ubicacion = ubicacionPlanta.getAbreviacion_ubicacion();
        this.descripcion_ubicacion = ubicacionPlanta.getDescripcion_ubicacion();
    }
    //Getters and Setters
}

请问如何集成弱实体的ID?

英文:

Edited (9/30/20): The table in MySQL is displayed as it follows:

如何在Spring Boot和Java持久化中映射弱实体


I have a entity called UbicacionPlanta (UbicateFloor in English), weak entity of Departamento (Department in English). The structure is the following:

@Entity(name = "ubicaciones_plantas")
@Table
public class UbicacionPlanta implements Serializable {

    @Id
    @JoinColumn(name = "iddepartamento", referencedColumnName = "iddepartamento", nullable = false)
    @OneToMany
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Departamento departamento;

    @Id
    @Column(name = "idubicacion")
    private Long idubicacion;

    @Column(name = "abreviacion_ubicacion")
    private String abreviacion_ubicacion;

    @Column(name = "descripcion_ubicacion")
    private String descripcion_ubicacion;

    public Departamento getDepartamento() {
        return departamento;
    }
//Getters and Setters

}

Intellij IDEA give an error like this:

如何在Spring Boot和Java持久化中映射弱实体

Regarding DTO, the class is implemented like:

public class MUbicacionPlanta {

    private String departamentoId;

    private Long idubicacion;

    private String abreviacion_ubicacion;

    private String descripcion_ubicacion;

    public MUbicacionPlanta(UbicacionPlanta ubicacionPlanta) {
        this.departamentoId = ubicacionPlanta.getDepartamento().getIddepartamentos();
        this.idubicacion = ubicacionPlanta.getIdubicacion();
        this.abreviacion_ubicacion = ubicacionPlanta.getAbreviacion_ubicacion();
        this.descripcion_ubicacion = ubicacionPlanta.getDescripcion_ubicacion();
    }
    //Getters and Setters
}

How can I do in order to integrate the ids for weak entity, please?

答案1

得分: 0

由于这个问题的帮助:https://stackoverflow.com/questions/29760730/how-to-mapping-an-onetomany-relation-with-weak-entity-in-hibernate

"Departamento"(部门)的实现如下:

@Entity(name = "departamentos")
@Table
public class Departamento implements Serializable {

    @Id
    @Size(max = 50)
    @Column(name = "iddepartamentos")
    private String iddepartamentos;

    @Column(name = "nombre_departamento")
    private String nombre_departamento;

    @JoinColumn(name = "sede_departamento", referencedColumnName = "sede_departamento", nullable = false)
    @ManyToOne
    private Sede sede;

    @OneToMany(mappedBy="departamentos")
    private Set<UbicacionPlanta> ubicacionPlantas;
    
    // 构造方法,getter 和 setter

}

而 "UbicacionPlanta"(楼层位置)如下:

@Entity(name = "ubicaciones_plantas")
@Table
public class UbicacionPlanta implements Serializable {

    @JoinColumn(name = "iddepartamento", referencedColumnName = "iddepartamento", nullable = false)
    @ManyToOne
    private Departamento departamento;

    @Id
    @Column(name = "idubicacion")
    private Long idubicacion;

    @Column(name = "abreviacion_ubicacion")
    private String abreviacion_ubicacion;

    @Column(name = "descripcion_ubicacion")
    private String descripcion_ubicacion;

    // 构造方法,getter 和 setter
}
英文:

Thanks to this question: https://stackoverflow.com/questions/29760730/how-to-mapping-an-onetomany-relation-with-weak-entity-in-hibernate

Departamento (Department) is implemented like this:

@Entity(name = &quot;departamentos&quot;)
@Table
public class Departamento implements Serializable {

    @Id
    @Size(max = 50)
    @Column(name = &quot;iddepartamentos&quot;)
    private String iddepartamentos;

    @Column(name = &quot;nombre_departamento&quot;)
    private String nombre_departamento;

    @JoinColumn(name = &quot;sede_departamento&quot;, referencedColumnName = &quot;sede_departamento&quot;, nullable = false)
    @ManyToOne
    private Sede sede;

    @OneToMany(mappedBy=&quot;departamentos&quot;)
    private Set&lt;UbicacionPlanta&gt; ubicacionPlantas;
    
    //Constructor, getters and setters

}

And UbicacionPlanta (UbicateFloor), as it follows:

@Entity(name = &quot;ubicaciones_plantas&quot;)
@Table
public class UbicacionPlanta implements Serializable {


    @JoinColumn(name = &quot;iddepartamento&quot;, referencedColumnName = &quot;iddepartamento&quot;, nullable = false)
    @ManyToOne
    private Departamento departamento;

    @Id
    @Column(name = &quot;idubicacion&quot;)
    private Long idubicacion;

    @Column(name = &quot;abreviacion_ubicacion&quot;)
    private String abreviacion_ubicacion;

    @Column(name = &quot;descripcion_ubicacion&quot;)
    private String descripcion_ubicacion;

    //Constructor, getters and setters
}

huangapple
  • 本文由 发表于 2020年8月21日 17:52:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63520556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定