Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data find by inner relation

My question is about the way Spring data is generating the query .

I have two entities : Message , Sender

@Entity
public class Message extends BaseEntity {
 @ManyToOne
 protected Account sender;
}

I have a call to

messageDao.findBySenderId(Long id)

The result is query all columns from the two two table with a left outer join between the two tables , but my expectation was simply to just select from message table where sender_id = the passed value.

So is there a way to force selecting only the first message entity and not to join with the other one? I want simple condition in the where clause by using findBy not custom @Query

like image 796
emad omara Avatar asked Jan 20 '17 07:01

emad omara


1 Answers

You will need a repository like (untested) :

@Repository
public interface MessageRepository extends JpaRepository<Message, Long> {
      Message findFirstBySenderId(Long id); 
}

See repositories.query-methods

like image 64
Essex Boy Avatar answered Oct 18 '22 19:10

Essex Boy