英文:
unable to persist MEDIUMTEXT type field in spring boot
问题
我在MySQL数据库中有一个名为'value'的字段,类型为MEDIUMTEXT。当我尝试持久化或获取模型时,会显示以下错误:
```无法提交JPA事务;嵌套异常为javax.persistence.RollbackException:在提交事务时出错```
## **模型** ##
```java
@Entity
@Table(name = "xyz_something")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Xyz {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@NotBlank(message = "key是必需的")
@Column(columnDefinition = "CHAR")
@Type(type = "org.hibernate.type.CharArrayType")
private char[] key;
@Column(columnDefinition = "MEDIUMTEXT")
@Type(type = "org.hibernate.type.TextType")
private String value;
// 获取器和设置器
仓库
public interface XyzRepository extends JpaRepository<Xyz, Integer> {
}
控制器
@RestController()
@RequestMapping("api/v1")
public class XyzController {
private static Logger logger = LogManager.getLogger();
@Autowired
XyzRepository xyzRepository;
@PutMapping("/xyz")
public HttpStatus insertValue(@RequestHeader(value="id") int id, @NotBlank @RequestBody String value) {
return upsert(value,id);
return HttpStatus.BAD_REQUEST;
}
private HttpStatus upsert(String value, int id) {
return xyzRepository.findById(id)
.map(xyz -> {
xyz
.setKey("key")
.setValue(value);
xyzRepository.save(xyz);
return HttpStatus.CREATED;
}).orElseGet(() -> {
Xyz xyz = new Xyz();
xyz
.setId(id)
.setKey("key")
.setValue(value)
xyzRepository.save(xyz);
return HttpStatus.CREATED;
});
}
}
如果我将'setValue(value)'这一行注释掉,它可以工作,否则我会得到上面提到的错误。我已经尝试过在@Column中使用@Lob和columnDefinition = "MEDIUMTEXT"。此外,我还尝试过在@Column中添加长度,但也不起作用。我做错了什么?提前谢谢。
<details>
<summary>英文:</summary>
I have a field named 'value' which is of type MEDIUMTEXT in the MySQL db. When I try to persist or fetch the model, it shows
```Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction```
## **Model** ##
@Entity
@Table(name = "xyz_something")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Xyz {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@NotBlank(message = "key is mandatory")
@Column(columnDefinition = "CHAR")
@Type(type = "org.hibernate.type.CharArrayType")
private char[] key;
@Column(columnDefinition = "MEDIUMTEXT")
@Type(type = "org.hibernate.type.TextType")
private String value;
// Getters and Setters
## **Repository** ##
public interface XyzRepository extends JpaRepository<Xyz, Integer> {
}
## **Controller** ##
@RestController()
@RequestMapping("api/v1")
public class XyzController {
private static Logger logger = LogManager.getLogger();
@Autowired
XyzRepository xyzRepository;
@PutMapping("/xyz")
public HttpStatus insertValue(@RequestHeader(value="id") int id, @NotBlank @RequestBody String value) {
return upsert(value,id);
return HttpStatus.BAD_REQUEST;
}
private HttpStatus upsert(String value, int id) {
return xyzRepository.findById(id)
.map(xyz -> {
xyz
.setKey("key")
.setValue(value);
xyzRepository.save(xyz);
return HttpStatus.CREATED;
}).orElseGet(() -> {
Xyz xyz = new Xyz();
xyz
.setId(id)
.setKey("key")
.setValue(value)
xyzRepository.save(xyz);
return HttpStatus.CREATED;
});
}
}
If I comment out the 'setValue(value)' line, it works, else I get an error mentioned above. I have tried using @Lob with columnDefinition = "MEDIUMTEXT". Also, I have tried putting length in the @Column, that doesn't work as well. What is it that I am doing wrong? Thanks in advance.
</details>
# 答案1
**得分**: 1
因为你使用了MySQL的保留关键字,即"key"。你需要在字段'key'的声明之前使用@Column(name = "\"key\"")进行映射。你可以参考[这里][1]获取有关保留关键字的更多信息。
[1]: https://vladmihalcea.com/escape-sql-reserved-keywords-jpa-hibernate/
<details>
<summary>英文:</summary>
It is because you are using a reserved keyword of MySQL i.e. "key". You need to map it using @Column(name = "\"key\"") above your declaration of the field 'key'. You can refer [here][1] for more information about reserved keywords.
[1]: https://vladmihalcea.com/escape-sql-reserved-keywords-jpa-hibernate/
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论