Ids for this class must be manually assigned before calling save on String ID
Asked Answered
R

3

8

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

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..

}

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.

Workarounds I found:

  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.

UPDATE

I'm using Spring Boot and Spring Data JPA.

How do I insert:

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.

The request I post:

{
    "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
    }
}
Reticulum answered 24/8, 2018 at 19:40 Comment(0)
C
7

try this approach

@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"
}
Christ answered 24/8, 2018 at 19:58 Comment(7)
I cannot use UUID. This primary key is set by a field which comes in a POST request.Reticulum
@MatheusCirillo ok, try only add @GeneratedValue(strategy=GenerationType.AUTO) for private String myId;Christ
Unfortunately, other errors raises. "Unknown integral data type for ids : java.lang.String; nested exception is org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String"Reticulum
@MatheusCirillo my question, how do you insert record ? do you use entityManager.persist ?Christ
Make sure you send myId as not nullChrist
I'm using Spring Data JPA. Question updated describing how I persist data.Reticulum
change to entityManager.persist(myClass) and make sure myId is not null, it should worksChrist
M
2

Based on the helpful answer by @PiotrRogowski, for numeric primary keys, for example those mapped to java.lang.Long:

@Entity
@Table(name = "myclass")
public class MyClass {
    @Id
    @GeneratedValue(generator = "Incremental")
    @GenericGenerator(
        name = "Incremental",
        strategy = "org.hibernate.id.IncrementGenerator",
    )
    @Column(name = "id", updatable = false, nullable = false, insertable = false, unique = true)
    private Long id;
        
    …
}

You can invoke it this way:
@Autowired
private EntityManager entityManager;
    
@PostMapping("")
@Transactional
public void add(@RequestBody MyClass myClass) {
    entityManager.persist(myClass);
}
Maura answered 29/11, 2023 at 15:30 Comment(0)
E
1

I faced this problem, turns out I was missing the setter for the id field. Make sure the setter method for the ID is defined in the entity class.

Exhalation answered 1/1, 2022 at 8:57 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Epiphany

© 2022 - 2024 — McMap. All rights reserved.