OneToMany Bi-Directional Mapping to fetch data through Query in Spring Boot

I'm having OneToMany bi-directional mapping for Drama-Characters (One Drama with multiple Characters).

Drama:

dramaId

dramaName

characters (Character entity)

Characters:

characterId

firstName

lastName

drama (drama entity reference)

Problem:

How can i fetch the drama details with respect to Character.firstName through Query annotation in CharacterRepository.

Upvotes: 0

Views: 126

Answers (1)

user404
user404

Reputation: 2028

It would be better if you could provide some code you tried. Whatever,

You can follow this for relation mapping hibernate-one-to-many Then declare your repository both for Drama and Character entities. Now, inside your Character-repo: say like this:

@Repository
public interface CharacterRepository extends  extends JpaRepository<Character, Long>{
 Character getByFirstName(String firstName); //i am assuming firstName is unique
}

You just need to call this method now to get details of Character and associated Drama entities.

Upvotes: 1

Related Questions