ยท
Sometimes you need to use several attributes of an entity to design your DB primary key.
JPA composite key serves that purpose.
Let's see how to implement such composite key !
1๏ธโฃ ๐๐จ๐ฎ ๐๐๐๐ข๐ง๐ ๐ญ๐ก๐ ๐๐จ๐ฆ๐ฉ๐จ๐ฌ๐ข๐ญ๐ ๐๐๐ฒ๐ ๐๐ฅ๐๐ฌ๐ฌ ๐๐จ๐ซ ๐ญ๐ก๐ ๐ข๐:
public class ๐๐จ๐จ๐ค๐๐ implements Serializable { private String ๐๐ฎ๐ญ๐ก๐จ๐ซ; private String ๐ง๐๐ฆ๐; // standard getters and setters }
2๏ธโฃ ๐๐จ๐ฎ ๐ฎ๐ฌ๐ ๐ข๐ญ ๐๐ฌ ๐ข๐๐ ๐ข๐ง ๐ฒ๐จ๐ฎ๐ซ ๐๐ง๐ญ๐ข๐ญ๐ฒ:
@Entity public class Book { @๐๐ฆ๐๐๐๐๐๐๐๐ private ๐๐จ๐จ๐ค๐๐ ๐ข๐; private String genre; private Integer price; //standard getters and setters }
3๏ธโฃ ๐๐จ๐ฎ ๐๐ข๐ง๐๐ฅ๐ฅ๐ฒ ๐ฆ๐๐ค๐ ๐ฒ๐จ๐ฎ๐ซ ๐๐๐ฅ๐ฅ๐ฌ๐ ๐ญ๐จ ๐ญ๐ก๐ ๐ซ๐๐ฉ๐จ๐ฌ๐ข๐ญ๐จ๐ซ๐ฒ:
@Repository public interface BookRepository extends JpaRepository<Book, ๐๐จ๐จ๐ค๐๐> { List<Book> findById๐๐๐ฆ๐(String name); List<Book> findById๐๐ฎ๐ญ๐ก๐จ๐ซ(String author); }