How to write A crud Query which can only fetch two colmun from my db?
suppose there are 4 columns in my db - (id,city,pincode,state) and i need only city,picode from crudRepository query
For example, it can be done like this:
SELECT city, pincode
FROM tableName;
If it is about SQL, then will help you:
SELECT table_name.city, table_name.pincode FROM table_name;
It works if there is already a connection to DB.
Related
I have various filter criteria :- Name, Id, value
database columns are:- StudentName,FatherName studentId height , weight
I want to filter[ studentId and (weightvalue) and (studentName like(Name) or FatherName like (Name))]
None of the filter parameters are mandatory.
can anyone help me to know how to write query for this. as when i am writing it shows multilr OR Conditions are not allowed in Mongo DB
Is it possible to create native SQL-queries for saving, updating or removing certain entities from the database via hibernate ?
What i am searching for is pretty much this...
String sqlQuery = session.save(listOfEntities);
session.execute(sqlQuery);
sqlQuery : "insert into players(id,...) values((10,...),(11,...))"
Why would you need the statements? Just doing session.persist(entity) will execute the statement for you already.
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 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.
I want fetch last record using hibernate criteria
Can u help me?
What do you mean by hibernate criteria in mySQL?
hibernate criteria is used in java to retrieve persistant class objects.
in my SQL you can run following query
SELECT col1, col2, ... FROM yourtable ORDER BY xyz DESC LIMIT 1