【问题标题】:Unidirectional OneToMany JPA only shows me the first related object "n" times单向 OneToMany JPA 仅向我显示第一个相关对象“n”次
【发布时间】:2022-01-23 20:49:24
【问题描述】:

我有下表,其中有很多文章。我想列出所有 'ventasc' 记录以及与 pk 具有相同 'iddocumento' 的所有 kardArt 记录。

升级: 我将 Set 更改为 List 但现在它显示了所有具有相同值的孩子(第一个)

@Entity
@Table(name = "ventasc")
public class Ventasc {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private @Getter @Setter Integer iddocumento;
    private @Getter @Setter String doc1;  
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Temporal(TemporalType.TIMESTAMP)
    private @Getter @Setter Date fecha1;
    private @Getter @Setter Double t_neto;
    @OneToMany
    @JoinColumn(name = "iddocumento")
    private @Getter @Setter List<KardArt> kardArts;
    
}

此表通过“iddocumento”链接到销售额。我认为这是问题所在,它可以作为 pk 和 fk 使用。我无法编辑数据库。

@Entity
@Table(name = "kard_art")
public class KardArt {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private @Getter @Setter Integer iddocumento;

    private @Getter @Setter Double movimiento;
    private @Getter @Setter Double subtotal;
}

注释的值是应该带的。

{
    "iddocumento": -2147483386,
    "doc1": "PED",
    "fecha1": "2021-10-05 05:00:00",
    "tneto": 5269.96,
    "kardArts": [
      {
        "iddocumento": -2147483386,
        "movimiento": 100,
        "subtotal": 1530
      },
      {
        "iddocumento": -2147483386,
        "movimiento": 100, //100.3
        "subtotal": 1530 //1534.59
      },
      {
        "iddocumento": -2147483386,
        "movimiento": 100, //85.6
        "subtotal": 1530 //1309.68
      },
      {
        "iddocumento": -2147483386,
        "movimiento": 100, //3
        "subtotal": 1530 //45.90
      },
      {
        "iddocumento": -2147483386,
        "movimiento": 100, //3
        "subtotal": 1530 //45.90
      }
    ]
},

【问题讨论】:

    标签: spring-boot hibernate jpa


    【解决方案1】:

    mappedBy 用于双向依赖。

    你必须使用@JoinColumn

    @JoinColumn(name = "iddocumento")
    @OneToMany
    private @Getter @Setter Set<KardArt> kardArts;
    

    【讨论】:

    • 同样的输出,它应该给我带来 46 个 'KardArts' 但它只带来了 1 个
    • 您确定数据库中的数据是正确的?你实现了equals和hashcode吗?
    • 是的,第一条记录重复children次数。所有小计的总和必须等于 tneto。我可以用 lombok 实现 hashcode 和 equals 吗?
    • 你说的“第一条记录是重复孩子的次数”是什么意思?
    • {"iddocumento": -2147483386, "movimiento": 100, "subtotal": 1530} 假设 ventasc 有 10 个 KardArt 孩子。第一条记录会重复10次
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 2012-08-15
    • 2016-02-27
    • 1970-01-01
    相关资源
    最近更新 更多