1

I have two classes Job and Client. A client can have many jobs.The two classes are as follows:-

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,property="jobId", scope = Job.class)
public class Job {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "job_id")
private long jobId;
@ManyToOne
private Client client;
public Client getClient() {
    return client;
}
public void setClient(Client client) {
    this.client = client;
}


@Entity
@JsonIdentityInfo(
    generator = ObjectIdGenerators.PropertyGenerator.class,
    property = "clientId",scope = Client.class)
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long clientId;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "client")
@Transient
private Collection<Job> jobs;

I have a client with id=1 in database and I want to save the id of the client as a foreign key in job table .A part of json is as follows:-

{
    "client":{"clientId":1}
}

The error I get is :-

Could not read JSON document: Already had POJO for id (java.lang.Long) [[ObjectId: key=1, type=com.fasterxml.jackson.databind.deser.impl.PropertyBasedObjectIdGenerator, scope=java.lang.Object]]

Could someone please help me on this?

Ramesh Khadka
  • 484
  • 1
  • 7
  • 19
  • https://stackoverflow.com/questions/31158509/how-to-generate-custom-id-using-hibernate-while-it-must-be-primary-key-of-table – StanislavL Jul 12 '17 at 12:35
  • It is throwing this error because of your JsonIdentityInfo annotation, saying your XML already has a Client instance with clientId:1 somewhere within it. See https://stackoverflow.com/questions/37302816/how-to-use-jsonidentityinfo-with-circular-references or https://stackoverflow.com/questions/42558927/serializing-already-had-pojo-for-id-java-lang-string which suggest just using the ID, not the full object in your references. – Chris Jul 12 '17 at 15:16
  • I have sent client of id 1 in two different places in same json. The two clients will be saved in two different tables in databases and the above works fine for the different ids.When the id is 1 for both clients in json then this problem arises. – Ramesh Khadka Jul 13 '17 at 12:30

0 Answers0