0

Based on the question asked here: Hibernate - Foreign keys instead of Entities

If I were to implement the following example:

@JoinColumn(name="message_key")
@ManyToOne(targetEntity=Message.class,fetch=FetchType.LAZY)
private Message message;

@Column(name="message_key", updatable=false, insertable=false)
private Long message_fk;

Wouldn't this affect Entity to DTO conversion? In this case, during conversion, the object graph will include the mappings of the Message object as well?

Would the following code behave in the exact same manner without the traversal of the Message object?

@Entity
@Table(name = "table")
public class Example {
    @Id
    @GeneratedValue
    private Long id;

    // more properties

    @Column(name = "message_key")
    private Long messageKey;

    // more properties

    @Override
    public Long getMessageKey() {
        return messageKey();
    }

    @Override
    public void setMessageKey(Long messageKey) {
        this.messageKey = messageKey;
    }

    // More getters and setters
}

In this case, there is already a column in the DDL for table named message_key, so a foreign key relationship is not entirely necessary however I just need the generated id to populate that column.

unjankify
  • 190
  • 9
  • Not entirely sure I understand your question, how are you converting the Entity to a DTO? Are you using an already existing mapper, or doing it manually? – Mário Fernandes Aug 16 '17 at 07:13
  • Using an existing mapper – unjankify Aug 16 '17 at 12:23
  • Normally, in those mappers you can define exceptions to the rule, meaning, you can say which properties you do not want to be copied, that should be more than enough – Mário Fernandes Aug 16 '17 at 14:03
  • @MárioFernandes using Mapstruct, the Message object could be ignored but by ignoring it we are now unable to access certain properties of the Message object. In this case, all I needed was the Long property in the Messages object. – unjankify Aug 16 '17 at 14:08
  • Why are you trying to access the Long property inside the Messages object, if you already have it as private Long message_fk? – Mário Fernandes Aug 17 '17 at 06:39

0 Answers0