在经过前面的一二的学习之后我们继续下面的
只需要在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()结果如下: