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.
Related
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"
I am using a Movie Database that will link to the front-end of my website. i am using Java Spring Boot to link the Database. My question is, How can i join a Movies table and Reviews table>]?
try {
String getAllActorsbyMoviesidQuery = "SELECT * FROM tblMovies JOIN tblReviews ON tblReviews.actor_id = tblMovies.actor_id";
statement = dbConnection.createStatement();
ResultSet resultSet = statement.executeQuery(getAllReviewsbyMoviesidQuery);
Joining the 2 tables on actor_id is not probably the suitable way.
I guess there exists inside tblReviews a column like movie_id that refers to the id column of the table tblMovies:
SELECT * FROM tblMovies JOIN tblReviews ON tblReviews.movie_id = tblMovies.id
change the column names to the actual ones.
If you want the reviews for a particular movie, then add a WHERE clause, like:
WHERE tblMovies.id = ?
or
WHERE tblMovies.title = ?
I am absolutly new in Hibernate and I have the following problem.
I have this standard SQL query:
SELECT count(*)
FROM TID003_ANAGEDIFICIO anagraficaEdificio
INNER JOIN TID002_CANDIDATURA candidatura
ON (candidatura.PRG_PAR = anagraficaEdificio.PRG_PAR AND candidatura.PRG_CAN = anagraficaEdificio.PRG_CAN)
INNER JOIN TID001_ANAGPARTECIPA anagPartecipa ON(anagPartecipa.PRG_PAR = candidatura.PRG_PAR)
INNER JOIN anagrafiche.TPG1029_PROVNUOIST provNuovIst ON (provNuovIst.COD_PRV_NIS = anagPartecipa.COD_PRV_NIS)
WHERE anagraficaEdificio.FLG_GRA = 1 AND provNuovIst.COD_REG = "SI";
This works fine and return an integer number.
The important thing to know is that in this query the only
parameter that can change (inserted by the user in the frontend of a webappplication) is the last one (this one: provNuovIst.COD_REG = "SI").
So, the application on which I am working use Hibernate and the requirement say that I have to implement this query using Hibernate Native SQL, I have found this tutorial:
http://www.tutorialspoint.com/hibernate/hibernate_native_sql.htm
that show this example:
String sql = "SELECT * FROM EMPLOYEE WHERE id = :employee_id";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Employee.class);
query.setParameter("employee_id", 10);
List results = query.list();
that, from what I have understand (correct me if I am doing wrong assertion), involves the use of an Employee model class. So th prvious query first define the query (using the :param_name syntax for the parameter), then create an SQLQuery Hibernate object, add the class used for the result, set the previous parameter neam and finally obtain a List (that I think Hibernate create as something like an ArrayList) with the retrieved object.
My problem is that I simply I have to obtain an integer value (because I have a SELECT count(*), so I will obtain an integer value and not a set of rows).
So how can I correctly use the Hibernate Native SQL to implement my SQL query into my Hibernate repository class?
Use SQLQuery.uniqueResult to retrieve a single value from the query:
String sql = "SELECT count(*) ...";
SQLQuery query = session.createSQLQuery(sql);
// set parameters...
int count = ((Number)query.uniqueResult()).intValue();
how to add a set parameter() metheod inside the inner query in hibernate?
I have try to do like this but already have a errors
this is my code
Query query=session.createQuery("select eq.euipmentName,eq.type from Euipment eq where eq.id in(select euipment from Quotation qt where qt. supQuotation=:ids)");
query.setParameter("ids",id);
list = (List<Euipment>)query.list();
I've done some corrections about your query:
1. qt. supQuotation has a space, I've removed
2. euipment in you sub query haven't alias, I add qt
String hql =
"select eq.euipmentName,eq.type " +
" from Euipment eq " +
" where eq.id in (select qt.euipment from Quotation qt where qt.supQuotation = :ids)";
Query query = session.createQuery(hql);
query.setParameter("ids",id);
list = (List<Euipment>)query.list();
Tell me, if it's OK
If not OK, please post here the error, and check if you have put in hibernate mappping file your classes
From Hibernate Documentation:
Execution of native SQL queries is controlled via the SQLQuery
interface, which is obtained by calling Session.createSQLQuery().
createQuery() creates Query object using the HQL syntax.
createSQLQuery() creates Query object using the native SQL syntax.
So replace createQuery with createSQLQuery for native SQL query.
Try with Criteria
Criteria c = getSession().createCriteria(Euipment.class, "e");
c.createAlias("e.quotation", "q"); // inner join by default
c.add(Restrictions.eq("q.supQuotation", id));
list = (List<Euipment>)c.list();
I am trying below query in HQL but I get no results. Can someone help me on why I don't get any results? I tried to query the DB directly (please see SQL below) and i get 12 records. But HQL gives me 0 records.
"cause" I input the following String - "'XXX1','YYY 2'"
DB I use is Pracle 11g.
String queryStr = "from DefectsTran t join t.defects d where d.releaseName=:rel and t.defectCause in :cause and t.latestRecord=:lastrec";
Query q = session.createQuery(queryStr);
q.setString("rel", release);
q.setString("cause", filter2);
q.setString("lastrec", "Y");
SQL query that works fine when I use in TOAD.
select count(*)
from QC10.defects_tran t
inner join QC10.defects on DEFECT_ID_FK_DT = RECORD_ID
where
DEFECT_CAUSE in ('Data Request Issue', 'Functioning as Expected', 'User Education Required', 'Test Script Incorrect', 'Test Specific')
and t.latest_record = 'Y'
Instead of q.setString("cause", filter2); use q.setParameterList("cause", filter2);. filter2 has to be of a Collection subtype. Please read more about other overloading available for setParameterList: http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html