java - Jackson Circular Dependencies -


i have circular dependency struggling solve right take these 2 classes - boiler plate code removed demo purposes

class 1

@entity @table(name = "t_credential") @cache(usage = cacheconcurrencystrategy.read_write) @jsonidentityinfo(generator=objectidgenerators.intsequencegenerator.class, property="@id") public class credential implements serializable {      @id     @generatedvalue(strategy = generationtype.auto)     private long id;       //removed @jsonignore want disable fields if no credentials available         //also fetch in eager fashion instead of lazily loading     @onetomany(mappedby = "credential",fetch=fetchtype.eager)     private set<usertask> usertasks = new hashset<>();      ....      ..... 

class 2

@entity @table(name = "t_usertask") @cache(usage = cacheconcurrencystrategy.read_write) @jsonidentityinfo(generator=objectidgenerators.intsequencegenerator.class, property="@id") public class usertask implements serializable {      /**      *       */     private static final long serialversionuid = -8179545669754377924l;       @id     @generatedvalue(strategy = generationtype.auto)     @column(name = "id")     private long id;       @manytoone(fetch=fetchtype.eager)     @notnull(message = "credential must not null")     private credential credential; 

unfortunately have use cases usertask needs load credentials , cases credential needs load usertasks

the annotation @jsonidentityinfo seems doing job if load usertasks, loads first usertask , credential, credential object load usertasks assigned credential. encounters new @id or usertask loads credential instead of 2 users tasks

any ideas can circumvent problem?

cheers damien

--question update have updated code use new annotations mentioned other users

class 1

    @entity @table(name = "t_credential") @cache(usage = cacheconcurrencystrategy.read_write) public class credential implements serializable {      /**      *       */     private static final long serialversionuid = -8696943454186195541l;        //removed @jsonignore want disable fields if no credentials available         //also fetch in eager fashion instead of lazily loading     @onetomany(mappedby = "credential",fetch=fetchtype.eager)     @jsonmanagedreference("credential")     private set<usertask> usertasks = new hashset<>();     .... .... 

class 2

@entity @table(name = "t_usertask") @cache(usage = cacheconcurrencystrategy.read_write) public class usertask implements serializable {       @id     @generatedvalue(strategy = generationtype.auto)     @column(name = "id")     private long id;      @manytoone(fetch=fetchtype.eager)     @notnull(message = "credential must not null")     @jsonbackreference("credential")     private credential credential; .... .... 

these classes work , have no circular dependencies however, when query usertasks or single task, credential object not returned though not have @jsonignore annotation specified. ideas causing this?

if using jackson httpmessageconverter, should check documentation - http://wiki.fasterxml.com/jacksonfeaturebidirreferences

you should add annotations shown here:

public class nodelist {     @jsonmanagedreference     public list<nodeforlist> nodes; }  public class nodeforlist {     public string name;      @jsonbackreference public nodelist parent;      public nodeforlist() { this(null); }     public nodeforlist(string n) { name = n; } } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -