ORM基础Hibernate之重写equals方法和hashcode方法

在经过前面的一二的学习之后我们继续下面的

只需要在User.java

package POJO;

import java.sql.Date;

public class User {
    private int id;
    private String name;
    private String gender;
    int age;
    Date birthday;
    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;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public boolean equals(Object other)
    {
        if(this==other)
            return true;
        if(!(other instanceof User))
        return  false;
        final User u=(User) other;
        if (!name.equals(u.getName()))
            return false;
        if(!birthday.equals(u.getBirthday()))
            return  false;
        if(!(age==u.age))
            return false;
        if (!gender.equals(u.gender))
            return false;
        return true;
    }
    public int hashcode()
    {
        int result;
        result=getName().hashCode();
        result=31*result+getBirthday().hashCode();
        result=31*result+getAge();
        return result;
    }
    public User(String name, int age)
    {
        this.name=name;
        this.age=age;
    }
    public User()
    { }
    public String toString() {
        return "User [id=" + id + ",name=" + name + ",age=" + age + ",gender" + gender + ",birthday=" +birthday;
    }
}

-----------------------------------这就完事啦--------------------------------------
接下来就是渊源
先导:

package DAO;

import java.sql.Date;

import junit.framework.TestCase;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import POJO.User;
public class manageUser extends TestCase
{
    public void testObjectIdentify()
    {
        Configuration cfg=null;
        SessionFactory sf=null;
        Session session=null;
        Transaction ts=null;
        try{
            sf=HibernateUtil.getSessionFactory();
            session=sf.openSession();
            //这里呢如果用的是openSession而没有用getCurrentSession
//            openSession()是不管任何情况都重新开启一个Session,而getCurrentSession();
//            相对的增加了一个判断,在有Session的情况下就会直接去调用,没有session的话才会创建,
//            如果有事务操作的话getCurrentSession();更好一些,很容易的使得一个线程只有一个session对象。
            ts=session.beginTransaction();
            String a="abc";
            String b="abc";
            System.out.println(a==b);
            //这里面“abc"在内存里面a,b指向的是一个
            System.out.println(a.equals(b));
            //比较内存的引用
            String c="abc";
            String d=new String("abc");
            System.out.println(c==d);
            //地址不同了
            System.out.println(c.equals(d));
            //但是这里String把equals重写了。引用不同,但是内容相同。
            ts.commit();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

public void testUserObjectIdentify()
    {
        Configuration cfg=null;
        SessionFactory sf=null;
        Session session=null;
        Transaction ts=null;
        try{
            sf=HibernateUtil.getSessionFactory();
            session=sf.openSession();
            //这里呢如果用的是openSession而没有用getCurrentSession
//            openSession()是不管任何情况都重新开启一个Session,而getCurrentSession();
//            相对的增加了一个判断,在有Session的情况下就会直接去调用,没有session的话才会创建,
//            如果有事务操作的话getCurrentSession();更好一些,很容易的使得一个线程只有一个session对象。
            ts=session.beginTransaction();
            User u1=session.get(User .class,4);
            User u2=session.get(User .class,4);
            System.out.println(u1==u2);
            //引用是相同的因为在同一个session中。session相当于一个缓存
            ts.commit();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

在这里插入图片描述

  public void testUserObjectIdentify()
    {
        SessionFactory sf=null;
        Session session=null;
        Transaction ts=null;
        User u1 = null,u2=null;
        try{
            sf=HibernateUtil.getSessionFactory();
            session=sf.openSession();
            //这里呢如果用的是openSession而没有用getCurrentSession
//            openSession()是不管任何情况都重新开启一个Session,而getCurrentSession();
//            相对的增加了一个判断,在有Session的情况下就会直接去调用,没有session的话才会创建,
//            如果有事务操作的话getCurrentSession();更好一些,很容易的使得一个线程只有一个session对象。
            ts=session.beginTransaction();
            u1=session.get(User .class,4);
            //User u2=session.get(User .class,4);
           // System.out.println(u1==u2);
            //引用是相同的因为在同一个session中。session相当于一个缓存
            ts.commit();
        }
        catch (HibernateException e)
        {
            e.printStackTrace();
            if(ts!=null)
            {
                ts.rollback();
            }
        }
        try{
        sf=HibernateUtil.getSessionFactory();
        session=sf.openSession();
        ts=session.beginTransaction();
        u2=session.get(User .class,4;
        ts.commit();
    }
    catch (HibernateException e)
    {
        e.printStackTrace();
        if(ts!=null)
        {
            ts.rollback();
        }
    }
        finally {
//            session.close();
//            sf.close();
        }
        System.out.println(u1==u2);
        System.out.println(u1.equals(u2));
    }

声明这里如果是false的话就是那个重写equals方法的问题注掉就好了
在这里插入图片描述
下面就加上我们开头的重写的equals方法以及hashcode()结果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值