Kamel Mili Kamel Mili - 2 months ago 12
JSON Question

inserting in many to one relation

hey everyone i have 2 classes

Admins.java

package com.spark.entites;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;


import com.fasterxml.jackson.annotation.JsonView;
import com.spark.View;

@Entity
public class Admins {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonView(View.all.class)
private int idu;
@JsonView(View.all.class)
private String logins;
@JsonView(View.all.class)
private String password;
@JsonView(View.all.class)
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
@JoinColumn(name = "idr")
private Role role;

public int getIdu() {
return idu;
}

public void setIdu(int idu) {
this.idu = idu;
}

public String getLogins() {
return logins;
}

public void setLogins(String logins) {
this.logins = logins;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}



public Admins(int idu, String logins, String password, Role role) {
super();
this.idu = idu;
this.logins = logins;
this.password = password;
this.role = role;
}

public Admins() {
super();
}

}


and Role.java

@Entity
public class Role {


@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@JsonView(View.all.class)
private int idr ;
@JsonView(View.all.class)
private String role ;
public int getIdr() {
return idr;
}
public void setIdr(int idr) {
this.idr = idr;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public Role(int idr, String role) {
super();
this.idr = idr;
this.role = role;
}
public Role() {
super();
}


as you can see i have a
manyToOne
relation with this 2 classess , am using
spring-boot
i have repository like this :

public interface AdminRepository extends JpaRepository<Admins, Integer>{
}


and in the controller :

@RestController
public class AdminsController {

@Autowired
AdminRepository adminRepository;

// this one to load R
@JsonView(View.all.class)
@RequestMapping(value = "/Admins/load")
public List<Admins> load() {
return adminRepository.findAll();
}

@RequestMapping(value = "/Admins/insert")
public void insertusers(Admins a) {
adminRepository.save(a);
}

}


in my controller i have 2 function one to load and one to insert when i try to insert idr value like this :

http://localhost:8080/Admins/insert?logins=yosra&password=yossra&role.idr=1


am getting value of role null

{
idu: 23,
logins: "yosra",
password: "yossra",
role: null
}


which is suppose to be like this :

{
idu: 3,
logins: "bassem",
password: "admin",
role: {
idr: 1,
role: "Admin"
}
}


Thanks to any help.

Answer

Try to add getter and setter for field role in Admins.class or add attribute access = AccessType.FIELD to annotation @Entity of Admins.class.