I Want to get a week value - java

I wan to get the value of only one week. I am using the following JPA query:
SELECT a
FROM questions.dao.hibernate.Questions a
WHERE (a.posted_date-CURRENT_DATE)>= 7
But I am getting an error message like
org.hibernate.QueryException: could not resolve property: posted_date of: questions.dao.hibernate.Questions [SELECT a FROM questions.dao.hibernate.Questions a WHERE (a.posted_date-CURRENT_DATE)>=7]
Please help me.
Thanks

This sounds pretty self-explanatory to me. The class questions.dao.hibernate.Questions does not have a property called posted_date (it may have the property - that would be a strange naming convention though - but Hibernate doesn't know it).

Related

jpa delete error using 2 #param with id and id

this is my code
picture of code
im dont understand, i think i already made similar in
'comment' and 'cart'
my problem in 'comment'
this is example my cart entity with attribute order_identifier and product_id and this is success to delete by this two attribute
but after when im made similar in comment this error show up
is this error because using two #param with 2 attribute id?
im not find the answer anywhere yet, whoever you help me to fix my concept im really appreciate it :')
Caused by: java.lang.IllegalStateException: Using named parameters for method public abstract void com.demo.lookopediaSinarmas.repositories.CommentRepository.deleteCommentByUserIdAndProductId(java.lang.Long,java.lang.Long) but parameter 'Optional[product_id]' not found in annotated query 'delete from comment where product_id:=product_id and user_id=:user_id'!
At line 20 by using : after = will resolve your problem.
Valid query is
delete from comment where product_id=:product_id and user_id=:user_id

Hibernate Query Parameters binding incorrectly

I have been trying to write a hibernate query and have been able to generate the hibernate query through the server, but the parameters in the parameterized query are not getting binded correctly i.e. if 23 is to be binded to parameter 1, but it's getting binded to paramter 3.
Any idea why this would be happening? What should be the points of errors that I should check to get to the root cause of this issue?
EDIT (Why the query is not here?)
The hibernate query is a complex one and it is not possible for me to publish it here. That's why my question is a general one. I just wanted to know, if someone else has also faced this issue, in which hibernate throws no errors and the query is generated successfully, but it's binding incorrect data to incorrect parameter.
I will try to give a rough idea through an example -->
e.g you write:
criteria.createAlias("D.A", "a", CriteriaSpecification.LEFT_JOIN, Restrictions.eq("a.delIndc", false));
criteria.createAlias("A.B", "ab", CriteriaSpecification.LEFT_JOIN,
Restrictions.sqlRestriction("sqlQuery"));
criteria.createAlias("ab.c", "abc", CriteriaSpecification.LEFT_JOIN, Restrictions.eq("c.dataEntry", "Star Wars"));
criteria.createAlias("abc.f", "f", CriteriaSpecification.LEFT_JOIN, Restrictions.eq("f.dataExit", "Star Trek"));
The query is getting generated successfully in the server (no error thrown), but while doing the parameter binding:
2017-12-09 12:09:21,396 TRACE [org.hibernate.jdbc.AbstractBatcher] (pool-14-thread-2) preparing statement
2017-12-09 12:09:21,396 TRACE [org.hibernate.type.BooleanType] (pool-14-thread-2) binding 'false' to parameter: 1
2017-12-09 12:09:21,396 TRACE [org.hibernate.type.BooleanType] (pool-14-thread-2) binding 'Star Trek' to parameter: 2
2017-12-09 12:09:21,396 TRACE [org.hibernate.type.BooleanType] (pool-14-thread-2) binding 'Star Wars' to parameter: 3
So, instead of binding 'Star Wars' to parameter 2, it's getting bind incorrectly to parameter 3.
So, in a nutshell, I want to know, why this might be happening? What are the checks I need to do to get to the root cause of this? And also if you faced this issue, how you found the issue and how you resolved it?
Hope this clarifies my query further. Please help and let me know, if you need any more info in this regard.
You can use 2 ways to bind the parameters in Hibernate - positional parameters, named parameters.
say you have query like:
select name from Emp where id=? and age=?
Here param at 0 position will represent id and param at 1 position will represent age. Here param order is important.
select name from Emp where id=:ID and age=:AGE
Here you can set the params using names ID and AGE, in this case order is not important as you are referring to params using names rather than using their positions.
Provide your code and the values you want to pass in the query.There can be a possibility that you might have modified the name of the property in the pojo/model class

DBUnit: dataset with MySQL YEAR type column

folks!
I'm having a problem using DBUnit:
In my test class, when I call DatabaseOperation.INSERT.execute(connection, dataSet), using a FlatXmlDataSet referencing a table that contains a column of type YEAR(4) - MySQL - I get the following:
(...)
Caused by: org.dbunit.dataset.datatype.TypeCastException: Error casting value for table 'Vehicle' and column 'LaunchYear'
at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:210)
at net.carroramafleet.ws.utils.DbUnitHelper.execute(DbUnitHelper.java:57)
... 33 more
Caused by: org.dbunit.dataset.datatype.TypeCastException: Unable to typecast value <2010> of type <java.lang.String> to DATE
at org.dbunit.dataset.datatype.DateDataType.typeCast(DateDataType.java:110)
at org.dbunit.dataset.datatype.DateDataType.setSqlValue(DateDataType.java:141)
at org.dbunit.database.statement.SimplePreparedStatement.addValue(SimplePreparedStatement.java:73)
at org.dbunit.database.statement.AutomaticPreparedBatchStatement.addValue(AutomaticPreparedBatchStatement.java:63)
at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:200)
... 34 more
Here's my dataset:
<dataset>
<Vehicle
ID="999"
LaunchYear="2010" />
</dataset>
As I have mentioned above, I have a YEAR(4) type column, LaunchYear, in the table Vehicle. And DBUnit can't insert this row because of this information can't be converted correctly.
I've already tried to replace this information using DBUnit's ReplacementDataSet, but I still have problem with TypeCastException. I really can't set a valid YEAR-formatted information.
Could somebody help me?
Thanks,
Jeff
This question is a bit old but I thought I would reply as I just ran into this same issue today.
I believe this is a bug in DbUnit. BTW, I'm using 2.4.9 but did check the release notes for later releases to see if this is mentioned as a bug fix.
The YEAR column is being converted into a java.sql.Date object. The initial bug is that there is no conversion from a simple string "2016" to a java.sql.Date. That leads to the TypeCastException. Changing this field to something like "2016-08-10" gets you past this initial error but leads to a SQLException when MySql attempts to truncate the Date into a integer or short.
The only way I have been able to work around this is to add specific code in the #SetUp or #Before methods to populate the table with initial data.

Hibernate - The column name att_id_1 is not valid

For some reason the query that is generated by hibernate is wrong. I have other tables/classes where the generated query is correct however for one table it is wrong. When I do a trace using MS Management Studio I see the following:
exec sp_prepexec #p1 output,NULL,N'select aprresourc0_.agrtid as agrtid1_14_, aprresourc0_.att_id_1 as att_id_2_14_,
aprresourc0_.att_id_2 as att_id_3_14_, aprresourc0_.att_id_3 as att_id_4_14_, aprresourc0_.att_id_4 as att_id_5_14_,
aprresourc0_.attribute_id as attribut6_14_, aprresourc0_.bflag as bflag7_14_, aprresourc0_.client as client8_14_, ...
from aprresourcepost aprresourc0_
where ...
select #p1
The columns att_id_1, att_id_2, att_id_3 and att_id_4 do not exist in the table or my Java class!
How do I fix this? Why does this happen?
It turns out to have been a simple mistake. I continued to work on the project and the next class I was looking at matched the query I was getting in my problem so I checked the code and found I was referencing the wrong class in my code!

Hibernate: column does not exist

When I try to load an Entity with Hibernate I get the following error in postgres-log:
ERROR: column appuser0_.device_token does not exist at character 35
STATEMENT: select appuser0_.id as id1_27_0_, appuser0_.device_token as device_t2_27_0_,....
The column device_token definitely exists - and if I copy-paste the whole logged statement and execute it in PGAdmin, I get the expected result.
So what do I forget? What is the difference between the Hibernate statement and the manually executed one?
This issue was caused by the multi tenant configuration so that the wrong DataSource has been chosen.
Depending on how you defined the query, the problem might be located somewhere else: For example, HQL Queries are using the "property-names" of the class, not the column names.
And if you have something like:
#Column("device_token")
private String deviceToken;
Then your HQL-Query should target "deviceToken" and not "device_token". We also encountered a similar error once: Hibernate was reporting "user_id" is missing, because we named the property "userId" with the underscored version for the column name only.
This might be not the problem for you but worth double checking it.

Categories