martin
martin

Reputation: 1787

Spring REST - add Entity to DB from JSON input - DB foreign key error

I have two entities: A and B:

enter image description here

In controller class I have these methods:

@RequestMapping(value="/json/get", method=RequestMethod.GET)
    public @ResponseBody List<B> getAll() {
        return BManager.findAll();
    }

@RequestMapping(value="/json/add", method=RequestMethod.POST)
    public @ResponseBody void addB(@RequestBody B b) {      
        B.setA(AManager.findByAddress(123456)); // I must be from JSON, not static!
        BManager.save(b);           
    }

URL /json/get return to me:

{  
   "value":1,
   "title":"hello"
}

there is no value for A. But it is ok. Problem is when I want to create new B entity from JSON input.

I send

curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"value":2,"title":"world"}' http://localhost:8080/test/json/add

If I have in method addB line B.setA(AManager.findByAddress(123456)); it save entity to db. But I need to specify address in input JSON like this (I can change json syntax):

curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"address":123456,"value":2,"title":"world"}' http://localhost:8080/test/json/add

it crash with:

SEVERE: Servlet.service() for servlet [spring] in context with path [/test] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'A_id' cannot be null

Questions:

1) How can I specify address (class A) from B in input JSON?

2) If I want address (from class A) in output JSON (class B) from method getAll how can I do it?

I would like to get /json/get:

{  
      "addrress":123456,
   "value":1,
   "title":"hello"
}

Upvotes: 0

Views: 1476

Answers (1)

Ramzan Zafar
Ramzan Zafar

Reputation: 1600

Your error is due to the primary key of the Address class this is null at the time of save try it like following

@RequestMapping(value="/json/add", method=RequestMethod.POST)
    public @ResponseBody void addB(@RequestBody B b) { 

        A a = AManager.findByAddress(123456);

        if(a == null)
        {
          // you need to create a new obj in here

           a = new A();
           a.setId(anyId);
            //remain code
        }

         //then set in here  
        B.setA(a); // I must be from JSON, not static!
        BManager.save(b);           
    }

for question # 2 Please post your POJO's

Updated

See you need to have Entity B as

 Class B {
         String id;
         String name;
         Address address;        
        //getter setters

        }



 Class Address
 {
   String address1;
   String address2;
   String code;

   //getter setters
 }

So in this sort of relation you need to send a json as

{
  name:"bla bla",
  id:"12345"
  address:{
            address1:"any dummy address",
            address2:"any dummy address",
            code:"12345"

  }

}

And now in your controller address object will be mapped to "b" object automatically by spring

@RequestMapping(value="/json/add", method=RequestMethod.POST)
    public @ResponseBody void addB(@RequestBody B b) { 

        // you can get your address object here or can save it directly
       Address address = b.getAddress();

        BManager.save(b);           
    }

Upvotes: 1

Related Questions