I have Entity which is defined with Value Object as field:
class CustomerEntity {
@Id
@Column(name = "customer_id", nullable = false)
private CustomerId customerId;
}
CustomerId is defined as Value Object:
class CustomerId implements Serializable {
private final String value;
//constructor, getter omitted for brewity
}
Repository is also easily defined and works well
interface CustomerRepository extends JpaRepository<CustomerEntity, CustomerId> {
Optional<Customer> findByCustomerId(CustomerId customerId);
}
How can I define Spring Data repository that I could search for customer on cusomter_id using String value?
Optional<Customer> findByCustomerIdStartsWith(String search);
Something similar to SELECT * FROM Customers where customer_id like '?%'? As far as I can understand I need to use custom JpaExpression with @Query annotation. But I can't figure out how can I cast Value Object field to string...
UPD 1 Have found workaround but it looks a bit ugly...
@Query("FROM CustomerEntity e where concat(e.customerId, '') like ?1%")
Optional<Customer> findByCustomerIdStartsWith(String search);
UPD 2
So I have found only way to make it work - to make Value Object as @Embeddable and use it within other objects as @Embedded with @AttributeOverride. Then solutions suggested in aswers will work. As a drawback I need to make empty constructor for Value Object.