Usr
Usr

Reputation: 2838

JPA @JoinColumn custom object type

I'm studying a project that uses JPA, which I'm pretty new at. I've encountered a logic that I'm not understanding quite well. I have an entity called A that has this field/column:

@Column(name = "COD_UOP_COO")
    private String codUopCoo;

then I have, in the same entity:

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "COD_UOP_COO", referencedColumnName = "COD_UOP")
    @BatchFetch(value = BatchFetchType.JOIN)
    private B b;

From what I understood, this means that b field is retrieved by joining the tables A and B on the fields specified in JoinColumn annotation, right? But how does the join work in this case? Is JPA retrieving an entity of the right-side of join clause?
Then I have this named query:

"select a from A a where (a.b.bfield = :parameter)"

what does this mean? Why are they checking equality on a field of entity B? In my table A I do not have any column that is foreign key with B. And b field is not a column in table A. So which column value of A am I checking? I'm missing the link with database structure.

Upvotes: 1

Views: 527

Answers (1)

Egeo
Egeo

Reputation: 140

The JPQL query is translatet to a SQL query.

This query return A objects where A.COD_UOP_COO = B.COD_UOP and B.bfield = parameter. Change bfield for the database column name of the element in the B Class.

You can see the nateive query adding this parameters to the persistence.xml:

<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>

Upvotes: 2

Related Questions