Home > Software design >  How to make Json result using JPA inheritance (spring boot)
How to make Json result using JPA inheritance (spring boot)

Time:09-22

I have an error about "findAll" when I use JPA inheritage tables.

I what make the Json result like this ["asdf" : "adf", "asdf" : "asdf"]

but the return values are like [com.example.model.AccountEntity@57af674a]

Controller

@RequestMapping(value = "/getMyInfoall", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getMemberall(@RequestBody JSONObject sendInfo) throws IOException {

        List user = UserService.findAll();
        
        JSONObject result = new JSONObject();
        result.put("data", user);

        return result.toJSONString();
    }

Service

public List findAll() {

        List users = UserRepository.findAll();
        return users;
    }

Repository

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

Entity

@Entity(name = "Users")
@Inheritance(strategy = InheritanceType.JOINED)
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private int userkey;

    @Column(nullable = false, unique = true)
    private String id;

    @Column(nullable = false, length = 50)
    private String name;

    @Column(nullable = false)
    private String password;

    @Column(nullable = true)
    private String email;
}

@Entity(name = "Account")
public class AccountEntity extends UserEntity{

    @Column(nullable = false, unique = true)
    private String accountno;

    @Column(nullable = true)
    private String accountname;

    @Column(nullable = false)
    private int accountpw;

    @Column(nullable = false)
    private long balance;
}```

CodePudding user response:

I would highly recommend to use Spring's default HTTPMessageConverters, e.g. Jackson for JSON.

Building a JSON-array from a List

But you can also use JSON.org's light-weight library like guided on JSON-java README:

  1. convert the List to an array, e.g. UserEntity[]
  2. create a JSONArray from this Java array
  3. return this JSON-array representation formatted as String, using method toString()
List<UserEntity> userList = // a list returned from your database/repo
UserEntity[] myArr = userList.toArray(new UserEntity[userList.size()]); // convert this to an array
// here simply follow the guide on JSON
JSONArray jArr = new JSONArray(myArr);
// return the JSON-array as string
return jArr.toString();

CodePudding user response:

You should convert your UserEntity objects to a UserDto DTO that would then be returned in your Controller. Rely on Jackson instead of JSONObject managed and created by you.

public class UserDto {
    private String id;
    private String name;
}

You Service should do the mapping:

public List<UserDto> findAll() {
    List<UserEntity> users = UserRepository.findAll();
    return users.stream().map(user -> // your mapping logic to UserDto object);
}

And your Controller just needs to return it:

@RequestMapping(value = "/getMyInfoall", produces = MediaType.APPLICATION_JSON_VALUE)
public List<UserDto> getMemberall(@RequestBody JSONObject sendInfo) throws IOException {
    return UserService.findAll();
}

You can do a similar thing with JSONObject sendInfo and replace it with an object of your own.

  • Related