多对一关系中的重复列映射(Spring JPA)

问题描述 投票:0回答:1

当我运行我的 springboot 项目时,出现错误:

Column 'transactionNumber' is duplicated in mapping for entity 'com.myproject.Entities.IndividualItemsTransactionEntity' (use '@Column(insertable=false, updatable=false)' when mapping multiple properties to the same column)

我当前的项目有 2 个表。一张是交易表(每笔交易只有一张)

@Entity
@Table(name = "transactions")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
public class TransactionsEntity {

    @Id
    @NonNull
    @Column(name = "transactionNumber", columnDefinition = "varchar(12)", length = 12, insertable=false, updatable=false)
    private String transactionNumber;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "transactionNumber", nullable = false)
        List<IndividualItemsTransactionEntity> individualItemsTransactionEntities;

//more redacted stuff
    }

每笔交易对于交易中购买的每件商品都有许多个 individualItemsTransactionEntity

@Entity
@Table(name = "individual_items_transaction")
@Getter
@Setter
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class IndividualItemsTransactionEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "individualItemTransactionID", columnDefinition = "bigint", insertable=false, updatable=false)
    private Long individualItemTransactionID;

    //We are using hibernate nullable = false to allow transaction entity ID to generate first before populating
    //all transaction item entities with non nullable
    @Column(name = "transactionNumber", columnDefinition = "varchar(12)", length = 12, nullable = false)
    private String transactionNumber; // <- this basically is referring back to the other transaction table

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "transactionNumber", nullable = false)
    TransactionsEntity transactionsEntity;
}

我基本上想要 SQL 等价物:

FROM transactions JOIN individual_items_transaction ON transactions.transactionNumber = individual_items_transaction.transactionNumber

谢谢!

java mysql spring-boot jpa spring-data-jpa
1个回答
0
投票

实体应如下所示:

@Entity
@Table(name = "individual_items_transaction")
@Getter
@Setter
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class IndividualItemsTransactionEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "individualItemTransactionID", columnDefinition = "bigint", insertable=false, updatable=false)
    private Long individualItemTransactionID

    //We are using hibernate nullable = false to allow transaction entity ID to generate first before populating
    //all transaction item entities with non nullable
    @Column(name = "transactionNumber", columnDefinition = "varchar(12)", length = 12, nullable = 

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "transactionNumber", nullable = false)
    TransactionsEntity transactionsEntity;
}


@Entity
@Table(name = "transactions")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
public class TransactionsEntity {

    @Id
    @NonNull
    @Column(name = "transactionNumber", columnDefinition = "varchar(12)", length = 12, insertable=false, updatable=false)
    private String transactionNumber;

    @OneToMany(fetch = FetchType.LAZY)
    @mappedBy(“transactionEntity”)
        List<IndividualItemsTransactionEntity> individualItemsTransactionEntities;

//more redacted stuff
    }
© www.soinside.com 2019 - 2024. All rights reserved.