标签:
1.查询排序
(1)数据库排序(推荐)
<map order-by="name ase" > <!--name的升序,降序desc-->
session.createQuery(" ").uniqueResult() //返回唯一的对象,前台对象只有一个
<set order-by="name asc">
(2)内存排序
<set sort="natural" >
sort属性值有三种:
字符串比较: o1.compareTo(o2);
2.联合主键映射规则:
类中的每个主键属性都对应到数据表中的每个主键列,hibernate要求具有联合主键的实体类:
public class Student implements Serializable { private String name; private String cardId; private int age; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((cardId == null) ? 0 : cardId.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (cardId == null) { if (other.cardId != null) return false; } else if (!cardId.equals(other.cardId)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
<hibernate-mapping> <class name="com.liule.hibernate.Student" table="Student"> <composite-id> <key-property name="cardId" column="cardId" type="string"></key-property> <key-property name="name" column="name" type="string"></key-property> </composite-id> <property name="age" column="age" type="int"></property> </class> </hibernate-mapping>
插入数据:
public static void main(String[] args) { Student student = new Student(); student.setAge(10); student.setCardId("123"); student.setName("zhangsan"); Session session = sf.openSession(); Transaction ts = null;
查询:
public static void main(String[] args) { Student studentPrimaryKey = new Student(); studentPrimaryKey.setCardId(10); studentPrimaryKey.setName("zhangsan"); Session session = sf.openSession(); Transaction ts = null; try { Student student = (Student)session.get(Student.class,studentPrimaryKey); System.out.println(student.getAge()); System.out.println(studentPrimaryKey.getAge()); ts = session.beginTransaction(); ts.commit(); }
标签:
原文地址:http://www.cnblogs.com/liu-Gray/p/4999510.html