package?junit;
import?java.sql.Connection;
import?java.sql.SQLException;
import?java.util.Date;
import?org.hibernate.Session;
import?org.hibernate.SessionFactory;
import?org.hibernate.Transaction;
import?org.hibernate.cfg.Configuration;
import?org.hibernate.jdbc.Work;
import?org.junit.Test;
import?com.model.Student;
public?class?SessionTest?{
@Test
public?void?test1(){
Configuration?configure?=?new?Configuration().configure();
SessionFactory?sessionFactory?=configure.buildSessionFactory();
Session?session?=?sessionFactory.openSession();
//System.out.println(session==session2);
Transaction?trans=session.beginTransaction();
session.doWork(new?Work()?{
@Override
public?void?execute(Connection?arg0)?throws?SQLException?{
//?TODO?Auto-generated?method?stub
System.out.println(arg0.hashCode());
}
});
Student?s?=?new?Student(7,?"kkk",?new?Date(),?"上海");
session.save(s);
trans.commit();
Session?session2?=?sessionFactory.openSession();
trans=session2.beginTransaction();
session2.doWork(new?Work()?{
@Override
public?void?execute(Connection?arg0)?throws?SQLException?{
//?TODO?Auto-generated?method?stub
System.out.println(arg0.hashCode());
}
});
s?=?new?Student(6,?"111",?new?Date(),?"上海");
session2.save(s);
trans.commit();
session.close();
session2.close();
}
}
2022-03-29
你看看判斷狀態,改變狀態的地方是不是有什么問題嘞。
vvqigfidkgsjkhugwbbdxbidzphyjrrzrcmrtguhoevcd
2016-11-30
理解是兩個不同對象就好,估計你也使用的Java 8
2016-11-30
hashCode方法是Object的方法 ?你是可以在任何子類里面復寫的(java所有類均繼承Object),如果手動設置 ?return “1"那就會相同
2016-11-30
我也使用的hibernate5也是這樣子,剛剛查了下Java中兩個不同對象的hashcode可能相同
connection hashcode:1968552224
Hibernate:?
? ? insert?
? ? into
? ? ? ? student
? ? ? ? (sname, gender, birthday, address, sid)?
? ? values
? ? ? ? (?, ?, ?, ?, ?)
connection hashcode:1968552224
false
Hibernate:?
? ? insert?
? ? into
? ? ? ? student
? ? ? ? (sname, gender, birthday, address, sid)?
? ? values
? ? ? ? (?, ?, ?, ?, ?)
2016-08-01
因為你顯示的關閉了session會話,如果你注釋掉,hashcode值就會不一樣了,這也就是老師一直在證明opensesion()為什么不會自動關閉session會話,getCurrentSession()能自動關閉session會話了。我在評論區有具體說明。你可以去看下
2016-06-30
不小心按了一個截圖按鈕。
console輸出是這個:
Hibernate:?
? ? drop table if exists STUDENT
Hibernate:?
? ? create table STUDENT (
? ? ? ? SID integer not null,
? ? ? ? SNAME varchar(255),
? ? ? ? BIRTHDAY datetime,
? ? ? ? ADDRESS varchar(255),
? ? ? ? primary key (SID)
? ? )
Jun 30, 2016 10:04:40 PM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@a7e2d9d'
1215029765
Hibernate:?
? ? insert?
? ? into
? ? ? ? STUDENT
? ? ? ? (SNAME, BIRTHDAY, ADDRESS, SID)?
? ? values
? ? ? ? (?, ?, ?, ?)
1215029765
Hibernate:?
? ? insert?
? ? into
? ? ? ? STUDENT
? ? ? ? (SNAME, BIRTHDAY, ADDRESS, SID)?
? ? values
? ? ? ? (?, ?, ?, ?)