英文:
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
如何为 source
和 transactionId
定义一个复合主键,并从代码中删除 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 = "yyyy/MM/dd HH:mm:ss")
private LocalDateTime requestDate;
private String status;
}
This is the repository
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);
}
when I try to annotate transactionSourceEnum with @Id
it says
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论