The column name is not valid - error - java

I am getting this error while I am fetching value from resultset.
Error : com.microsoft.sqlserver.jdbc.SQLServerException: The column name company.short_name is not valid
CASE 1 :
select company.short_Name,location_name from company,location;
this query is executing fine on SQL Server but in my java code when I trying to retrieve value like resultset.getString("company.short_name"); that time this give the above error.
CASE 2 :
select company.short_Name short_name,location_name from company,location;
and retrieve value like resultset.getString("short_name"); than it work fine with both database MySQL and MSSQL.
I am migrating my database from MySQL to MSSQL.above case 1 is work fine in MySQL, but why it is not work in MSSQL?

resultset.getString("company.short_name"); is wrong here. No need to specifying fully qualified name while trying to fetch the data in your application. Just specify the column name like resultset.getString("short_name");.
Cause even though you say select company.short_Name ... query out the column name as short_Name since that's what defined in table schema.
In case both tables has same column which may result in ambiguity, give a alias name to the columns like
select company.short_Name as company_shortname,
location.short_Name as location_shortname,
location.location_name from company,location;

add the following to your application.properties file
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

When you do
select company.short_Name,location_name from company,location;
This query outs the column name short_Name and resultSet would also have short_Name
since the company.short_name doesnt exist you get an error.

the function resultset.getString(String columnLabel)
Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.
Parameters:
columnLabel the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column
Returns:
the column value; if the value is SQL NULL, the value returned is null
Throws:
SQLException - if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set
in the function resultset.getString(String columnLabel), the arg is a column name for executing sql, the statement select company.short_Name,location_name from company,location; will get a result set, which has table headers short_Name,location_name

Related

Data not fetching in Hibernate but fetching in SQL with same equal condition

SELECT * FROM <TABLE_NAME> WHERE date_date_time = '2025-04-01 10:16:12' is fetching data from SQL.
But in Hibernate, the same date ('2025-04-01 10:16:12') is not matching with the column and giving the null result set.
BUT, when I query using LIKE '2025-04-01 10:16:12', it is giving the result set same as I was getting in SQL query. There is nothing wrong with the code, it is working fine for all other columns except SQL COLUMN TYPE "DATETIME"

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.

HQL does not ignore values with blank string in column

I am trying to setup a query for my application to pull only values from a table that have a specific column set. Mostly this column will be null, but if you edit and save the item on the application end without putting anything in this field, then it saves a blank string to that database field.
I have tried the TSQL query:
SELECT * from TABLE where COLUMN is not NULL AND COLUMN != ''
This query returns the results I need, but when I run the same query in HQL:
SELECT OBJECT from TABLE where COLUMN is not NULL and COLUMN <> ''
Then it still contains the values that have a blank string in that column. I have tried this using HQL with the operators <> and !=, and have also tried converting it to a criteria object using Restrictions.ne("column","") but nothing seems to provide the result I need.
I tried Length as in the comments, but had no luck. With the length in the query hibernate generates the full query as so. the time_clock_id column is the one that i'm having the problem with. Hibernate is set to SQLServerDialect
select timezone0_.time_zone_id as time1_368_, timezone0_.version as version368_, timezone0_.modification_timestamp as modifica3_368_, timezone0_.time_offset as time4_368_, timezone0_.modification_user as modifica5_368_, timezone0_.name as name368_, timezone0_.description as descript7_368_, timezone0_.active as active368_, timezone0_.time_clock_id as time9_368_ from time_zone timezone0_ where timezone0_.active=1 and (timezone0_.time_clock_id is not null) and len(timezone0_.time_clock_id)>0
Rookie Mistake. There was another place within my action class where I was using a different query to build the select list in the application. This was resulting in the list being overwritten with all values instead of those that use blank. After snipping this duplication I can use the operator column <> '' and I am getting the correct results

Mysql&MSAccess: insert into table which have an incremented field

I am sorry if there is a duplicate but I tried all ways still I can't do the insertion.
I have a table with only two fields ( ID , Name )
When I run this SQL code it must be insert a new record and increment the ID field automatically
because it's auto increment but unfortunately don't work .
See the trail and errors :
MYSQL :
PreparedStatement pr = con.prepareStatement("insert into names(name) values(?)");
pr.setString(2,"Azad");
java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
insert into names(id,name) values(?,?)
java.sql.SQLException: No value specified for parameter 1
MS Access :
insert into names(name) values(?)
java.lang.ArrayIndexOutOfBoundsException: 1
insert into names(id,name) values (?,?)
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect
What's the reason of those errors ? and how to solve it ?
Thanks for suggestions and answers.
change pr.setString(2,"Azad"); to pr.setString(1,"Azad");
The first parameter is related to the position of the ? in the prepared statement, not the column index in the table.
java.sql.SQLException: No value specified for parameter 1. This is down to the fact that you have specified two parameters for the query. But have only specified one value, for the second parameter. If "ID" is an auto incremented column then you don't need to pass in a value. If its not then
pr.setString(1,IDVALUE);
pr.setString(2,"Azad");

Hibernate Projection returing result as Object

I want to write following sql using projection in hibernate
SELECT id, name, MAX(ver)
FROM bizterm
WHERE name ILIKE '%AB%'
GROUP BY name, id order by name asc
I have written following code
Session session=sessionFactory.getCurrentSession();
Criteria criteria=session.createCriteria(BizTerm.class);
criteria.add(Restrictions.ilike("name", searchString,MatchMode.ANYWHERE));
criteria.addOrder(Order.asc("name"));
ProjectionList projList=Projections.projectionList();
projList.add(Projections.max("ver"));
projList.add(Projections.groupProperty("id"));
projList.add(Projections.groupProperty("name"));
criteria.setProjection(projList);
In the table id,ver are the PKEY.
This query is running fine but in result I am getting value of only 3 column that is ver,id,name that too as Object,There are other column like status,level in the BizTerm table those value are not getting returned in the result set. How can I get those using projection? I tried projList.add(Projections.property("status")); but its not working.
I want this query to be executed
SELECT id, name,status,level MAX(ver)
FROM bizterm
WHERE name ILIKE '%AB%'
GROUP BY name, id order by name asc using projection in hibernate
Did like this..now its working no error
Criteria criteria=session.createCriteria(BizTerm.class);
criteria.add(Restrictions.ilike("bizTermName", searchString,MatchMode.ANYWHERE));
criteria.addOrder(Order.asc("bizTermName"));
ProjectionList projList=Projections.projectionList();
projList.add(Projections.max("bizTermversion"));
projList.add(Projections.groupProperty("bizTermId"));
projList.add(Projections.groupProperty("bizTermName"));
projList.add(Projections.max("status"));
projList.add(Projections.max("levels"));
projList.add(Projections.max("createDate"));
projList.add(Projections.max("modifyDate"));
Note: I dont need maximum of status,levels,createDate,modifyDate but if I am not writing them inside max function then getting following error
[Error Code: 0, SQL State: 42803] : column "levels" must appear in the GROUP BY clause or be used in an aggregate function
with max function everything seems to be fine

Categories