代码:如下(一对多单向关联,Grade 对 Students )
package entity; import java.util.HashSet; import java.util.Set; /** * Grade entity. @author MyEclipse Persistence Tools */ public class Grade implements java.io.Serializable { // Fields private Integer gid; private String gname; private String gdesc; private Set studentses = new HashSet(0); // Constructors /** default constructor */ public Grade() { } /** full constructor */ public Grade(String gname, String gdesc, Set studentses) { this.gname = gname; this.gdesc = gdesc; this.studentses = studentses; } // Property accessors public Integer getGid() { return this.gid; } public void setGid(Integer gid) { this.gid = gid; } public String getGname() { return this.gname; } public void setGname(String gname) { this.gname = gname; } public String getGdesc() { return this.gdesc; } public void setGdesc(String gdesc) { this.gdesc = gdesc; } public Set getStudentses() { return this.studentses; } public void setStudentses(Set studentses) { this.studentses = studentses; } } package entity; /** * Students entity. @author MyEclipse Persistence Tools */ public class Students implements java.io.Serializable { // Fields private Integer sid; //private Grade grade; private String sname; private String sex; // Constructors /** default constructor */ public Students() { } /** minimal constructor */ /*public Students(Grade grade, String sname) { this.grade = grade; this.sname = sname; } */ /** full constructor */ // public Students(Grade grade, String sname, String sex) { // this.grade = grade; // this.sname = sname; // this.sex = sex; // } // Property accessors public Integer getSid() { return this.sid; } public void setSid(Integer sid) { this.sid = sid; } /* public Grade getGrade() { return this.grade; } public void setGrade(Grade grade) { this.grade = grade; }*/ public String getSname() { return this.sname; } public void setSname(String sname) { this.sname = sname; } public String getSex() { return this.sex; } public void setSex(String sex) { this.sex = sex; } } public class Test { Session session=null; Transaction tran=null; public static void main(String[] args) { new Test().save(); } //添加 public void save(){ Grade g=new Grade(); g.setGid(1112701); g.setGname("二班"); g.setGdesc("一年级二班"); Students stu=new Students(); stu.setSid(9); stu.setSname("张华2"); stu.setSex("女"); Students stu1=new Students(); stu1.setSid(10); stu1.setSname("刘平2"); stu1.setSex("男"); g.getStudentses().add(stu); g.getStudentses().add(stu1); //try { session=new Configuration().configure().buildSessionFactory().openSession();//得到会话对象 tran=session.beginTransaction();//事务对象 System.out.println(session==null); session.save(g);//添加 session.save(stu); session.save(stu1); [color=red] tran.commit();[/color] //这行报错 System.out.println("添加成功!"); //} catch (HibernateException e) { //System.out.println("添加失败"); //} session.close(); } }
原因:pl/sql number(5),只能插入5位数字