1

I am using hibernate annotation @GenerateType to generate the value for the non-primary key. Here I need to pass some values(as parameters) to generator class. I saw we can do it using Configurable. But problem is we can only use them for primary keys(@Id), whereas my one is non-primary. Can any one suggest me the right approach.

Code:

@Entity
public class Student{
    @GeneratorType(type = IdGenerator.class,when= GenerationTime.INSERT)
    private Integer deptId;
}  

public class IdGenerator implements ValueGenerator<Integer>{

    @Override
    public Integer generateValue(Session session, Object owner) {
       //Need to return value based on parameter** which changes dynamically.
    }

}
Sai
  • 11
  • 2
  • https://stackoverflow.com/questions/277630/hibernate-jpa-sequence-non-id – Nikolas Charalambidis May 18 '21 at 05:44
  • Thank you for the reply. But I did not find the code which is suitable for my scenario. i.e., pass parameters to generators. – Sai May 18 '21 at 05:48
  • Tied this way, still JsonSerializer> createContextual(SerializerProvider prov, BeanProperty property) is not getting called. @GeneratorType(type = IdGenerator.class,when= GenerationTime.INSERT) @JsonSerialize(using = IdGenerator.class) @InjectSeqParam(sequenceFldName = "deptId") private Integer deptId; – Sai May 18 '21 at 07:49

0 Answers0