Concatenation of two string field in db context in entity framework - entity-framework-4.1

I want to perform the following query in dbcontext(Entity framework 4.1)
Please Help .
Select empcode+'-'+Empname as empName from tblemployee

from employee in context.Employees
select new { empName = employee.empcode + employee.Empname }

Related

how to write hql join query

I'm new from HQL any one Please tell me how to write this Query in HQL.
My SQL Method is
public Integer validateEditDate(int id, String date);
My SQL Query is
SELECT
count(a.activity_task_id)
FROM activity_task_details AS a
JOIN milestone_activity_details AS b
ON a.milestone_activity_id = b.milestone_activity_id
WHERE a.milestone_activity_id = 17
AND DATE(a.task_end_date) > '20161229';
Where id and date is dynamic
Please convert this sql query in Hql query
To convert a SQL query in HQL you must define, as first, classes about db table mapping.
You have these two tables:
activity_task_details
milestone_activity_details
So you must have two classes, as follow:
ActivityTaskDetails
MilestoneActivityDetails
So, you must map your fields as properties. Suppose you have done this, your query will became:
SELECT
count(a.activity_task_id)
FROM ActivityTaskDetails a, MilestoneActivityDetails b
WHERE a.milestone_activity_id = b.milestone_activity_id
AND a.milestone_activity_id = :paramId
AND a.task_end_date > :paramDate;
Pay attention, if possible, your task_end_date in class ActivityTaskDetails declare as date
To execute your query:
String hql = "Your HQL query write upper";
Query q = session.createQuery(hql);
q.setString("paramId", id);
q.setDate("paramDate", date);
q.list();
session is a variable to get session factory object

Sql- how to use relational field in condition

I have three tables which is mapped like this: paymentDetails <-employee<-designation.
Now I have to get datas from paymentDetails table by particular designation of employee..
select *
from paymentDetails
where payment_date=date and employee.designation.desig_id=2;
And I am using Yii2 framework How can I achieve this in Yii2.
I get unknown column error. How to resolve this ?
$command = Yii::app()->db->createCommand()
->select(*)
->from('paymentDetails')
->where('payment_date=date')
->andWhere('employee.designation.desig_id=2')
select *
from paymentDetails
where payment_date=date and employee.designation.desig_id=2;
this will not work in SQL either, it is beacause you are using the tables employee and designation and you do not actually join them in any way.
Now you have not given us any details regarding the name of the models, but it should be something like
$paymentDetails = PaymentDetails::find()->joinWith('employee.designation')-where(['employee.designation.desig_id' => 2, 'payment_date' => 'date'])->all();
This will execute
select *
from paymentDetails JOIN employee ON 'theDefinedRelation' JOIN designation ON 'theSecondDefinedRelation' where payment_date=date and employee.designation.desig_id=2;
Anyway, it will be a long day, if you do not know why the SQL fails you have to learn SQL first.

Select query in spring hibernate in spring mvc

Hi i am writing an spring mvc, employee application using mysql database,hibernate annotations and jsp . The database contains one table "Empdata" where empid is primary key.And there is a column "team" in "Empdata".I want to select employees in a specific team, example all the details of employees in "Team1".Here i can perform delete and edit operations in the application. For delete operation i am using
sessionfactory.getCurrentSession().createQuery("DELETE FROM Resource WHERE empid=" +resource.getEmpId()).executeUpdate();
query.I know the command line query for select is
SELECT * FROM EmpData WHERE EMPLTEAM ="Team1"
I want to know how to convert this query into hibernate?
please help,thanks in advance..
Query query = session.createQuery("from Resource where emplteam = :team");
query.setParameter("team", "Team1");
List list = query.list();
emplteam should be the property of your class Resource , not your database column's name.
I guess it is simple using HQL .
String hql = "FROM Resource E WHERE E.emplteam = team1";
Query query = session.createQuery(hql);
List results = query.list();
Hope this helps
Note: Keywords like SELECT , FROM and WHERE etc. are not case sensitive but properties like table and column names are case sensitive in HQL.
have you tried using criteria api?
Criteria crit = sessionFactory.getCurrentSession()
.createCriteria(EmpData.class).add(Restrictions.eq("EMPLTEAM", "teamxxx");
List<EmpData> result = crit.list();
For example
Query query = session.createQuery("from Student where name=:name");
query.setParameter("name", "Raj");
In your case i guess the Entity name is Empdata(The object that represent the table)
And the field in the object is team(That has getter and setter in object)
Query query = session.createQuery("from Empdata where team=:teamParam");
query.setParameter("teamParam", "team1");

How to write this SQL query as a JPA query

I have this query that is running successfully in mysql but I am trying to write it as a JPA query and I keep getting errors. Here are my tables.
Table 1: business_accounts{id, business_name}
Table 2: work_locations{location_id, name, contractor_id }
//contractor_id on Table2 is the foreign key matched to id on table 1.
My sql query is that I want to return all values of business_name where id from table 1 equals contractor_id from table 2 and where name="Dublin" Here is my sql query that is working in mysql:
SELECT b.business_name FROM work_locations w
inner join business_accounts b on b.id=w.contractor_id where name="Carlow"
And here is the approach that I am taking in JPA that isn't working. Any suggestions are appreciated. Thanks
String countyName="Dublin";
Query myQuery2 = em.createQuery("SELECT b FROM business_accounts b join "
+ "w where b.id=w.contractor_id and w.name=:countyName");
myQuery2.setParameter("countyName", countyName);
You need to create correct entities corresponding to the table.
a. Business_Account entity
b. Work_Location is the other
c. You may choose to create separate a separate join table for the join between a. and b. I would suggest that, but its your preference.
Based on the a. and b. Try something on these lines -
String countyName="Dublin";
final TypedQuery<String> query = entityManager
.createQuery(
"Select distinct b.business_name from Business_Account b, IN(b.id) location where b.id=location and location.name =:countyName", String.class); //$NON-NLS-1$
query.setParameter("countyName", countyName); //$NON-NLS-1$
Try the below code:
Use createNativeQuery
SELECT b.business_name instead of SELECT b
Add the ENTITY CLASS with the query that will be the return type.
String QUERY = "SELECT b.business_name FROM business_accounts b join w where b.id=w.contractor_id and w.name=?)";
List list = em.createNativeQuery(QUERY, ENTITY_CLASS_HERE.class)
.setParameter(1, countyName)
.getResultList();

Translate MySQL query into cakephp query

I would like to translate this $query using the find() method of the cakephp's ORM.
$custom_query = $this->Agency->query(
"SELECT a.id, a.name, a.created, c.total
FROM agencies a
join (SELECT agency_id, sum(price) total
FROM commands
GROUP BY agency_id) C
on a.id = c.agency_id;"
);
(This query does exactly what I want but I'd like to understand how to use cakephp ORM and $virtualFields)
Thank for your help and your time.
IF you are using CakePHP 1.3 or higher, you can define virtual fields in the model(i.e. calculated fields).
According to your query, I would define the virtual field in the model class like this:
$virtualFields = array(
'total'=>'(SELECT SUM(price) FROM commands WHERE agency_id = Agency.id)'
);
Then you can call that field as a regular ORM field:
$this->Agency->find('all',array(
'fields'=>array(
'id','name','created','total'
)
)
);
The Cake manual page : http://book.cakephp.org/2.0/en/models/virtual-fields.html