我已经在 SQL 中完成了此操作,以获取parentName作为对parentId的响应。
public class FolderStructure extends CommonAttribute implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long folderId;
@JsonIgnore
@ManyToOne
@JoinColumn(name = "parentid")
private FolderStructure parent;
@Transient
private Long parentid;
@Transient
private String parentName;
@JsonIgnore
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<FolderStructure> childFolder = new HashSet();
public void setParentid() {
if (parent != null && parent.getFolderId() > 0) {
this.parentid = parent.getFolderId();
}
}
public void setParentName() {
if (parent != null && parent.getFolderId() > 0
&& parent.getFolderName() != null) {
this.parentName = parent.getFolderName();
}
}
我收到如下响应,{
folderId: 4,
folderName: "BATTERY LOG",
parentid: 1,
parentName: "Engine",
folderType: "custom"
}
我正在尝试使用 mongoDB 通过添加relmongo依赖性来执行相同的代码。但是我不明白如何在mongoDB中连接两列。这是我的模特
@EnableRelMongo
@Document(collection = "folderstructure")
public class FolderStructure
{
@JsonIgnore
@JoinProperty(name = "parentid")
private FolderStructure parent;
@Field("parentid")
private Long parentid;
@Transient
private String parentName;
@JsonIgnore
@OneToMany( fetch=FetchType.EAGER, cascade = CascadeType.PERSIST)
@JoinProperty(name = "parent")
private Set<FolderStructure> childFolder = new HashSet();
这是我的控制器,
@GetMapping("")
public String getAllFolders() {
String res = "";
try {
JSONObject jsonobj = new JSONObject();
List<FolderStructure> folderlst = folderDao.findAll();
List<FolderStructure> folderlstRes = new ArrayList<FolderStructure>();
for (FolderStructure folder : folderlst) {
folder.setParentid();
folder.setParentName();
folderlstRes.add(folder);
}
if (folderlstRes != null && folderlstRes.size() > 0) {
ObjectMapper mapperObj = new ObjectMapper();
String Detail = mapperObj.writeValueAsString(folderlstRes);
jsonobj.put("data", new JSONArray(Detail));
jsonobj.put("responsecode", 200);
jsonobj.put("message", "Success");
jsonobj.put("timestamp", new Date());
} else {
jsonobj.put("responsecode", 404);
jsonobj.put("message", "Record Not Found");
jsonobj.put("timestamp", new Date());
}
res = jsonobj.toString();
} catch (JSONException | JsonProcessingException ex) {
CampbellResponse derr = new CampbellResponse(500, new Date(), "JSON parsing exception occurred.");
res = derr.toString();
ex.printStackTrace();
} catch (Exception ex) {
CampbellResponse derr = new CampbellResponse(500, new Date(), "Exception is occurred.");
res = derr.toString();
ex.printStackTrace();
}
return res;
}
但是我得到的是parentName作为响应。