dto

How to convert a JPA OneToMany relationship to DTO

三世轮回 提交于 2020-07-20 10:27:19
问题 I have a class Plan in which there is a list of Activity . The Activity class has a reference to a single Plan . Hence there is a OneToMany relationship like this: @Entity public class Plan { @OneToMany(mappedBy = "Plan") private List<Activity> activities; } @Entity public class Activity { @ManyToOne @JoinColumn(name= "PLAN_ID") private Plan plan; } I need to convert them to DTOs to be sent to presentation layer. So I have an assembler class to simply convert domain objects to POJO. public

spring boot data @query to DTO

淺唱寂寞╮ 提交于 2020-07-18 07:23:58
问题 I want to assign the result of a query to a DTO object. The DTO looks like this: @Getter @Setter @NoArgsConstructor public class Metric { private int share; private int shareholder; public Metric(int share, int shareholder) { this.share = share; this.shareholder = shareholder; } } And the query looks like the following: @RepositoryRestResource(collectionResourceRel = "shareholders", path = "shareholders") public interface ShareholderRepository extends PagingAndSortingRepository<Shareholder,

How to hash password before inserting?

笑着哭i 提交于 2020-07-09 19:15:13
问题 I am using Nest.Js with TypeORM and I want to hash my password before persisting into the DB. I tried using the event decorator @BeforeInsert() however it wasn't working for me but later I found that it was not working because I was taking an DTO as an input. user.controller.ts @Post() async create(@Body() data: CreateUserDto, @Res() res) { // if user already exist const isUserExist = await this.service.findByEmail(data.email); if (isUserExist) { throw new BadRequestException('Email already

When saving entity converted from a DTO, hibernate throws TransientPropertyValueException: object references an unsaved transient instance"

我与影子孤独终老i 提交于 2020-06-17 10:24:43
问题 First off - I know, it might seem like the same question has been asked a million times. However, this is related rather to DTOs, not entities nor missing cascades. If I create an entity myself and save it, everything is fine. The problem occurs when I create a DTO, convert it with ModelMapper and then try to save the converted entity. If you look at the test class, the first test(saveCarByEntity) passes but the second(saveCarByDto) one produces the error. Every class connected can be seen

Is it a good practice to use DTO in the service layer? [duplicate]

北城以北 提交于 2020-05-31 05:42:10
问题 This question already has answers here : Spring Entities should convert to Dto in service? (1 answer) Should services always return DTOs, or can they also return domain models? (9 answers) Which layer should be used for conversion to DTO from Domain Object (2 answers) Should service layer accept a DTO or a custom request object from the controller? (2 answers) Data Transfer Object DTO Where to build (4 answers) Closed 3 months ago . I have a controller that converts dto to an entity and

How to manually test input validation with NestJS and class-validator

大兔子大兔子 提交于 2020-05-16 08:54:28
问题 TLNR: I was trying to test DTO validation in the controller spec instead of in e2e specs, which are precisely crafted for that. McDoniel's answer pointed me to the right direction. I develop a NestJS entrypoint, looking like that: @Post() async doStuff(@Body() dto: MyDto): Promise<string> { // some code... } I use class-validator so that when my API receives a request, the payload is parsed and turned into a MyDto object, and validations present as annotations in MyDto class are performed.

How to manually test input validation with NestJS and class-validator

一曲冷凌霜 提交于 2020-05-16 08:52:32
问题 TLNR: I was trying to test DTO validation in the controller spec instead of in e2e specs, which are precisely crafted for that. McDoniel's answer pointed me to the right direction. I develop a NestJS entrypoint, looking like that: @Post() async doStuff(@Body() dto: MyDto): Promise<string> { // some code... } I use class-validator so that when my API receives a request, the payload is parsed and turned into a MyDto object, and validations present as annotations in MyDto class are performed.

DTOs. Properties or fields?

为君一笑 提交于 2020-05-12 11:19:39
问题 I need to create some DTO classes to transport our business objects across WCF. Since these are just bags of data with no functionality, is there any reason I can't just use fields, or is there some good reason to expose them properly as properties? //fields [DataContract] class CustomerDTO { [DataMember] public int Id; [DataMember] public string Name; } //or properties? [DataContract] class CustomerDTO { [DataMember] public int Id { get; set; } [DataMember] public string Name { get; set; } }