JDBC - select query from a materialized view does not work - java

I'm trying to create a select query from a materialized view using jdbc. Here is my actuall code:
Query q = entityManager.createNativeQuery("SELECT v.name FROM my_materialized_view v");
List<Object[]> authors = q.getResultList();
But when I run this code I get the following error:
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement
There's an specific to select materialized views in this context? Because if I use that code querying from a normal table it works perfect...

dont use SQL ALIAS
try
"SELECT name FROM my_materialized_view"

Related

query for retrieving data from view is not working

I created a view as an integration of three tables but when I retrieve the data in the view using query ... the system stop working
I create a view using this query:
PreparedStatement statement1 = (com.mysql.jdbc.PreparedStatement) con1.prepareStatement("
CREATE VIEW mcps1_patients_view AS
SELECT a.PatientID
, PatientGender
, PatientDateOfBirth
, PatientMaritalStatus
, a.Ad_ID
, Ad_StartDateAndTime
, Ad_EndDateAndTime
, d.ICD10CM_Code
, d.PrimaryDiagnosisDescription
From patientcorepopulatedtable p
JOIN admissionscorepopulatedtable a
JOIN admissionsdiagnosescorepopulatedtable d
");
result1 = statement1.execute();
System.out.print("connected");
and I want to make a query on the created view ...
PreparedStatement statement4 = (com.mysql.jdbc.PreparedStatement) con2.prepareStatement(" SELECT * From mcps2_patients_view where P_ID= ? ");
statement4.setString(1, Hospital2_id);
result4 = statement4.executeQuery();
System.out.print("connected \n")
but the last query is not working its stopped working
what is the problem ???
and how can I solve it?
I think you are confusing some things, the query sintax you are passing at the creation of the view should be native SQL. From the javadoc
Creates a PreparedStatement object for sending parameterized SQL statements to the database
What you pasted there seems to be JPQL. The SQL Join statement needs also the ON part to specify what column the join is being done. Something like: SELECT... FROM patient p join patient_admission pa on pa.patient_id = p.id
Is that a typo? I expect your code creating the view thows an exception.

sql query very slow in Hibernate, fast on mysql

I have a really simple query to select all rows from a table. In hibernate it always takes about 17-18 seconds. I specified, show_sql to be true. I copied the sql command directly to mysql and it took 0.00022 seconds...
this is the query:
SELECT
organizati0_.id AS id1_5_,
organizati0_.adminList AS adminLis2_5_,
organizati0_.demoAccount AS demoAcco3_5_,
organizati0_.disabled AS disabled4_5_,
organizati0_.enduserCredits AS enduserC5_5_,
organizati0_.name AS name6_5_,
organizati0_.pushNotificationRoom AS pushNoti7_5_,
organizati0_.totalLicenses AS totalLic8_5_,
organizati0_.usedLicenses AS usedLice9_5_
FROM
organization organizati0_
this is the code for the query:
Session session = ThreadSessionHolder.getThreadSession();
Query query = session.createQuery("from Organization ");
List list = query.list();
return list;
any ideas? this is driving me nuts
I found the issue.
I was performing an initialization operation in the constructor which shouldn't be done when selecting all

Hibernate Criteria for sum(distinct clicks)

I am unable to come up with the criteria which can perform sum(distinct clicks) in query. I am getting result with simple sql query but cannot make criteria to do the same.
Currently i can just do
.add(Projections.property("clicks").as("clicks"))
A simple sql query is like this
select sum(distinct clicks) as clicks, source as source, sum(jobs_sent) as jobsSent from tbl_click_count where date= '2016-09-04' and domain = 'somename' and whitelabel = 'somename' group by source, whitelabel, domain order by id desc
and my criteria is like
Criteria criteria = getSessionFactory().createCriteria(FeedClickCountModel.class)
.add(Restrictions.ge("date", startDate))
.add(Restrictions.le("date", uptoDate))
.setProjection(Projections.projectionList()
.add(Projections.sum("jobsSent").as("jobsSent"))
.add(Projections.groupProperty("source").as("source"))
.add(Projections.property("whiteLabel").as("whiteLabel"))
.add(Projections.property("domain").as("domain"))
.add(Projections.property("totalJobs").as("totalJobs"))
.add(Projections.property("clicks").as("clicks"))
// ^This i want is sum(distinct clicks)
.add(Projections.property("date").as("date"))
.add(Projections.property("id").as("id")))
.setResultTransformer(Transformers.aliasToBean(FeedClickCountModel.class));
Is there any way to do this without sqlprojection.
.add(Projections.distinct(Projections.property("clicks")));
Like this?
Projections.sum(Projections.countDistinct(propertyName));
Try this.
Without projections, you will need to use hibernate SQL or native SQL queries.

How to write the HQL query for database view in mysql

I am using netbean and followed below steps to execute HQL query for table
Did Reverse Engg
Generated mapping and POJO
Query query = session.createQuery("from MasUser as masUser where masUser.userName = '" + userName + "'");
masUser = (masUser) query.uniqueResult();
Now I am able to execute and got result.
Then I have created view for this table
CREATE VIEW test_view AS SELECT user_name, address FROM mas_user
Now I need to execute test_view in hibernate HQL.
Please provide the steps.
If you created the view in the database, the process of querying a view is exactly the same as working with a table. Simply create an entity (POJO) for the view, attach the mapping annotations and then create and execute a query. Repeat the same process that you did for your MasUser table.

how to execute the stored procedure in hibernate 3

I am new in hibernate. I am using hibernate 3 in my application using hibernate annotations , I am developing application in struts 1.3.
My question is :
I have googled a lot but could not understand how to call a stored procedure in hibernate using annotations , I have a simple scenario : suppose I have 2 fields in my jsp say 1) code 2) name , I have created a stored procedure in database for inserting those records into table. Now my problem is that how to execute it
List<MyBean> list = sessionFactory.getCurrentSession()
.getNamedQuery("mySp")
.setParameter("code", code)
.setParameter("name", name)
I don't know the exact code how to do this. But I guess something like that actually I come from jdbc background therefore have no idea how to do this and same thing I want when selecting the data from database using stored procedure.
Hibernate provides many simple ways to call a SP like
Native SQL
Named Query in native SQL as Annotation/XML mapping file
Following link shows how each of above can be implemented
http://www.mkyong.com/hibernate/how-to-call-store-procedure-in-hibernate/
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html#sp_query
Sample to run native SQL query using hibernate:
Session session = getSession();
SQLQuery sqlQuery = session.createSQLQuery("SELECT COUNT(id) FROM tableName WHERE external_id = :external_id");
sqlQuery.setParameter("external_id", idValue);
int count = ((BigInteger) sqlQuery.uniqueResult()).intValue();
releaseSession(session);
You can execute your stored procedure using Hibernate's SQLQuery with the same SQL as when you call it against the database. For example, in postgreSQL:
String query = "select schema.procedure_name(:param1)";
SQLQuery sqlquery = sessionFactory.getCurrentSession().createSQLQuery(query);
sqlquery.setInteger("param1", "this is first parameter");
sqlQuery.list();
Hope it helps.

Categories

Resources