So I’m going to be using Flex 4 with Spring and Hibernate.
Everything is configured and working. I know this as I can do simple queries, like listing all values in a table.
Problem is when I try to perform a ‘select’ query, then I get all the values, as I was getting before, and not the specific attributes through Select query.
I’m a beginner, so kindly overlook my lack of more technically sound words..but I don use them as I don wanna mis-quote.
Following is some code which will make You understand things better:
This is class used to store data coming from the MySQL database–
package flex;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="intrial1")
public class intrial1 implements Serializable {
@Id @GeneratedValue
@Column( name = "id")
private int id;
@Column( name = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
This is the class where the sessionFactory does things(too many import statements, just to try to make things work-ignore)–
package flex;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Query;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.NamedNativeQueries;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Transactional;
@Repository
@RemotingDestination
public class SpringBean_Service {
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory factory) {
sessionFactory = factory;
}
@SuppressWarnings("unchecked")
@RemotingInclude
@Transactional
public List<intrial1> getList() {
return sessionFactory.getCurrentSession().createQuery("from intrial1 ").list();
}
@SuppressWarnings("unchecked")
@RemotingInclude
@Transactional
public List<intrial1> getListAgain() {
org.hibernate.Query q = sessionFactory.getCurrentSession().createQuery("select id from intrial1 where name='chirayu'");
List l = q.list();
return l;
}
@RemotingInclude
@Transactional
public void createFriend(String name, int id) {
intrial1 f = new intrial1();
f.setName(name);
f.setId(id);
sessionFactory.getCurrentSession().save(f);
}
}
In above class, getList() works
perfect, lists all the values. The
createFriend is used to enter values
of id+name, and works smoothly. The
problem is with getListAgain()
function, it gives me a result,
imagine 2 columns, having heading as
‘id’ and ‘name’, but listing id values
in both the columns (No names), As You
can understand, I am looking for
result of getListAgain() function as –
“1 column having header as ‘id’, and
listing id wherever the name
=’chirayu'”.
Kindly help, as I will need to clear this and move ahead with development.
Thank You.
Regards,
Chirayu
UPDATE NOTE: Would like to say one thing here, if only I make a class as below, which is identical to intrial1 class, but has no return statement for name, i.e., no getName() defined, I get my correct result for the query –
‘select id from intrial1 where
name=’chirayu”
package flex;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class intrial2 {
@Id @GeneratedValue
@Column( name = "id")
private int id;
@Column( name = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
Is this strange or meant to be like this. Still looking for answers.
The query
doesn’t return intrial1 entity, so you should change return type to List or simply change your query to: