I was given a project at work including JPA / Hibernate
I have an object for example that is let s say described as
class SampleObject {
string value;
string othervalue;
}
and lets say that the value in the database is populated with 2 rows
table - SampleObject
row 1 - value = "21", othervalue = "some other value"
row 2 - value = "31", othervalue = "some other other value"
I can write a query that returns rows that follows
Query q = entityManager.createQuery("select s from SampleObject s where value = 21")
and I can write a query that returns rows that follows
Query q = entityManager.createNativeQuery("select * from dbo.SampleObject s where value = 21", SampleObject.class)
but what i cannot do is write those same queries as a string value and return data
ex 1:
entityManager.createQuery("select s from SampleObject s where value = '21'")
ex 2:
entityManager.createQuery("select s from SampleObject s where value = :val").setParameter("val", "21")
ex 3:
entityManager.createNativeQuery("select * from dbo.SampleObject s where value = '21'", SampleObject.class)
but as you can see this is stored as a varchar and IS REQUIRED to remain a string so i need to be able to query by a string
Disclaimer:
I am new to hibernate - very new .... very very new!
I should add in SQL SSMS I can directly query by string
I also tried using one of the suggestions below and for context it didn't resolve
CodePudding user response:
You could use:
entityManager.unwrap(Session.class)
.createQuery("<your query>")
.setParameter("identifier", "3", StringType.INSTANCE);
That implementation works with the exact type that you want to pass.
CodePudding user response:
I have to apologize I had an old connection string
and I was directly querying the current data in ssms and the connection string was grabbing old data in the java
my fault it was working the whole time
walking through the debugger showed old data
thank you for your help