由于复合键,Spring JPA新插入未发生

qoefvg9y  于 2023-04-10  发布在  Spring
关注(0)|答案(1)|浏览(73)
@Entity
@Table(name = "USERS", schema = "UR")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@IdClass(UserIdEntity.class)
public class UserEntity {
@SequenceGenerator(
name = "sequence_name",
sequenceName = "SEQ_USER_ID",
allocationSize = 1,
schema = "AR")
@GeneratedValue(generator = "SEQ_USER_ID")
@Column(name = "USER_ID", length = 8,unique = true ,columnDefinition = "BIGINT" )
@NotNull
@Id
private BigInteger userId;
@Id
@Column(name = "USER_NUM")
@NotNull
private String userNum;
@Id
@Column(name = "USER_CD")
@NotNull
private String userCd;
@NotNull
@Column(name = "USER_ADDR")
private String userAddr;
}
@NoArgsConstructor
@AllArgsConstructor
@Data
public class UserIdEntity implements Serializable {
@NotNull
private BigInteger userId;
@Column(name = "USER_NUM" ,length = 5) @NotNull private String userNum;
@Column(name = "USER_CD",length = 5)  @NotNull private String userCd;
}
this is calling through service class
{userRepository.save(UserEntity)}

我试图插入用户记录,但它没有发生,而更新正在发生,没有任何关注通过做一些研究,它指出,与JPA复合键有一些挑战,而做插入,所以有人请让我知道我错过了什么。每次当我调试准备好的语句时,在执行插入时,它调用合并而不是保存bcoz JPA将其作为具有更新值的实体,而不是新插入即使userId是3个主键中的一个是null,而其余2个PK具有默认值。我也尝试通过临时变量将userId设置为新的,但没有运气。

your text
7z5jn7bk

7z5jn7bk1#

尝试从此处删除@NotNull注解

@NotNull
private BigInteger userId;

因为您已经为userId指定了@GeneratedValue(generator =“SEQ_USER_ID”),所以它将在运行时赋值。不需要指定@NotNull。

public class UserIdEntity implements Serializable {

private BigInteger userId;
@Column(name = "USER_NUM" ,length = 5) @NotNull private String userNum;
@Column(name = "USER_CD",length = 5)  @NotNull private String userCd;
}

另外,在服务类上使用@Transactional annotation。

相关问题