i'm using hibernate 3.2,and load works werid.here problem.
package entity; import java.util.date; import java.util.hashset; import java.util.set; import javax.persistence.cascadetype; import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.onetomany; @entity public class teacher { private int id; private string name; private set<student> students = new hashset<student>(); private date time; public teacher() { super(); } public teacher(int id, string name, set<student> students, date time) { super(); this.id = id; this.name = name; this.students = students; this.time = time; } @id @generatedvalue public int getid() { return id; } public string getname() { return name; } @onetomany(mappedby="teacher",cascade={cascadetype.all},fetch=fetchtype.eager) @joincolumn(name="teacher_id") public set<student> getstudents() { return students; } public date gettime() { return time; } public void setid(int id) { this.id = id; } public void setname(string name) { this.name = name; } public void setstudents(set<student> students) { this.students = students; } public void settime(date time) { this.time = time; } @override public string tostring() { return "teacher+"+this.name+",student number:"+this.students.size(); } } teacher.java
another class
package entity; import java.util.date; import javax.persistence.cascadetype; import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.manytoone; @entity public class student implements cloneable{ private date brith; private int id; private teacher teacher; public student() { super(); } public student(date brith, int id, teacher teacher) { super(); this.brith = brith; this.id = id; this.teacher = teacher; } public date getbrith() { return brith; } @id @generatedvalue public int getid() { return id; } @manytoone(cascade={cascadetype.all},fetch=fetchtype.lazy) public teacher getteacher() { return teacher; } public void setbrith(date brith) { this.brith = brith; } public void setid(int id) { this.id = id; } public void setteacher(teacher teacher) { this.teacher = teacher; } @override public string tostring() { return "student brith="+this.brith+"teacher="+teacher.getname(); } } student.java
here test codes,
package entity; import java.util.date; import java.util.hashset; import java.util.set; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.cfg.annotationconfiguration; import org.junit.afterclass; import org.junit.beforeclass; import org.junit.test; public class hibernate { static sessionfactory sessionfactory; @beforeclass public static void setupbeforeclass() throws exception { sessionfactory = new annotationconfiguration().configure().buildsessionfactory(); } @afterclass public static void teardownafterclass() throws exception { sessionfactory.close(); } @test public void testsavestudent(){ student s = new student(); s.setbrith(new date()); student s1 = new student(); s1.setbrith(new date()); teacher t = new teacher(); t.setname("teacher"); t.settime(new date()); set<student> set = new hashset<student>(); set.add(s1); set.add(s); t.setstudents(set); s1.setteacher(t); s.setteacher(t); session session = sessionfactory.getcurrentsession(); session.begintransaction(); session.save(s1); session.gettransaction().commit(); } @test public void testfetchstudent(){ testsavestudent(); session session = sessionfactory.getcurrentsession(); session.begintransaction(); student s = (student)session.load(student.class, 1); s.getbrith(); session.gettransaction().commit(); system.out.println(s); } @test public void testfetchstudent2(){ testsavestudent(); session session = sessionfactory.getcurrentsession(); session.begintransaction(); student s = (student)session.load(student.class, 1); system.out.println(s); session.gettransaction().commit(); system.out.println(s); } @test public void testfetchteacher(){ testsavestudent(); session session = sessionfactory.getcurrentsession(); session.begintransaction(); teacher t = (teacher)session.load(teacher.class, 1); /*system.out.println(t); t.setname("what doing"); session.update(t);*/ session.gettransaction().commit(); system.out.println(t); } public static void main(string[] args) throws exception { setupbeforeclass(); } }
let make easier,the method teststudent fail,but teststudent2 succeed,what magic in session.load method?why not same result?
Comments
Post a Comment