Home > other >  Hibernate - Use formula on column from Secondary table
Hibernate - Use formula on column from Secondary table

Time:03-15

I have the following entity :

@Entity
@Table(name = "myTable")
@SecondaryTable(name = "myOtherTable", pkJoinColumns = @PrimaryKeyJoinColumn(name = "someId"))
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ContactEntity {

  @Id
  @Column(name = "id")
  private Long id;
  @Column(name = "firstName", table = "myOtherTable")
  private String firstName;
  @Column(name = "lastName", table = "myOtherTable")
  private String lastName;

}

firstName and lastName from myOtherTable should be trimed. I tried using @Formula("trim(firstName)" but I get the following error firstName does not exist in table myTable Is there a way to apply a formula to a secondary table ? (without using select ... from myOtherTable)? Or should I use a converter like below https://www.baeldung.com/jpa-attribute-converters Thanks.

CodePudding user response:

@Formula makes the field read-only so you probably want to use the converter instead. From the javadoc:

Defines a formula (derived value) which is a SQL fragment that acts as a @Column alternative in most cases. Represents read-only state.

  • Related