Hibernate 查询排序与联合主键映射

1.查询排序

(1)数据库排序(推荐)

<map order-by="name ase" >   <!--name的升序,降序desc-->
session.createQuery("  ").uniqueResult()  //返回唯一的对象,前台对象只有一个
<set order-by="name asc">

(2)内存排序

<set sort="natural" >

sort属性值有三种:

  • natural,升序
  • unsorted,不排序
  • 类名,自定义的排序规则,实现Comparator接口,并实现该接口中的compare方法,将类名作为sort属性名。

字符串比较: o1.compareTo(o2);

2.联合主键映射规则:

类中的每个主键属性都对应到数据表中的每个主键列,hibernate要求具有联合主键的实体类:

  • 必须实现Serializable接口
  • 实现hashCoad()、equals(),重写原因hibernate要根据数据库的联合主键来判断某两行记录是否是一样的,若一样,那么就是同一个对象;若不一样,则为不同对象。这反应到程序领域中就是根据hashcoad与equals方法来判断某两个对象是否能够放到诸如set这样的集合中。联合主键实现接口原因,使用get()、load()时,需要先构造出来该实体的对象,并且将查询依旧(联合主键)设置进去,然后作为get()或load()的第二个参数传进去即可。
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();
        }

 (2)将主键所对应属性提取出一个类(称之为主键类),并且主键类需要实行serializable接口,重写equals()与hasCode()方法,原因同上。

public class Student implements Serializable
{
    private int age;
    private StudentPrimaryKey studentPrimaryKey;
    
    public StudentPrimaryKey getStudentPrimaryKey()
    {
        return studentPrimaryKey;
    }
    public void setStudentPrimaryKey(StudentPrimaryKey studentPrimaryKey)
    {
        this.studentPrimaryKey = studentPrimaryKey;
    }
    public int getAge()
    {
        return age;
    }
    public void setAge(int age)
    {
        this.age = age;
    }
    
}
public class StudentPrimaryKey implements Serializable
{
    private String name;
    private String cardId;
    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;
    }
    @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;
        StudentPrimaryKey other = (StudentPrimaryKey) 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;
    }
    
    
}
<hibernate-mapping>
    <class name="com.liule.hibernate.Student" table="Student">
    
    
        <composite-id name="studentPrimaryKey" class="com.liule.hibernate.StudentPrimaryKey"> <!-- 提取出来的主要差别 -->
            <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);
        
        StudentPrimaryKey studentPrimaryKey = new StudentPrimaryKey();
        studentPrimaryKey.setCardId("123");
        studentPrimaryKey.setName("zhangsan");
        
        student.setStudentPrimaryKey(studentPrimaryKey);
        
        Session session = sf.openSession();
        Transaction ts = null;
        try

查询数据:

public static void main(String[] args)
    {
        Session session = sf.openSession();
        Transaction ts = null;
        try
        {
            StudentPrimaryKey studentPrimaryKey = new StudentPrimaryKey();
            studentPrimaryKey.setName("zhangsan");
            studentPrimaryKey.setCardId("123");
            Student student = (Student)session.get(Student.class,studentPrimaryKey);
            System.out.println(student.getAge());
            
            ts = session.beginTransaction();
            ts.commit();
        }

 

你可能感兴趣的:(Hibernate 查询排序与联合主键映射)