在调用保存字符串 ID 之前,必须手动分配此类的 ID [英] Ids for this class must be manually assigned before calling save on String ID

查看:18
本文介绍了在调用保存字符串 ID 之前,必须手动分配此类的 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经阅读了很多关于同一问题的问题,但我仍然无法解决这个问题.

Already read lots of questions about the same issue, but I still not be able to solve this problem.

我的数据库需要有一个 String 主键.

I need to have a String primary key on my database.

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class MyClass {

    @Id
    private String myId;
    private String name;

    // getters and setters..

}

问题是,如果我在 @Id 注释字段中使用 String 类型,当我尝试保存对象时,Hibernate 会抛出异常.

The problem is that, if I use String type in a @Id annotated field, Hibernate throws an exception when I try to save the object.

ids for this class must be manually assigned before calling 

是的,我正在为该字段设置一个值.

And yes, I'm setting a value to the field.

我发现的解决方法:

  1. 在字段中添加 @GeneratedValue 注释 - 无效
  2. 将字段类型更改为 Integer - 这对我来说不可行
  3. 添加接收 myId 作为参数的构造函数 public MyClass(String myId){ ... } - 无效
  4. 使用 UUID - 我不能,因为此 ID 是由 POST 请求中的字段设置的.
  1. Add @GeneratedValue annotation to the field - not worked
  2. Change the field type to Integer - it's not feasible for me
  3. Add a constructor that receives myId as parameter public MyClass(String myId){ ... } - not worked
  4. Use UUID - i can't, cause this id is set by a field that comes in with a POST request.

这些解决方法都不适合我.

None of these workarounds worked for me.

更新

我正在使用 Spring Boot 和 Spring Data JPA.

I'm using Spring Boot and Spring Data JPA.

如何插入:

我有一个 @PostMapping 带注释的方法,它处理 POST 请求并调用一个服务来执行一些业务逻辑并调用我的存储库进行持久化.

I have an @PostMapping annotated method which handles POST request and call a service that do some business logic and call my repository for persisting.

我发布的请求:

{
    "myId": "myId",
    "name": "myName"
}

MyService.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {

    @Autowired
    private MyRepository myRepository;

    public MyClass save(MyClass myClass) {
        return myRepository.save(myClass); // save() should persist my object into the database
    }
}

推荐答案

试试这个方法

@Entity
public class MyClass{

    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(
        name = "UUID",
        strategy = "org.hibernate.id.UUIDGenerator",
    )
    @Column(name = "id", updatable = false, nullable = false)
    private UUID id;

    …
}

======================================

======================================

我这样调用,在我的环境中一切正常:

I invoke like this and in my envirenment all work fine:

@Autowired
private EntityManager entityManager;

@PostMapping("")
@Transactional
public void add(@RequestBody MyClass myClass){
        entityManager.persist(myClass);
}

并请求通过带有正文的邮寄方式发送:

and requst send by post with body:

{
    "myId" : "213b2bbb1"
}

这篇关于在调用保存字符串 ID 之前,必须手动分配此类的 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆