79172160

Date: 2024-11-09 05:25:06
Score: 2.5
Natty:
Report link

When you attempt to join OtherEntity with the transient other field in MarkerEntity, JPA throws an error because it doesn't know how to handle this transient association Instead of using a @Transient field, you can define a relationship between MarkerEntity and OtherEntity using @ManyToOne. This allows JPA to handle the join naturally without needing a transient field.

@Entity @Table(name = "OTHER") public class OtherEntity implements Serializable { @Id private Long id; @Column(name = "code") private String code; // Add a default constructor for JPA public OtherEntity() {} public OtherEntity(Long id, String code) { this.id = id; this.code = code; } // Getters and Setters }

@Entity @Table(name = "MARKER") public class MarkerEntity implements Serializable { @Id private Long id; @Column(name = "OTHER_FIELD", insertable = false, updatable = false) private Long otherId; @ManyToOne @JoinColumn(name = "OTHER_FIELD", referencedColumnName = "id") private OtherEntity other; public MarkerEntity() {} public MarkerEntity(Long otherId, OtherEntity other) { this.otherId = otherId; this.other = other; } // Getters and Setters } With this relationship set up, your query can be simplified as follows

@Query("SELECT new MarkerEntity(m.other.id, m.other) FROM MarkerEntity m WHERE enter code herem.id = :id")? public List entities(@Param("id") Long id);

Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @Transient
  • User mentioned (0): @ManyToOne
  • User mentioned (0): @Entity
  • User mentioned (0): @Entity
  • User mentioned (0): @ManyToOne
  • Starts with a question (0.5): When you
  • Low reputation (1):
Posted by: Bhavesh mahajan