JPA 多对多关联 中间表带有属性 两个外键作为中间表的联合主键时 直接操作中间表查询修改的方法

https://blog.csdn.net/u013756305/article/details/83542758

1)、建立联合主键

public class WorkDateTimeProductKey  implements Serializable{

       private static final long serialVersionUID = 3586335994284551414L;

       private Product product;

       private WorkDateTime workDateTime;

}

2)、中间表的仓库

public interface  WorkDateTimeProductRepository extends JpaRepository<WorkDateTimeProduct, Long> {

      WorkDateTimeProduct findByWorkDateTime_IdAndProduct_Id(long workDateTimeId ,long productId);

}

3)、测试代码

@SpringBootTest

@RunWith(SpringRunner.class)

public class WorkDateTimeProductRepositoryTest {



       @Autowired

       WorkDateTimeProductRepository WorkDateTimeProductRepository;



       @Test

       public void findByWorkDateTimeIdAndProductIdTest(){

               WorkDateTimeProduct  workDateTimeProduct =WorkDateTimeProductRepository.findByWorkDateTime_IdAndProduct_Id(12l, 13l);

               workDateTimeProduct.getAmount();

              System.out.println(workDateTimeProduct.getAmount());

       }

}

WorkDateTime

Product

WorkDateTimeProduct

Last updated

Was this helpful?