I have two tables that I want to join using hibernate. The join column, as represented in my model is a String (its a varchar(10) in my database). When I run the HQL query, what I see is the following error, "conversion failed when converting the varchar value 'AS00' to data type int. "AS500" is the first value of join column in the first row.
I do not know why hibernate is doing this. My join column is not an int. I have checked both models corresponding to my tables and they are both defined as Strings. Is there some kind of restriction on the data types that can be used for join columns?
Please post both your model and the hql query.
If I had to take a guess (and that's all any of us can do without specifics), I would say that your hql query does not use .setParameter and it does not have single quotes around the string value in your query... so it is trying to implicitly convert the value to int.
Example that would cause this error:
Query query = session.createQuery("from Person where name = bob");
Related
I am running into the issue where I am making a query from one table and trying to map and store the results it returns into another table. There is one field in my new table that's unique to that table while the other values being mapped have the same name from the original mapped table. Now the problem I have is that hibernate is saying that a unique field is not part of the result set so it can't map it. What can I do to let hibernate know that this field is only part of the new table and not part of the table it' gets mapped from?
Table A
name
age
Table B
name
age
height
#Query(
value = "select name, age, from table A group by (name, age)",
nativeQuery = true
)
List<TableB> mapData();
Hiberate Returns
The column name height was not found in this ResultSet.
I must remind you that such a return value can easily break the code design. For query operations on the same data table, the best option is to return the same pojo.
If you need to convert TableA to TableB in other logic, maybe you can search for "java object copy", for example: http://dozer.sourceforge.net/
I have a scenario, where the user will provide a Select statement. I need to find out the columns (their names, type, and other metadata), but I do not want to execute the query.
I know that I can execute the query and figure it from the ResultSet, but if the query returns many rows, then it may not be a good approach.
For example, consider the query
select name, age from people where people.dob = '1976';
Is there a way of getting the projected column metadata (i.e. metadata of name and age columns) without executing the query?
Solved this by using a PreparedStatement. A PreparedStatement does not execute the statement.
I have a dynamic query that is being generated in base of some data passed to a function. For this reason, I don't actually know how many columns I will have in my result. (The query is a pivot of Oracle 11G).
I know that all the generated columns will be numeric items, there's a fixed column that will be always a string.
How can i get a map<String, List<Double>> from hibernate mapping?
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-select
Just create select in HQL and get list of map.
As a example:
select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n ) from Cat cat
Using native query tutorial:
http://www.flexjunk.com/2008/03/07/native-sql-in-hibernate/
Background: I am mapping Sybase stored procedure return values to java objects using Spring.
For example I map a Sybase datatype of varchar as a String type in Java, and a Sybase datatype of int as an int type in Java, etc.
I have come across the following code in one of the stored procedures:
SELECT DISTINCT
A.Col1 AS val1,
A.Col2 AS val2,
NULL AS someVal,
A.col3 AS val3,
...
A.col9 AS val9
FROM #SomeTable A
ORDER BY Col2, Col3
I have 2 related questions:
What does Null mean in this scenario? I am confused as to what is happening here.
I am able to determine the data type of Col1, Col2, etc. of course by looking at the table definition of Table A defined earlier in the stored procedure. Thus I know what datatype I can define in my Java object for val1, val2, etc.. But what about "someVal"? What datatype mapping am I supposed to perform for this Null value?
I am fairly inexperienced in SQL. Perhaps the answer is much simpler than I realize.
This creates a column with NULL values in all rows. This trick is useful when
The reader of your query results expects a column someVal to be there, treating situations when this column is missing as errors, or
Your query is part of a UNION ALL query inside a GROUP BY query, with other queries filling in values for NULLs.
Here is an example of the later situation:
SELECT -- This query flattens the results of the two sub-queries
document_id
, MIN(approval_date) as approval_date
, MIN(availability_date) as availability_date
FROM (
SELECT -- This subquery supplies approval_date
document_id
, MAX(approval_date) AS approval_date
, NULL AS availability_date
FROM document_approvals
GROUP BY document_id
UNION ALL
SELECT -- This subquery supplies availability_date
document_id
, NULL AS approval_date
, MAX(availability_date) AS availability_date
FROM document_approvals
GROUP BY document_id
)
GROUP BY document_id
You will have a column called someVal with NULL as the value for each row.
With a ResultSet, you can use getString(int) or getString(String) which states
Returns: the column value; if the value is SQL NULL, the value
returned is null
You can choose any reference type you want, including Void, to map this column. Or don't use any, ie. don't map it. Depends on your requirement.
I have a sql statement with multiple joins; sometimes there are records resulting from the joins and sometimes not. It is not an error when the joins are empty.
Is there a way to test whether a particular join is empty without having to trap the exception occurring when a resulting column is not found in the result set?
In other words, if, using JDBC, I execute the following sql statement:
select * from AA left outer join BB on AA.keyField = BB.keyField
where keyField = "123"
I would like to know whether that join found any fields without having to trap an exception. If I then execute the java statement:
String valueString = rs.getString("BB.keyField");
I get an exception. I cannot compare rs.getString("BB.keyField") to null, because the getString throws an exception if it did not find any values in the join.
I hope this makes the question more clear, and I apologize for not having expanded on it more in the first place.
The query won't return columns with such names. It will just use the simple names of the columns (name, keyField, etc.). Execute the query inside your database query tool, and you'll see which names are assigned to the retrieved columns.
You should never do a select *, and always select specific columns. And you should assign aliases to columns if two columns have the same name:
select AA.id as aId, AA.name as aName, BB.id as bId, BB.name as bName from ...
And, then, you can safely use
rs.getString("aId");
rs.getString("bName");
...
Also, the exception thrown contains a message, which should help you identify the problem. Read it. And if you don't understand it, post it.