【发布时间】:2018-07-22 11:06:39
【问题描述】:
我正在尝试使用 Spring Data JPA Inner Join 从不同的表中访问特定列。
我创建了四个模型。 AccountModel 是主要模型。 我在 AccountModel 中使用了 manyToOne 映射到其他三个模型(DepartmentModel、AccountCPCMappingModel、InvestigatorModel)。我正在尝试使用 AccountRepository 中的本机查询访问四个表中的特定列
这是我的应用场景。
1.AccountModel.java
package com.demo.model;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="account")
public class AccountModel implements Serializable {
private static final long serialVersionUID = 1L;
@Id
public Integer naccountid;
public String namount;
public String sacctdesc;
public Integer naccountcpcmappingid;
public Integer nindirectcostrate;
public Integer nagencyid ;
public Integer ndeptid ;
public String sgrantnum;
public Timestamp dstartdate;
public Timestamp denddate;
public String slocation;
public String sclientacctid;
public Integer ninvestigatorid;
public Integer ninstid;
public Integer ntempaccountid;
@ManyToOne(optional = true)
@JoinColumn(name="ndeptid",insertable = false, updatable = false)
public DepartmentModel department;
@ManyToOne(optional = true)
@JoinColumn(name="ninvestigatorid",insertable = false, updatable = false)
public InvestigatorModel investigator;
@ManyToOne(optional = false)
@JoinColumn(name="naccountcpcmappingid",insertable = false, updatable = false)
public AccountCPCMappingModel accountCPC;
//...Getter and Setter
}
2 部门模型
package com.demo.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "department")
public class DepartmentModel implements Serializable {
private static final long serialVersionUID = 1L;
@Id
public Integer ndeptid;
public String sdeptname ;
public Integer ninstid ;
public Boolean bislocked;
public String sclientdeptid;
public Integer nsurveymethodid;
public Boolean bisjointuse;
public Integer ntempdeptid;
public Boolean balternatejointusepercentage;
public Integer ndivid;
//...Getter and Setter
3.InvestigatorModel
package com.demo.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="investigator")
public class InvestigatorModel implements Serializable{
private static final long serialVersionUID = 1L;
@Id
public Integer ninvestigatorid;
public String sinvestigatorname;
public Integer ninstid ;
public String stitle;
public Integer ntempinvestigatorid;
public Integer nempid;
帐户存储库
package com.demo.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.demo.model.AccountModel;
@Repository
public interface AccountRepository extends JpaRepository<AccountModel, Integer>{
@Query(value="select acct.sclientacctid,acct.sacctdesc,acct.slocation,invest.sinvestigatorname, \r\n" +
"dept.sclientdeptid,dept.sdeptname,acp.sccpcode \r\n" +
"from Account acct join Department dept on acct.nDeptID=dept.nDeptID \r\n" +
"join Investigator invest on acct.nInvestigatorID=invest.nInvestigatorID \r\n" +
"join AccountCPCMapping acp on acct.nAccountCPCMappingID=acp.nAccountCPCMappingID \r\n" +
"where acct.nInstID=60 \r\n" +
"order by acct.sclientacctid Asc",nativeQuery=true)
List<Object[]> findByNaccountid();
}
账户服务
package com.demo.services;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.model.AccountModel;
import com.demo.repository.AccountRepository;
@Service
public class AccountService {
@Autowired
AccountRepository accRepo;
public List<AccountModel> findLocation()
{
return accRepo.findBySlocation();
}
public Set<Object[]> gridLoad()
{
Set<Object[]> gridObj=accRepo.findByNaccountid();
return gridObj;
}
}
帐户控制器
package com.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.demo.model.AccountModel;
import com.demo.services.AccountService;
@RestController
@RequestMapping("/SpaceStudy/SpaceAdmin")
public class AccountController
{
@Autowired
AccountService accService;
@CrossOrigin(origins="*")
@GetMapping("/AccountMaintenance/LoadLocation")
public List<AccountModel> findLocation()
{
return accService.findLocation();
}
@CrossOrigin(origins="*")
@PostMapping("/AccountMaintenance/LoadGrid")
public Set<Object[]> GridLoad()
{
return accService.gridLoad();
}
}
我得到这种格式的输出
但我想要这样的 JSON 格式输出(键、值对)
样本输出
[
{
"userId": 1,
"id": 1
}
]
谁能帮我在我的 json 数据代码中更改什么。
【问题讨论】:
-
将您的方法名称
findByNaccountid更改为getBasedOnNAccountId并试一试。 -
我试过这个,但我得到了同样的错误
-
在要检索的列列表中添加 n.accountid 字段
-
我根据要求更新了我以前的 Quation 如果你知道怎么做请回复我
-
你可以选择 abc as abc
标签: spring-data-jpa inner-join hibernate-mapping many-to-one