我想在使用JOIN执行SQL查询后实现DTO映射:
查询:
public List<Businesses> findCompaniesBusinessesById(Integer id) {
String hql = "SELECT bus FROM " + Businesses.class.getName() + " bus "
+ " INNER JOIN " + Companies.class.getName() + " comp "
+ " ON comp.id = bus.company_id "
+ " WHERE bus.business_owner_id = :id "
+ " AND bus.business_owner_type = 'Merchant' "
+ " ORDER BY bus.id ASC";
TypedQuery<Businesses> query = entityManager.createQuery(hql, Businesses.class).setParameter("id", id);
List<Businesses> businesses = query.getResultList();
return businesses;
}
实体公司:
@Entity
@Table(name = "companies")
public class Companies implements Serializable {
@Id
@Column(name = "id", unique = true, updatable = false, nullable = false)
private int id;
@Column(length = 255)
private String name;
@Column
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime created_at;
@Column
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime updated_at;
...
public Companies(String name) {
super();
this.name = name;
}
}
实体企业:
@Entity
@Table(name = "businesses")
public class Businesses {
@Id
@Column(name = "id", unique = true, updatable = false, nullable = false)
private int id;
@Column(length = 4)
private Integer business_owner_id;
@Column(length = 255)
private String business_owner_type;
@Column(length = 4)
private Integer company_id;
@Column
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime created_at;
@Column
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime updated_at;
.......
}
!注意!。在“企业”实体中,设计时没有属性name
。
DTO:
public class BusinessesDTO {
private Integer id;
private String name;
.......
}
!注意!在BusinessesDTO中,我们期望name
是设计好的,并且我们希望对其进行映射。
DTO映射:
@Mapper(config = BaseMapperConfig.class)
public interface BusinessesMapper {
BusinessesDTO toDTO(Businesses company);
.....
}
查询结果:
您会看到有一个列名。
我试图使用此代码来获取映射:
@Autowired
private BusinessesMapper businesses_mapper;
List<BusinessesDTO> list = null;
try {
list = StreamSupport.stream(merchantService.findCompaniesBusinessesById(id).spliterator(), false)
.map(businesses_mapper::toDTO)
.collect(Collectors.toList());
} catch (Exception e) {
e.printStackTrace();
}
String joinedList = list.stream()
.map(BusinessesDTO::getName)
.collect(Collectors.joining(", "));
我试图实现构造函数表达式来像这样直接创建DTO:
public List<Businesses> findCompaniesBusinessesById(Integer id) {
String hql = "SELECT org.datalis.plugin.entity.Companies(comp.name) FROM " + Businesses.class.getName() + " bus "
+ " INNER JOIN " + Companies.class.getName() + " comp "
+ " ON comp.id = bus.company_id "
+ " WHERE bus.business_owner_id = :id "
+ " AND bus.business_owner_type = 'Merchant' "
+ " ORDER BY bus.id ASC";
TypedQuery<Businesses> query = entityManager.createQuery(hql, Businesses.class).setParameter("id", id);
您知道什么适合使用构造函数吗?
java.lang.IllegalArgumentException: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode
\-[METHOD_CALL] MethodNode: '('
+-[METHOD_NAME] IdentNode: 'org.datalis.plugin.entity.Companies' {originalText=org.datalis.plugin.entity.Companies}
\-[EXPR_LIST] SqlNode: 'exprList'
\-[DOT] DotNode: 'companies1_.name' {propertyName=name,dereferenceType=PRIMITIVE,getPropertyPath=name,path=comp.name,tableAlias=companies1_,className=org.
plugin.entity.Companies,classAlias=comp}
+-[ALIAS_REF] IdentNode: 'companies1_.id' {alias=comp, className=org.datalis.plugin.entity.Companies, tableAlias=companies1_}
\-[IDENT] IdentNode: 'name' {originalText=name}
[SELECT org.datalis.plugin.entity.Companies(comp.name) FROM org.datalis.plugin.entity.Business