I have two entities with a OneToOne relation. I want to query the main Entity by a value on the secondary entity:
@Entity
public class Main {
@Id
@GeneratedValue(strategy= GenerationType.UUID)
private String oid;
@OneToOne(mappedBy = "main")
private Secondary secondary;
}
@Entity
public class Secondary {
@Id
@GeneratedValue(strategy= GenerationType.UUID)
private String oid;
private String value;
@OneToOne
@JoinColumn(referencedColumnName = "oid")
private Main main;
}
In Spring Data this is straight forward with a repository method:
findBySecondary_Value(String value)
Do I need to use a native query or JPQL for that or is there a way with the static find
method from PanacheEntityBase
?
I'm not quite sure what you mean with findBySecondary_Value(..)
, I'm not that familiar with Spring (Data), but you can query from the Secondary the Main entity, like this:
public static Secondary findByMainValue(String value) {
return find("main.field = ?1", value).firstResult(); //or return optional
}