我使用Spring MVC和Hibernate构建Web服务,我的问题是我得到了随机索引的json数组,我在我的DAOImpl上应用了Ordering方法,但这根本没有效果。例如,我第一次执行
[{"id":23,"name":"apple"},{"id":21,"name":"mango"},{"id":27,"name":"grape"},{"id":14,"name":"melon"},{"id":7,"name":"banana"}]
然后在我得到的第二次执行
[{"id":14,"name":"melon"},{"id":27,"name":"grape"},{"id":7,"name":"banana"},{"id":23,"name":"apple"},{"id":21,"name":"mango"}]
并且总是在另一个执行中随机编入索引
这是我的代码
控制器
@ResponseBody
@Transactional
@RequestMapping(value="json/dosen/getMakul", method=RequestMethod.POST)
public String getMakulByDosen(@RequestParam(required=true) Integer dosenid) {
String json=null;
List<ModelMakul> listMakul = makulservice.getListMakulByDosen(dosenid, null);
Map<String,Object> data = new HashMap<String,Object>();
data.put("makul", listMakul);
try {
ObjectMapper obj = new ObjectMapper();
json = obj.writeValueAsString(data);
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
DAOImpl
public List<ModelMakul> getListMakulByDosen(Integer id, Integer page) {
// TODO Auto-generated method stub
List<ModelMakul> list = new ArrayList<ModelMakul>();
Criteria c = getCurrentSession().createCriteria(ModelMakul.class);
c.add(Restrictions.eq("dosen.id", id));
if(page!=null) {
c.setFirstResult((page-1)*LISTSIZE);
c.setMaxResults(LISTSIZE);
}
c.addOrder(Order.desc("id"));
list.addAll(c.list());
if(page==null) {
setTotalPage(c);
}
return list;
}
模型1
package id.ac.undip.mlearning.model;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Type;
@Entity
@Table(name="tbl_dosen")
public class ModelDosen {
@Id
@GeneratedValue
@Type(type="java.lang.Integer")
@Column(name="id_dosen")
private Integer id;
@Column(name="nip")
private String nip;
@Column(name="nama")
private String nama;
@ManyToOne
@JoinColumn(name="id_jurusan")
private ModelJurusan jurusan;
@OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true,mappedBy="dosen")
@JsonIgnore
private Set<ModelMakul> makul;
@OneToMany(mappedBy="dosen")
@JsonIgnore
private Set<ModelMateri> materi;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
public Set<ModelMateri> getMateri() {
return materi;
}
public void setMateri(Set<ModelMateri> materi) {
this.materi = materi;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Set<ModelMakul> getMakul() {
return makul;
}
public void setMakul(Set<ModelMakul> makul) {
this.makul = makul;
}
public String getNip() {
return nip;
}
public void setNip(String nip) {
this.nip = nip;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public ModelJurusan getJurusan() {
return jurusan;
}
public void setJurusan(ModelJurusan jurusan) {
this.jurusan = jurusan;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
模型2
package id.ac.undip.mlearning.model;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Type;
@Entity
@Table(name="tbl_makul")
public class ModelMakul {
@Id
@GeneratedValue
@Type(type="java.lang.Integer")
@Column(name="id_makul")
private Integer id;
@ManyToOne
@JoinColumn(name="id_jurusan")
private ModelJurusan jurusan;
@Column(name="kode_makul")
private String kode;
@Column(name="nama")
private String nama;
@Column(name="semester")
private Integer semester;
@ManyToOne
@JoinColumn(name="id_dosen")
private ModelDosen dosen;
@OneToMany(mappedBy="makul")
private Set<ModelMateri> materi;
public Set<ModelMateri> getMateri() {
return materi;
}
public void setMateri(Set<ModelMateri> materi) {
this.materi = materi;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public ModelJurusan getJurusan() {
return jurusan;
}
public void setJurusan(ModelJurusan jurusan) {
this.jurusan = jurusan;
}
public String getKode() {
return kode;
}
public void setKode(String kode) {
this.kode = kode;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public Integer getSemester() {
return semester;
}
public void setSemester(Integer semester) {
this.semester = semester;
}
public ModelDosen getDosen() {
return dosen;
}
public void setDosen(ModelDosen dosen) {
this.dosen = dosen;
}
}
我在这里做错了什么?
对不起英语不好