Reputation: 69
I'm trying to create an entity that has the same entity type as a collection. I have a "view" of this entity in the database, and it has the org_id and the root_id. The org_id in the entity is mapped with the @Id column.
In the "view" of the sql, I don't have a foreign key mapped. Is it possible to map this foreign key in hibernate / JPA somehow, or specify a query to do so? I'm using spring data, so JPQL would work.
The code looks like this:
@Entity
@Table
public class SupplierData {
@Id
@Column(name = "org_uuid")
private String orgUUID;
@Column(name = "root_uuid")
private String rootUUID;
@ManyToOne
@JoinColumn(name="root_uuid", insertable=false, updatable=false)
private SupplierData rootSupplierData;
@OneToMany(mappedBy="rootSupplierData", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Collection<SupplierData> supplierDatas=new HashSet<>();
// ......
But a findall query doesn't show any results. If I remove the ManyToOne and OneToMany, all of my data is shown.
I think I'm missing a foreign key relation in the database, but I can't add that myself. Is there a way to map that myself in the jpa annotations?
Upvotes: 1
Views: 307
Reputation: 69
I fixed it. I had to remove one of the rootUUID references. I had both the string and the object. I can use one or the other.
For example I remove @Column(name = "root_uuid") . private String rootUUID;
And it works. That's because @JoinColumn(name="root_uuid"... Already references the column.
Upvotes: 1