java - How to create a model, in Spring, from json where the foreign key is referenced as a long attribute? -


one group has many users:

group

import com.fasterxml.jackson.annotation.jsonignore; import com.fasterxml.jackson.annotation.jsonmanagedreference;  import javax.persistence.*; import java.util.collection; import java.util.list;  @entity @table(name = "groups") public class group {      @id     @column(name = "id")     private long id;      @column(name = "name")     private string name;      //@jsonmanagedreference     @onetomany(mappedby = "group"             //, fetch = fetchtype.eager             //, cascade = cascadetype.all     )     private list<users> itsuser;      //getters , setters omitted clarity } 

users

import com.fasterxml.jackson.annotation.jsonbackreference; import com.fasterxml.jackson.annotation.jsonignore; import com.fasterxml.jackson.annotation.jsonmanagedreference; import com.fasterxml.jackson.annotation.jsonproperty;  import javax.persistence.*; import static javax.persistence.generationtype.sequence;  @entity @table(name = "users") @sequencegenerator(name = "sequence_user_id",     //my own name in java (unique)         sequencename = "generator_sequence_users",     //in database         initialvalue = 1,         allocationsize = 1) public class users {      @jsonproperty(value = "id") //these play role when both reading or writing     @id     @column(name = "id")     @generatedvalue(strategy=sequence, generator="sequence_user_id")     private long id;      @jsonproperty(value = "name")     @column(name="name")     private string name;      @jsonproperty(value = "username")     @column(name="username")     private string username;      @jsonproperty(value = "password")     @column(name="password")     private string password;      @jsonproperty(value = "email")     @column(name="email")     private string email;      @jsonproperty(value = "picture")    //now works both mypic , picture json keys     @column(name="picture")     private string picture;      //@column(name="group_id")  //we have manytoone this, cannot repeat     @jsonproperty(value = "groups_id")       //to ignore in jpa (http://stackoverflow.com/questions/1281952/jpa-fastest-way-to-ignore-a-field-during-persistence)     private long itsgroupid;      @transient     public long getitsgroupid() {         if(itsgroupid == null) {             this.itsgroupid = group.getid();         } else {             //nop         }         return itsgroupid;     }      public void setitsgroupid(long itsgroupid) {         this.itsgroupid = itsgroupid;     }      //@jsonignore     //@jsonproperty(value = "groups_id")     //@jsonbackreference     @manytoone(optional = false, targetentity = group.class)     @joincolumn(             name = "group_id",  //column name             referencedcolumnname = "id"   //reference name     )     private group group;     //getters , setters omitted clarity } 

we using spring spring-data , jackson things automagically cannot configure magic:

we trying stick on following constraints @ same time:

1) keep ability have reference groupid , manytoone relationship group.
easy achieved putting @transient annotation @ groupid because @column not allowed since have declared @manytoone annotation. have implement getgroupid method accordingly.

2) return json of users class contains groups_id.
can implemented setting @jsonproperty annotation.

3) create user class, , save in database, json. json contains groups_id has value integer foreign key.
not work because setting @transient above, system refuses save in database transient or @ least how interpret exception:

http status 500 - request processing failed; nested exception org.springframework.dao.dataintegrityviolationexception: not-null property references null or transient value: com.pligor.mvctest.models.users.group; nested exception org.hibernate.propertyvalueexception: not-null property references null or transient value: com.pligor.mvctest.models.users.group

on backend this:

group group = grouprepository.findbyid(userresource.getgroupid()); if (group != null) {    user user = new user(userresource);    user.setgroup(group);    userrepository.save(); } 

the idea behind need fetch group db, able link newly created user


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -