Mohan Raja
Mohan Raja

Reputation: 129

Eclipse link JPA - Get only entity of associated table records

I am new to JPA. In my new project, we are using Eclipse Link JPA.

I have two tables. Students, Addresses. Each student may have multiple addresses(For address history). So one to many relationship. In StudentEntity.java file, I mentioned Set.

Now my question is, whenever I am fetching all student details, inside Set, I needed only one Addressed entry, based on created date in addresses table. That means When I fetch List using createQuery, how can I mention, associated table conditions.

Example:-

StudentEntity

STUDENT1 --- John STUDENT2 --- Robert

Addresses

Addr_id - STU_ID -- Addr1 --City ---created date 1 - STUDENT1 -- latestadd --latestcity -- 23-8-2017 2 - STUDENT1 -- oldaddr --oldcity -- 12-8-2016

Student:-

@OneToMany() Set adddresses;

So when i don createQuery("select s from Students"), i want to get only address having addr_id = 1 which is latest address.

How can i achieve this using createQuery?

Upvotes: 0

Views: 227

Answers (2)

Mohan Raja
Mohan Raja

Reputation: 129

Got the required result with below query

Student s join s.addresses a where a.id = 1" is giving result and also, for "@OneToMany" annotation we need to give "@JoinFetch".

Upvotes: 1

Aran Smith
Aran Smith

Reputation: 11

Use createQuery("select s from Students where addr_id = 1") As far as i can tell this is what you are asking for

Upvotes: 0

Related Questions