加入多个实体生成的JSON没有属性名称。
合作伙伴系统:
@Entity
@Table(name = "partnersystem")
@NamedQueries({
@NamedQuery(name="PartnerSystem.findAllPartnerSystemByPartnerId",
query="SELECT new ps.idPartnerSystem, "
+ "ps.partnerIdPartner, ps.name, "
+ "e.name as env, s.name as system "
+ "FROM PartnerSystem ps "
+ "LEFT JOIN ps.partner p "
+ "LEFT JOIN ps.system s "
+ "LEFT JOIN s.environment e "
+ "WHERE ps.partnerIdPartner = :partnerIdPartner ")
})
public class PartnerSystem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idPartnerSystem")
private int idPartnerSystem;
..
@ManyToOne (cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinColumn(name = "partner_idPartner", insertable=false, updatable=false)
private Partner partner;
..
@ManyToOne (cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinColumn(name = "PartnerSystem_idSystem", insertable=false, updatable=false)
private System system;
}
合作伙伴
@Entity
@Table(name = "partner")
public class Partner
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idPartner")
private int idPartner;
..
@JsonIgnore
@OneToMany(mappedBy="partner",
cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
private List<PartnerSystem> partnerSystems = new ArrayList<>();
..
}
系统
@Entity
@Table(name = "system")
public class System {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idSystem")
private int idSystem;
@JsonIgnore
@OneToMany(mappedBy="system",
cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
private List<PartnerSystem> partnerSystems = new ArrayList<>();
@ManyToOne (cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinColumn(name = "Environment_idEnvironment", insertable=false, updatable=false)
private Environment environment;
}
存储库,命名查询的存储库方法返回List,因此从逻辑上讲,它假定返回字段和值的数组。
@Repository
public interface PartnerSystemRepository
extends JpaRepository<PartnerSystem, Integer>{
public List<Object[]> findAllPartnerSystemByPartnerId(int partnerIdPartner);
}
输出:
[
[
1115,
459,
"CP US5-Generic",
"US Internal",
null,
19,
null,
"Dev",
"Generic"
],
[
1118,
459,
"CP US5-SAP",
"US Internal",
null,
21,
null,
"Dev",
"SAP"
]
]
如何获取带有属性名称的JSON