所以我将在Spring和Hibernate中使用Flex 4。 一切都配置和工作。我知道这一点,因为我可以做简单的查询,比如列出表中的所有值。 问题是,当我尝试执行“选择”查询时,我会获得所有值,就像我之前获得的那样,而不是通过选择查询获得特定属性。
我是初学者,所以请忽略我缺乏技术上更健全的词语。但是我不会使用它们,因为我不想误报。
以下是一些能让您更好地理解事物的代码: 这是用于存储来自MySQL数据库的数据的类 -
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;
}
}
这是sessionFactory执行操作的类(太多的import语句,只是为了让事情工作 - 忽略) -
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);
}
}
在上面的类中,getList()有效 完美,列出所有的价值观。该 createFriend用于输入值 id +名称,工作顺利。该 问题是getListAgain() 功能,它给了我一个结果, 想象2列,标题为 'id'和'name',但列出了id值 在两个列(没有名字),作为你 能理解,我在找 getListAgain()函数的结果为 - “1列标题为'id',和 列出id的名称 = 'chirayu已'”。
请帮助,因为我需要明确这一点并继续开发。 谢谢。
此致 chirayu已
更新注意: 我想在此处说一件事,如果我只是按照以下方式开课,这与 intrial1 class 完全相同,但是没有名称的return语句,即没有定义getName(),我得到了正确的查询结果 -
'从intrial1中选择id 名称= 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;
}
}
这是奇怪的还是意味着像这样。仍在寻找答案。
答案 0 :(得分:1)
查询
select id from intrial1 where name='chirayu'
不会返回intrial1实体,因此您应该将返回类型更改为List,或者只是将查询更改为:
from intrial1 where name='chirayu'