Home > Net >  Embedded and Embeddable data not stored in the database
Embedded and Embeddable data not stored in the database

Time:12-13

I am Learning java-spring boot and i was able to store the normal data in db. but i wasn't able to store the complex json data.

the json data:

{
    "id": 1,
    "name": "Leanne Graham",
    "address": {
        "street": "Kulas Light",
        "city": "Gwenborough"
    }
}

User.java

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
@Entity
@Table(name = "User_tbl")
public class User {
    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    private Long id;
    private String name;
    @Embedded
    @Column(name = "Address")
    private Address address;
}

Address.java

import jakarta.persistence.Embeddable;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
@Embeddable
public class Address {
    private String street;
    private String city;
}

UserController.java

import com.example.JsonParser.model.User;
import com.example.JsonParser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/list")
    public Iterable<User> list() {
        return userService.list();
    }
    @PostMapping("/add")
    public User save(User user){

        return userService.add(user);
    }
}

UserRepository.java

import com.example.JsonParser.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}

UserService.java

import com.example.JsonParser.model.User;
import com.example.JsonParser.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserRepository repo;
    public Iterable<User> list() {
        return repo.findAll();
    }

    public Iterable<User> save(List<User> users) {
        return repo.saveAll(users);
    }

    public User add(User user) {
        repo.save(user);
        return user;
    }
}

application.yml

spring:
  h2:
    console:
      enabled: 'true'
  datasource:
    url: jdbc:h2:mem:Ramesh
  jpa:
    defer-datasource-initialization: 'true'

when i send the post request with json data

{
    "id": 1,
    "name": "Leanne Graham",
    "address": {
        "street": "Kulas Light",
        "city": "Gwenborough"
    }
}

i got response as

{
    "id": 1,
    "name": null,
    "address": null
}

H2 db: enter image description here also, the db is empty what i should modify or add in order to store the data?

CodePudding user response:

The first thing is that the data from the request is not reaching the Controller. If you put a breakpoint in your save method you can see that all the attributes inside the User will be null. So the first thing you need to do is to annotate the method argument with this @RequestBody, so automatically the JSON data will be converted to your Bean (given that the fields are having the same name).

@PostMapping("/add")
public User save(@RequestBody User user){

    return userService.add(user);
}

Second thing is that both your User and Address class should be annotated with @NoArgsConstructor.

So once you've done both the issue will be solved, and data will be saved and retrieved properly.

CodePudding user response:

As you have created Two Entity Classes. First, you have to save the Address and then take Address Instance and set it in the User Entity class and then save it. OR Make address as a string instead of Entity class and Try to Pass the data as JSON String to the controller so that I will save it as JSON String

   public class User {
    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    private Long id;
    private String name;
    private String address;
}

{
  "id": 1,
  "name": "Leanne Graham",
  "address": "{    "street": "Kulas Light",    "city": "Gwenborough"  }"   
}
  • Related