Amit
Amit

Reputation: 656

How to use part of composite key in JPA repository methods?

I have a class with embedded Id

@Entity
@Table(name="account_table")
Public class AccountLink {

@EmbeddedId
private AccountLinkKey accountLinkKey;
//Rest of code
}

@Embeddable
Public class AccountLinkKey {
//Rest code here

@ColumnName(name="i am hiding")
private String segAccount;

@ColumnName(name="i am hiding")
private String secAccount;
//Rest of code
}

Now in my repository I want to have a method to search only by secAccount, so how should I write findBy..

List<AccountLink> findBy...(String secAccount); 

I tried findByAccountLinkKeySecAccount(String secAccount) but still no success.

Upvotes: 3

Views: 25511

Answers (4)

vvauban
vvauban

Reputation: 475

I used this example: https://www.baeldung.com/spring-jpa-embedded-method-parameters

You define the Composite Key Class for the id:

@Embeddable
public class BookId implements Serializable {

    private String author;
    private String name;

    // standard getters and setters
}

You use it as id in your entity:

@Entity
public class Book {

    @EmbeddedId
    private BookId id;
    private String genre;
    private Integer price;

    //standard getters and setters
}

You finally make your calls to the repository:

@Repository
public interface BookRepository extends JpaRepository<Book, BookId> {

    List<Book> findByIdName(String name);

    List<Book> findByIdAuthor(String author);
}

Example of code where I used it: Quiz Engine with JetBrainsAcademy

Upvotes: 7

Manoj meghwal
Manoj meghwal

Reputation: 1

It's useful when we used for delete method for @Embeded for keys with @Entity.

  1. Your keys should append '_' with the variable of Embeddable class. example: pkId_firstName (pkid is key of @Embeded and firstName is from Embedded class variable.)
  2. it should traverse with keys to a variable with 'underscore symbol. since without '_' underscore it does not work.

Upvotes: 0

Dhwanil Patel
Dhwanil Patel

Reputation: 2573

Normally when you want to perform some action on embedded columns in JPA,You bind your columns with underscore('_').

Example :

findBy<Entity_Class_Column_Name>_<Embaded_Class_Column_Name>(...)

Upvotes: 1

Reaz Murshed
Reaz Murshed

Reputation: 24211

I have rewritten your class and could add repository function as well. Here's my implementation which you might consider taking a look at.

Here's the class AccountLink.java.

@Entity
@Table(name = "account_table")
public class AccountLink {

    @Column(name = "id")
    public Long id;

    @EmbeddedId
    private AccountLinkKey accountLinkKey;

    @Embeddable
    public class AccountLinkKey {
        @Column(name = "seg_account")
        private String segAccount;

        @Column(name = "sec_account")
        private String secAccount;
    }
}

And the AccountLinkRepository.java.

public interface AccountLinkRepository extends JpaRepository<AccountLink, Long> {
    AccountLink findAccountLinkByAccountLinkKey_SecAccount(String secAccount);
}

I have added an id column in the AccountLink class as the primary key of the account_table. You might need something like this as well for your table.

Upvotes: 4

Related Questions