复合主键,由字符串和枚举类型变量组成。

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

Composite primary key for a String and an enum type variable

问题

以下是翻译好的内容:

这是我的交易实体类

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Transaction{

    @Id
    private int id;

    @NotNull
    @Enumerated(EnumType.STRING)
    private TransactionSourceEnum source;

    @NotNull
    @Column(unique = true)
    private String transactionId;

    private String switchingId;

    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
    private LocalDateTime requestDate;

    private String status;
}

这是存储库

public interface TransactionRepository extends JpaRepository<Transaction, Integer> {

    Transaction findByTransactionIdAndTransactionSource(String transactionId, TransactionSourceEnum transactionSource);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = "DELETE FROM transaction WHERE transaction_cre_sys_date <= (now() - interval 6 month)", nativeQuery = true)
    void deleteSixMonthOldTransactions();

    void deleteByTransactionCreSysDateBefore(LocalDateTime date);

    Boolean existsByTransactionId(String transactionId);
}

当我尝试用 @Id 注释 transactionSourceEnum 时,它会报错

org.springframework.beans.factory.BeanCreationException: 在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] 中定义的名为 'entityManagerFactory' 的 bean 创建失败: 调用 init 方法失败; 嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: default] 无法构建 Hibernate SessionFactory; 嵌套异常是 org.hibernate.MappingException: 复合标识类必须实现 Serializable: com.adl.et.telco.xlbss.paymentservice.domain.entities.Transaction

如何为 sourcetransactionId 定义一个复合主键,并从代码中删除 id 变量。

英文:

This is my transaction Entity class

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Transaction{

    @Id
    private int id;

    @NotNull
    @Enumerated(EnumType.STRING)
    private TransactionSourceEnum source;

    @NotNull
    @Column(unique = true)
    private String transactionId;

    private String switchingId;

    @JsonFormat(pattern = &quot;yyyy/MM/dd HH:mm:ss&quot;)
    private LocalDateTime requestDate;


    private String status;

}

This is the repository

public interface TransactionRepository extends JpaRepository&lt;Transaction, Integer&gt; {

    Transaction findByTransactionIdAndTransactionSource(String transactionId, TransactionSourceEnum transactionSource);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = &quot;DELETE FROM transaction WHERE transaction_cre_sys_date &lt;= (now() - interval 6 month)&quot;,nativeQuery = true)
    void deleteSixMonthOldTransactions();

    void deleteByTransactionCreSysDateBefore(LocalDateTime date);

    Boolean existsByTransactionId(String transactionId);

}

when I try to annotate transactionSourceEnum with @Id it says

org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;entityManagerFactory&#39; defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Composite-id class must implement Serializable: com.adl.et.telco.xlbss.paymentservice.domain.entities.Transaction

How do I define a composite primary key for source and transactionId and remove the id variable from the code

答案1

得分: 0

创建另一个类,您随后可以将其用作主键。要做到这一点,您必须使用@EmbeddedId

您的类:

public class Transaction{

    @EmbeddedId
    private TransactionPK primaryKey;

    ... 其他字段获取器和设置器
}

新类用作主键:

@Embeddable
public class TransactionPK implements Serializable{

    // 如果IDE要求,从您的IDE中创建自己的serialVersionUID
    private static final long serialVersionUID = 6202269445639364170L;

    @NotNull
    @Enumerated(EnumType.STRING)
    private TransactionSourceEnum source;

    @NotNull
    @Column(unique = true)
    private String transactionId;

    ... 获取器和设置器
}
英文:

Create one more class, which you can then use as your primary key. To do this you must use @EmbeddedId

Your class:

public class Transaction{

    @EmbeddedId
    private TransactionPK primaryKey;

    ... other fields, getters and setters

New class used as the primary key:

@Embeddable
public class TransactionPK implements Serializable{

    // create your own serialVersionUID from your IDE if it asks
    private static final long serialVersionUID = 6202269445639364170L;

    @NotNull
    @Enumerated(EnumType.STRING)
    private TransactionSourceEnum source;

    @NotNull
    @Column(unique = true)
    private String transactionId;

    ...getters and setters

huangapple
  • 本文由 发表于 2020年7月23日 20:05:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63053856.html
匿名

发表评论

匿名网友

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

确定