so I want to search one value in 3 columns in the MYSQL table and write JPA query like this:
SELECT * FROM table WHERE 123 IN(col1, col2, col3);
so the problem is col2 and col3 are nullable they may have null value so the type of these columns is Option[String] in the entity class.
so I tried something like this in JpaRepository:
@Repository
trait ContactRepo extends JpaRepository[Contact, Long] {
def findFirstByCol1OrCol2OrCol3(c1: String, c2: scala.Option[String], c3: scala.Option[String]): Contact
}
and while calling this:
val optionData: scala.Option[String] = scala.Option("1234567890")
val someData: scala.Some[String] = scala.Some("12345678980")
val simpleStringData: String = "1234567890"
val contact = contactRepo.findFirstByCol1OrCol2OrCol3(simpleStringData, optionData, optionData)
I tried all variables(optionData, someData and simpleStringData) but I keep getting this error:
Parameter value [1234567890] did not match expected type [scala.Option (n/a)]
I tried OnIsNull also but still not working
I don't know what I'm missing I think its some small mistake that I don't understand
problem: how can i write JPA query for SELECT * FROM table WHERE 123 IN(col1, col2, col3);
Note: type of col2 and col3 are scala.Option[String]
I know this is old question but I faced the same problem today and I solved it by checking the exact value that required to the function. When you create JPA query with option type then query mapped variable in entity class is also defined as option so while passing values to the query method you need to pass it like:
Option[Option[String]]
so in your method it should be:
trait ContactRepo extends JpaRepository[Contact, Long] {
def findFirstByCol1OrCol2OrCol3(c1: String, c2: Option[Option[String]], c3: scala.Option[String]): Contact
}
and while calling:
val optionData = Some(Some("1234567890"))
val someData= Some(Some("1234567890"))
val simpleStringData: String = "1234567890"
val contact = contactRepo.findFirstByCol1OrCol2OrCol3(simpleStringData, optionData, someData)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With