有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Hibernate连接两个表以获得JSON结果

嗨,我是冬眠新手。我有两个实体/表,需要将它们组合起来,以将两个表作为一个JSON响应。i、 e当我调用租户表以及租户详细信息时,还必须检索与租户相关的二进制数据。这是我的桌子明细

Table tenant;

 id(primary key),
 binary_id(foreign key)(primary key in binary table),
 name

Table binary;

 id(primary key),
 tenant_id(foreign key)(primary key in tenant table),
 fileLocation,
 description

每个租户在二进制表中都有多条记录。i、 e每个租户将有多个二进制id,这些id在二进制表中有条目

我的SQL有点像

  SELECT tenant.name,binary.description,binary.fileLocation FROM Tenant tenant,Binary binary WHERE tenant.binary_id=binary.id

我不知道在hibernate中如何做

有人能帮助我如何获得租户和租户相关的二进制表数据作为JOSN响应吗

我的租户映射类:

    /**
 * Mapping from 'TenantEntity' to 'Tenant'
 * @param tenantEntity
 */
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.getBinary() != null) {
        tenant.setBinaryId(tenantEntity.getBinary().getId());
    }
    return tenant;
}


/**
 * Mapping from 'Tenant' to 'TenantEntity'
 * @param tenant
 * @param tenantEntity
 */
public void mapTenantToTenantEntity(Tenant tenant, TenantEntity tenantEntity) {
    if(tenant == null) {
        return;
    }

    //--- Generic mapping 
    map(tenant, tenantEntity);

    //--- Link mapping ( link : tenant )
    if( hasLinkToBinary(tenant) ) {
        BinaryEntity Binary1 = new BinaryEntity();
        Binary1.setId( tenant.getBinaryId() );
        tenantEntity.setBinary( Binary1 );
    } else {
        tenantEntity.setBinary( null );
    }

}

先谢谢你


共 (0) 个答案