简体   繁体   中英

How to find by on the reference key related other table field(using JPA)?

For example, table A has primary key id, referenced id is bid; table B also has primary key id equal to bid, filedB and more.

My question is that by using Java JPA how to find by B.fieldB?

ADAO extends CrudRepository{
   findBy(____);//find by referenced bid related table's fieldB.
}

Entity class example:

@Entity @Table(name="A") public class A implements Serializable{ id; bid; };

@Entity @Table(name="B") public class B implements Serializable{ id; fieldB; } 

According to @manish suggestion, that's worked on my side.

First off, using annotation to define the relation ship between A and B;

@Entity @Table(name="A") public class A implements Serializable{ id; bid;
@JoinColumn(name = "bid") @ManyToOne B b;
 };

@Entity @Table(name="B") public class B implements Serializable{ id; fieldB; } 

Then findByBFieldB(string fieldB) in my A-Repository;

Another way: using annotated query as following:

 @Query(value = "SELECT a.* FROM A a LEFT JOIN B b ON a.bid=b.id WHERE b.field LIKE :someString", nativeQuery = true);

I wish I could do as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM