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.
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 need to create a temporary table with hibernate and then insert data from a list into this table. here is what I mean :
String hql = "create temporary table temp (id int)";
session.createQuery(hql);
and inside this for loop i insert the data from a list into table 'temp' :
for(Example example : examples) {
hql = "insert into temp (" + example.getId() + ")";
query = session.createQuery(hql);
query.executeUpdate();
}
when i fill the table with the valuse from examples (which is a list), then i can use this table for another query. create and insert to this table has to be done automatically in my code and cant do it in database manually because the list 'examples' is made is the code.
this is what i think about the code, but it does not work. can anybody tell me how i can do this in correct way? thanks.
I don't think Hibernate Query Language manage "create" operation. Probably you need to run a native query in using createNativeQuery if you are using EntityManager or createSQLQuery if you are using Hibernate Session:
String hql = "create temporary table temp (id int)";
session.createSQLQuery(hql).executeUpdate();
I have 2 tables TABLE1 and TABLE2.Table1 is having name and Table2 is having email and Phone.
To get the name,email and phone,I query as below
query = entityManagerUtil.createNativeQuery("select s.Name,c.Phone1,c.Email1 from Table1 s,Table2 c where c.id= s.NodeID and s.NodeID =21")
Now my next requirement is to update name,email and phone.As these parameters are present in different tables so I am searching for single query which will update 2 tables.Unfortunately I am using sql server and there is no way to update 2 tables using single query
So I am thinking to use #Transactional and 2 queries to update 2 tables like the follow
#Transactional
public void updateDetails()
{
Query query1= entityManagerUtil.entityManager.createNativeQuery("update Table1 set Name='' where id in (select NodeID from Table 2) and NodeID=21");
Query query2= entityManagerUtil.entityManager.createNativeQuery("update Table2 set Email='' and phone1='' where NodeID in (select id from Table 2) and NodeID=21");
query1.executeUpdate();
query2.executeUpdate();
}
Is there any other better way to update 2 tables?
you can use JDBCTemplate
http://sujitpal.blogspot.com.es/2007/03/spring-jdbctemplate-and-transactions.html
It allows to do multiple queries with one connection, so you save some time instead of doing it twice.
Why don't you use Hibernate entities for that. Just load the entities associated with Table1 and table2, modify them and let the automatic dirty checking mechanism to update the tables on your behalf. That's one reason for using an ORM by the way.
I'm building a select that has to get me all distinct values from a table.
The sql I would normally write would look like this: "SELECT DISTINCT ARTIST FROM MUSICLIB"
However, ebean is generating the following: "SELECT DISTINCT ID, ARTIST FROM MUSICLIB"
The finder is as such:
find.select("artist").setDistinct(true).findList();
I've found that ebean is generating this ID on every single query, no matter what options I set.
How do I accomplish what I'm looking for?
You can't do that, Ebean for objects mapping requires ID field, and if you won't include it you'll get some mysterious exceptions.
Instead you can query DB without mapping and then write your SQL statement yourself:
SqlQuery sqlQuery = Ebean.createSqlQuery("SELECT DISTINCT artist FROM musiclib");
List<SqlRow> rows = sqlQuery.findList();
for (SqlRow row : rows) {
debug("I got one: " + row.getString("artist"));
}
Of course if artist is a relation, you need to perform additional query using list of found IDs with in(...) expression.
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.