2

I basically have a Class A and a Class B where ClassA has many of ClassB, i use a OneToMany annotation to get a List of all the ClassB Objects that belong to a ClassA Object, now i want to convert the information in ClassA to a ClassA DTO but in the DTO i only want a list of the ids of the ClassB Objects and not the Objects. Im using the Spring Framework and the ModelMapper, the data of the objects can be accessed via rest api in Json format.

ClassA:

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "ClassA")
public class ClassA implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @OneToMany(
            mappedBy = "myClassA",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private java.util.List<Domain> domains = new ArrayList<>();
}

ClassB:

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDate;


@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "ClassB")
public class ClassB implements Serializable {
    private static final long serialVersionUID = 1L;
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Long id;
    @Column(name="projekt_id")
    private Long projectId;
    @ManyToOne
    @JsonIgnore
    @JoinColumn(name = "projekt_id", insertable = false, updatable = false)
    private ClassA myClassA;
}
MagicMikko
  • 21
  • 3
  • How are you using the modelmapper? – Amir M Sep 29 '21 at 11:27
  • i use the modelmapper for my other classes and their DTOs so i would prefer a solution using it for consistency – MagicMikko Sep 29 '21 at 11:29
  • What have you done using modelmapper that didn't work as you expected? Please show your work. – Amir M Sep 29 '21 at 11:31
  • nothing didnt work, i just dont know how to do it at all, so i thought i would mention im already using modelmapper incase thats somehow helpful or anything. sry if im a bit unclear im new to this ^^ – MagicMikko Sep 29 '21 at 11:35
  • Have you read modelmapper documents? start here http://modelmapper.org/getting-started/#mapping Also this will help http://modelmapper.org/examples/flattening/#example-1 – Amir M Sep 29 '21 at 11:38
  • i have read all that and i feel like it could be done with something like deep mapping but it seems like thats the not most efficient/fastest way to do it, this is part of a bigger project so runtime is something i have to keep in mind – MagicMikko Sep 29 '21 at 11:41

0 Answers0