无法在Spring Boot中持久化MEDIUMTEXT类型字段。

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

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 &#39;value&#39; 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 = &quot;key is mandatory&quot;)
@Column(columnDefinition = &quot;CHAR&quot;)
@Type(type = &quot;org.hibernate.type.CharArrayType&quot;)
private char[] key;

@Column(columnDefinition = &quot;MEDIUMTEXT&quot;)
@Type(type = &quot;org.hibernate.type.TextType&quot;)
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(&quot;/xyz&quot;)
public HttpStatus insertValue(@RequestHeader(value=&quot;id&quot;) 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 &#39;setValue(value)&#39; line, it works, else I get an error mentioned above. I have tried using @Lob with columnDefinition = &quot;MEDIUMTEXT&quot;. Also, I have tried putting length in the @Column, that doesn&#39;t work as well. What is it that I am doing wrong? Thanks in advance. 

</details>


# 答案1
**得分**: 1

因为你使用了MySQL的保留关键字,即&quot;key&quot;。你需要在字段&#39;key&#39;的声明之前使用@Column(name = &quot;\&quot;key\&quot;&quot;)进行映射。你可以参考[这里][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. &quot;key&quot;. You need to map it using @Column(name = &quot;\&quot;key\&quot;&quot;) above your declaration of the field &#39;key&#39;. You can refer [here][1] for more information about reserved keywords.
  [1]: https://vladmihalcea.com/escape-sql-reserved-keywords-jpa-hibernate/

</details>



huangapple
  • 本文由 发表于 2020年5月5日 18:12:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61610687.html
匿名

发表评论

匿名网友

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

确定