我需要将DTO保存在链接到实体的数据库表中

发布于 2025-02-14 02:08:56 字数 4358 浏览 1 评论 0原文

我需要在数据库中保存用户的ID和应用程序ID,以跟踪更改。我已经尝试了模型模型,但它不起作用,当我尝试将DTO数据保存在db中时,我有一个错误

@Entity
@Table(name = "logfileapp")
public class LogFileApp implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer idLogApp;

@Column(name = "data")
private LocalDateTime data;
@Column(name = "nodoconsole")
private Integer nodoConsole;
@Column(name = "launchingmeetingdatagatheringstarting")
private Time launchingMeetingDataGatheringStarting;
@Column(name = "avganalysistime")
private BigDecimal avgAnalysisTime;
@Column(name = "automationenablingdate")
private Date automationEnablingDate;
@Column(name = "done")
private Boolean done;
@Column(name="idpreupdate")
private Integer idPreUpdate;
@Column
private String nome_App, apmCode, insertedInCastProgram, stakeholderEngagement,
stakeholderBrief, onBoardingKitDelivery, primaRestitution, ownerOnboarding, ownerAFP, 
gdsUnit, tecnologia, serverManager,
soloCMS, macchina, noteOnboarding, fase, afpStatus, pubblicatoDashboard, noteAppOwner,  
jiraautomationActivation,
repoAvailability, automationStatus, automationNotes, greenItIndex, 
onboardingKitClosing, sourceCodeFinalDelivery,
linkConfluence, businessCriticality, devMethodology, provider;

@ManyToOne(cascade = CascadeType.ALL , fetch = FetchType.LAZY)
@JoinColumn(name = "FK_idUtente")
private Utente utente;

@ManyToOne(cascade = CascadeType.ALL , fetch = FetchType.LAZY)
@JoinColumn(name = "FK_idApp")
private Applicazione applicazione;

我的DTO

public class LogFileAppDTO {

private Integer idLogApp;
private LocalDateTime data;
private Integer nodoConsole;
private Time launchingMeetingDataGatheringStarting;
private BigDecimal avgAnalysisTime;
private Date automationEnablingDate;
private Boolean done;
private Integer idPreUpdate;
private String nome_App, apmCode, insertedInCastProgram, stakeholderEngagement, stakeholderBrief,
        onBoardingKitDelivery, primaRestitution, ownerOnboarding, ownerAFP, gdsUnit, tecnologia, serverManager,
        soloCMS, macchina, noteOnboarding, fase, afpStatus, pubblicatoDashboard, noteAppOwner,
        jiraautomationActivation, repoAvailability, automationStatus, automationNotes, greenItIndex,
        onboardingKitClosing, sourceCodeFinalDelivery, linkConfluence, businessCriticality, devMethodology,
        provider;
private Integer idUtente;
private Integer idApplicazione;

public LogFileAppDTO() {
}

public LogFileAppDTO(LogFileApp l) {
    super();
    idLogApp = l.getIdLogApp();
    data = l.getData();
    nodoConsole = l.getNodoConsole();
    launchingMeetingDataGatheringStarting = l.getLaunchingMeetingDataGatheringStarting();
    avgAnalysisTime = l.getAvgAnalysisTime();
    automationEnablingDate = l.getAutomationEnablingDate();
    done = l.getDone();
    idPreUpdate = l.getIdPreUpdate();
    nome_App = l.getNome_App();
    apmCode = l.getApmCode();
    insertedInCastProgram = l.getInsertedInCastProgram();
    stakeholderEngagement = l.getStakeholderEngagement();
    stakeholderBrief = l.getStakeholderBrief();
    onBoardingKitDelivery = l.getOnBoardingKitDelivery();
    primaRestitution = l.getPrimaRestitution();
    ownerOnboarding = l.getOwnerOnboarding();
    ownerAFP = l.getOwnerAFP();
    gdsUnit = l.getGdsUnit();
    tecnologia = l.getTecnologia();
    serverManager = l.getServerManager();
    soloCMS = l.getSoloCMS();
    macchina = l.getMacchina();
    noteOnboarding = l.getNoteOnboarding();
    fase = l.getFase();
    afpStatus = l.getAfpStatus();
    pubblicatoDashboard = l.getPubblicatoDashboard();
    noteAppOwner = l.getNoteAppOwner();
    jiraautomationActivation = l.getJiraautomationActivation();
    repoAvailability = l.getRepoAvailability();
    automationStatus = l.getAutomationStatus();
    automationNotes = l.getAutomationNotes();
    greenItIndex = l.getGreenItIndex();
    onboardingKitClosing = l.getOnboardingKitClosing();
    sourceCodeFinalDelivery = l.getSourceCodeFinalDelivery();
    linkConfluence = l.getLinkConfluence();
    businessCriticality = l.getBusinessCriticality();
    devMethodology = l.getDevMethodology();
    provider = l.getProvider();
    idUtente = l.getUtente().getIdUtente();
    idApplicazione = l.getApplicazione().getIdApplicazione();
}

有三个实体,在修改其中一个实体时,我需要修改后实体的DTO ID和修饰符用户的ID。但是我无法将其放入数据库中,因为我的实体想要对象在DTO想要ID时。

I need to save the id of User and the id of Application in the database to keep track of the changes. I've tried the ModelMapper but it doesn't work and when i try to save the DTO data in the DB I've this error: java.lang.IllegalArgumentException: Unknown entity: it.progettogestionale.dto.generic.LogFileAppDTO

My Entity

@Entity
@Table(name = "logfileapp")
public class LogFileApp implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer idLogApp;

@Column(name = "data")
private LocalDateTime data;
@Column(name = "nodoconsole")
private Integer nodoConsole;
@Column(name = "launchingmeetingdatagatheringstarting")
private Time launchingMeetingDataGatheringStarting;
@Column(name = "avganalysistime")
private BigDecimal avgAnalysisTime;
@Column(name = "automationenablingdate")
private Date automationEnablingDate;
@Column(name = "done")
private Boolean done;
@Column(name="idpreupdate")
private Integer idPreUpdate;
@Column
private String nome_App, apmCode, insertedInCastProgram, stakeholderEngagement,
stakeholderBrief, onBoardingKitDelivery, primaRestitution, ownerOnboarding, ownerAFP, 
gdsUnit, tecnologia, serverManager,
soloCMS, macchina, noteOnboarding, fase, afpStatus, pubblicatoDashboard, noteAppOwner,  
jiraautomationActivation,
repoAvailability, automationStatus, automationNotes, greenItIndex, 
onboardingKitClosing, sourceCodeFinalDelivery,
linkConfluence, businessCriticality, devMethodology, provider;

@ManyToOne(cascade = CascadeType.ALL , fetch = FetchType.LAZY)
@JoinColumn(name = "FK_idUtente")
private Utente utente;

@ManyToOne(cascade = CascadeType.ALL , fetch = FetchType.LAZY)
@JoinColumn(name = "FK_idApp")
private Applicazione applicazione;

My DTO

public class LogFileAppDTO {

private Integer idLogApp;
private LocalDateTime data;
private Integer nodoConsole;
private Time launchingMeetingDataGatheringStarting;
private BigDecimal avgAnalysisTime;
private Date automationEnablingDate;
private Boolean done;
private Integer idPreUpdate;
private String nome_App, apmCode, insertedInCastProgram, stakeholderEngagement, stakeholderBrief,
        onBoardingKitDelivery, primaRestitution, ownerOnboarding, ownerAFP, gdsUnit, tecnologia, serverManager,
        soloCMS, macchina, noteOnboarding, fase, afpStatus, pubblicatoDashboard, noteAppOwner,
        jiraautomationActivation, repoAvailability, automationStatus, automationNotes, greenItIndex,
        onboardingKitClosing, sourceCodeFinalDelivery, linkConfluence, businessCriticality, devMethodology,
        provider;
private Integer idUtente;
private Integer idApplicazione;

public LogFileAppDTO() {
}

public LogFileAppDTO(LogFileApp l) {
    super();
    idLogApp = l.getIdLogApp();
    data = l.getData();
    nodoConsole = l.getNodoConsole();
    launchingMeetingDataGatheringStarting = l.getLaunchingMeetingDataGatheringStarting();
    avgAnalysisTime = l.getAvgAnalysisTime();
    automationEnablingDate = l.getAutomationEnablingDate();
    done = l.getDone();
    idPreUpdate = l.getIdPreUpdate();
    nome_App = l.getNome_App();
    apmCode = l.getApmCode();
    insertedInCastProgram = l.getInsertedInCastProgram();
    stakeholderEngagement = l.getStakeholderEngagement();
    stakeholderBrief = l.getStakeholderBrief();
    onBoardingKitDelivery = l.getOnBoardingKitDelivery();
    primaRestitution = l.getPrimaRestitution();
    ownerOnboarding = l.getOwnerOnboarding();
    ownerAFP = l.getOwnerAFP();
    gdsUnit = l.getGdsUnit();
    tecnologia = l.getTecnologia();
    serverManager = l.getServerManager();
    soloCMS = l.getSoloCMS();
    macchina = l.getMacchina();
    noteOnboarding = l.getNoteOnboarding();
    fase = l.getFase();
    afpStatus = l.getAfpStatus();
    pubblicatoDashboard = l.getPubblicatoDashboard();
    noteAppOwner = l.getNoteAppOwner();
    jiraautomationActivation = l.getJiraautomationActivation();
    repoAvailability = l.getRepoAvailability();
    automationStatus = l.getAutomationStatus();
    automationNotes = l.getAutomationNotes();
    greenItIndex = l.getGreenItIndex();
    onboardingKitClosing = l.getOnboardingKitClosing();
    sourceCodeFinalDelivery = l.getSourceCodeFinalDelivery();
    linkConfluence = l.getLinkConfluence();
    businessCriticality = l.getBusinessCriticality();
    devMethodology = l.getDevMethodology();
    provider = l.getProvider();
    idUtente = l.getUtente().getIdUtente();
    idApplicazione = l.getApplicazione().getIdApplicazione();
}

I have three entities, when modifying one of them I need for my DTO the id of the modified entity and the id of the modifier user. But I can't get it into the database because my entity wants the object while the dto wants the ids.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霓裳挽歌倾城醉 2025-02-21 02:08:58

对于dto< - >实体转换您可以使用:

  • ModelMapper
  • 映射
  • 实现接口转换器< s,t>和等。

您无法将DTO保存在db beacuse中,这不是实体类。

,但我无法将其纳入数据库,因为我的实体想要
当DTO想要ID时对象。
您可以将ID更改为DTO。
简单示例:

public class LogFileApp {
    private Utente utente;
}

public class LogFileAppDTO {
    private UtenteDTO utente;
}

如果您想要具有ID的地图嵌套对象,则可以使用深度映射。
有关更多信息,请参见本文:

https://www.baeldung.com/java-modelmapper 通过其他方法可以使工作更轻松。类似:

服务:

public interface MapperService {

    /**
     * Note: outClass object must have default constructor with no arguments
     *
     * @param <D>      type of result object.
     * @param <T>      type of source object to map from.
     * @param entity   entity that needs to be mapped.
     * @param outClass class of result object.
     * @return new object of <code>outClass</code> type.
     */
    <D, T> D map(T entity, Class<D> outClass);

    /**
     * Note: outClass object must have default constructor with no arguments
     *
     * @param entityList list of entities that needs to be mapped
     * @param outCLass   class of result list element
     * @param <D>        type of objects in result list
     * @param <T>        type of entity in <code>entityList</code>
     * @return list of mapped object with <code><D></code> type.
     */
    <D, T> List<D> mapAll(Collection<T> entityList, Class<D> outCLass);

    /**
     * Maps {@code source} to {@code destination}.
     *
     * @param source      object to map from
     * @param destination object to map to
     */
    <S, D> D map(S source, D destination);
}

服务实施

@Service
@RequiredArgsConstructor
public class MapperServiceImpl implements MapperService {

  private final ModelMapper modelMapper;

  /**
   * Note: outClass object must have default constructor with no arguments
   *
   * @param <D> type of result object.
   * @param <T> type of source object to map from.
   * @param entity entity that needs to be mapped.
   * @param outClass class of result object.
   * @return new object of <code>outClass</code> type.
   */
  public <D, T> D map(final T entity, Class<D> outClass) {
    return modelMapper.map(entity, outClass);
  }

  /**
   * Note: outClass object must have default constructor with no arguments
   *
   * @param entityList list of entities that needs to be mapped
   * @param outCLass class of result list element
   * @param <D> type of objects in result list
   * @param <T> type of entity in <code>entityList</code>
   * @return list of mapped object with <code><D></code> type.
   */
  public <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
    return entityList.stream().map(entity -> map(entity, outCLass)).collect(Collectors.toList());
  }

  /**
   * Maps {@code source} to {@code destination}.
   *
   * @param source object to map from
   * @param destination object to map to
   */
  public <S, D> D map(final S source, D destination) {
    modelMapper.map(source, destination);
    return destination;
  }
}

For DTO <--> Entity conversion you can use:

  • ModelMapper
  • MapStruct
  • implement the interface Converter<S, T> and etc.

You can't save DTO in DB beacuse it's not an Entity class.

But I can't get it into the database because my entity wants the
object while the dto wants the ids.
You can change Ids to DTOs.
Simple example:

public class LogFileApp {
    private Utente utente;
}

public class LogFileAppDTO {
    private UtenteDTO utente;
}

If you want map nested objects with ID, so you can use deep mappings.
See this article for more information:
https://www.baeldung.com/java-modelmapper

If you want to use ModelMapper you can make work easier with additional methods. Something like:

Service:

public interface MapperService {

    /**
     * Note: outClass object must have default constructor with no arguments
     *
     * @param <D>      type of result object.
     * @param <T>      type of source object to map from.
     * @param entity   entity that needs to be mapped.
     * @param outClass class of result object.
     * @return new object of <code>outClass</code> type.
     */
    <D, T> D map(T entity, Class<D> outClass);

    /**
     * Note: outClass object must have default constructor with no arguments
     *
     * @param entityList list of entities that needs to be mapped
     * @param outCLass   class of result list element
     * @param <D>        type of objects in result list
     * @param <T>        type of entity in <code>entityList</code>
     * @return list of mapped object with <code><D></code> type.
     */
    <D, T> List<D> mapAll(Collection<T> entityList, Class<D> outCLass);

    /**
     * Maps {@code source} to {@code destination}.
     *
     * @param source      object to map from
     * @param destination object to map to
     */
    <S, D> D map(S source, D destination);
}

Service implementation

@Service
@RequiredArgsConstructor
public class MapperServiceImpl implements MapperService {

  private final ModelMapper modelMapper;

  /**
   * Note: outClass object must have default constructor with no arguments
   *
   * @param <D> type of result object.
   * @param <T> type of source object to map from.
   * @param entity entity that needs to be mapped.
   * @param outClass class of result object.
   * @return new object of <code>outClass</code> type.
   */
  public <D, T> D map(final T entity, Class<D> outClass) {
    return modelMapper.map(entity, outClass);
  }

  /**
   * Note: outClass object must have default constructor with no arguments
   *
   * @param entityList list of entities that needs to be mapped
   * @param outCLass class of result list element
   * @param <D> type of objects in result list
   * @param <T> type of entity in <code>entityList</code>
   * @return list of mapped object with <code><D></code> type.
   */
  public <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
    return entityList.stream().map(entity -> map(entity, outCLass)).collect(Collectors.toList());
  }

  /**
   * Maps {@code source} to {@code destination}.
   *
   * @param source object to map from
   * @param destination object to map to
   */
  public <S, D> D map(final S source, D destination) {
    modelMapper.map(source, destination);
    return destination;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文