我正在开发一个购物应用程序,在我的应用程序中我有表租户,在租户表中我有列Binary_id,它是数据库中二进制表的主键。现在,当我向租户表发出get请求时,我将所有租户表字段作为JSON。但我有从二进制表到租户的@ManyToOne关系,即租户可以有多个二进制记录。因此,在从POSTMAN客户端进行GET调用而不是获取租户详细信息时,我需要将与该租户相关的所有二进制记录作为JSON。
现在,当我从POSTMAN客户端
调用http://localhost:8080/sportsmvc/rest/tenant时,我将获得如下JSON[
{
"id": 2,
"binaryId": "1002",
"name": "AltisArena"
},
{
"id": 9,
"binaryId": "1001",
"name": "Agon"
} ]
但我需要响应JSON如下:
[
{
"id": 2,
"name": "AltisArena",
"listOfBinary": [
{
"tenant_id": 2,
"location": "location1",
"description": "ABC"
},
{
"tenant_id": 2,
"location": "location2",
"description": "ABCD"
}
]
},
{
"id": 9,
"name": "Agon",
"listOfBinary": [
{
"tenant_id": 9,
"location": "location3",
"description": "desc1"
},
{
"tenant_id": 9,
"location": "location4",
"description": "desc2"
}
]
}
]
代码段:
租户实体:
@Entity
@Table(name="tenant", catalog="db_sports" )
// Define named queries here
@NamedQueries ( {
@NamedQuery ( name="TenantEntity.countAll", query="SELECT COUNT(x) FROM TenantEntity x" )
} )
public class TenantEntity implements Serializable {
private static final long serialVersionUID = 1L;
//----------------------------------------------------------------------
// ENTITY PRIMARY KEY ( BASED ON A SINGLE FIELD )
//----------------------------------------------------------------------
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", nullable=false)
private Integer id ;
@Column(name="name", nullable=false, length=300)
private String name ;
//----------------------------------------------------------------------
// ENTITY LINKS ( RELATIONSHIP )
//----------------------------------------------------------------------
@ManyToOne
@JoinColumn(name="binary_id", referencedColumnName="id")
private SwaBinaryEntity swaBinary ;
SWA_Binary实体:
@Entity
@Table(name="SWA_Binary", catalog="db_sports" )
// Define named queries here
@NamedQueries ( {
@NamedQuery ( name="SwaBinaryEntity.countAll", query="SELECT COUNT(x) FROM SwaBinaryEntity x" )
} )
public class SwaBinaryEntity implements Serializable {
private static final long serialVersionUID = 1L;
//----------------------------------------------------------------------
// ENTITY PRIMARY KEY ( BASED ON A SINGLE FIELD )
//----------------------------------------------------------------------
@Id
@Column(name="id", nullable=false, length=100)
private String id ;
@Column(name="file_location", nullable=false, length=400)
private String fileLocation ;
@Column(name="description", nullable=false, length=200)
private String description ;
TenantRestController:
@RequestMapping( value="/tenant",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public List<Tenant> findAll() {
return tenantService.findAll();
}
TenantServiceImpl:
@Override
public List<Tenant> findAll() {
List<TenantEntity> entities = tenantPersistence.loadAll();
List<Tenant> beans = new ArrayList<Tenant>();
for(TenantEntity entity : entities) {
beans.add(tenantServiceMapper.mapTenantEntityToTenant(entity));
}
return beans;
}
TenantServiceMapper:
public Tenant mapTenantEntityToTenant(TenantEntity tenantEntity) {
if(tenantEntity == null) {
return null;
}
//--- Generic mapping
Tenant tenant = map(tenantEntity, Tenant.class);
//--- Link mapping ( link to SwaBinary )
if(tenantEntity.getSwaBinary() != null) {
tenant.setBinaryId(tenantEntity.getSwaBinary().getId());
}
return tenant;
}
任何人都可以帮助解决这个问题。
先谢谢。
答案 0 :(得分:1)
也许我误解了模特,但似乎有点不对劲。在您需要JSON的情况下,您拥有多个二进制文件的租户,但在JPA模型中,它反之亦然,租户有一个二进制文件。
TenantEntity不应该这样吗?:
@OneToMany
private List<SwaBinaryEntity> swaBinary