CrudRepository Query更新正在插入新行,而不是更新现有行



我试图更新表中的一行,但它创建了一个新行,而不是更新现有行。我创建了下表

create table if not exists credential (
credential_id INT NOT NULL AUTO_INCREMENT,
system_id varchar(50),
domain varchar(50),
custom_key varchar(50),
user_name varchar(50),
credential varchar(500),
PRIMARY KEY (credential_id)
);

我有以下实体

@Entity
@Table(name="credential")
public class CredentialEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long credentialId;
String systemId;
String domain;
String customKey;
String userName;
String credential;

public CredentialEntity() {
super();
// TODO Auto-generated constructor stub
}
public CredentialEntity(String systemId, String domain, String customKey, String userName,
String credential) {
super();
this.systemId = systemId;
this.domain = domain;
this.customKey = customKey;
this.userName = userName;
this.credential = credential;
}
public String getSystemId() {
return systemId;
}
public String getDomain() {
return domain;
}
public String getCustomKey() {
return customKey;
}
public String getUserName() {
return userName;
}
public String getCredential() {
return credential;
}
}

我有以下更新声明

@Modifying
@Query("update CredentialEntity c set c.credential= ?1 where c.systemId = ?2 and c.domain = ?3 and c.customKey = ?4 and c.userName = ?5")
public void updateCredential(String credential, String systemId, String domain, String customKey, String userName);

它所做的是在表中插入另一行

来自

mysql> select * from credential;
+---------------+-----------+--------+------------+-----------+-----------------------------------------+
| credential_id | system_id | domain | custom_key | user_name | credential                              |
+---------------+-----------+--------+------------+-----------+-----------------------------------------+
|             8 | AAAA      | BBBB   | CCCC       | user1     | {"userName":"user1","password":"pass1"} |
|             9 | AAAA      | BBBB   | CCCC       | user2     | {"userName":"user2","password":"pass2"} |

运行更新后,我得到

+---------------+-----------+--------+------------+-----------+-----------------------------------------+
| credential_id | system_id | domain | custom_key | user_name | credential                              |
+---------------+-----------+--------+------------+-----------+-----------------------------------------+
|             8 | AAAA      | BBBB   | CCCC       | user1     | {"userName":"user1","password":"pass1"} |
|             9 | AAAA      | BBBB   | CCCC       | user2     | {"userName":"user2","password":"pass2"} |
|            10 | AAAA      | BBBB   | CCCC       | user2     | {"userName":"user3","password":"pass3"} |

我想要的是两行,第二行user2,更新为user3和password3

上面的代码确实有效。一个非常愚蠢的错误。我调用了错误的方法,而不是调用调用更新的服务,而是调用set方法。

当你把眼镜放错地方的时候,不要在晚上编码。

相关内容

  • 没有找到相关文章

最新更新