首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

hibernate annotation 一对多保存对象时不能插入外键解决思路

2012-03-27 
hibernate annotation 一对多保存对象时不能插入外键各位大侠,请帮帮小弟吧:在做一对多保存对象时出现不能

hibernate annotation 一对多保存对象时不能插入外键
各位大侠,请帮帮小弟吧:在做一对多保存对象时出现不能插入外键的现象,这怎么解决,

Java code
@Entity@Table(name="TEACHERS")@SequenceGenerator(name="SEQ_TEACHERS",sequenceName="SEQ_TEACHERS")public class Teacher {        private Long id;        private String code;        private String name;        private String type;        private List<Student> students;        @Id    @GeneratedValue(generator="SEQ_TEACHERS",strategy=GenerationType.SEQUENCE)    @Column(name="id")    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }        @Column(name="CODE")    public String getCode() {        return code;    }    public void setCode(String code) {        this.code = code;    }        @Column(name="NAME")    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }        @Column(name="TYPE")    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }        @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)    @JoinColumn(name="TECH_ID",nullable=true,updatable=true)    public List<Student> getStudents() {        return students;    }    public void setStudents(List<Student> students) {        this.students = students;    }}

Java code
@Entity@Table(name="STUDENTS")@SequenceGenerator(name="SEQ_STUDENTS",sequenceName="SEQ_STUDENTS")public class Student {        private Long id;        private String code;        private String name;//    @Column(name="TECH_ID")//    private Long techId;        private Teacher teacher;        @Id    @GeneratedValue(generator="SEQ_STUDENTS",strategy=GenerationType.SEQUENCE)    @Column(name="id")    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }        @Column(name="CODE")    public String getCode() {        return code;    }    public void setCode(String code) {        this.code = code;    }        @Column(name="NAME")    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    //    public Long getTechId() {//        return techId;//    }//    public void setTechId(Long techId) {//        this.techId = techId;//    }        @ManyToOne(cascade=CascadeType.ALL)    @JoinColumn(name="TECH_ID")    public Teacher getTeacher() {        return teacher;    }    public void setTeacher(Teacher teacher) {        this.teacher = teacher;    }}

Java code
public class AssociationsTest {    /**     * @param args     */    public static void main(String[] args) {//        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();        SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();        Session session = sessionFactory.openSession();        Transaction trans = session.beginTransaction();        Teacher t = new Teacher();        t.setCode("000001");        t.setName("xiaoxue");        t.setType("shuxue");        List<Student> list = new ArrayList<Student>();        Student s0 = new Student();        s0.setCode("000001");        s0.setName("xioali");        list.add(s0);        Student s1 = new Student();        s1.setCode("000002");        s1.setName("zhangsan");        list.add(s1);        t.setStudents(list);        session.save(t);        trans.commit();    }} 


执行到session.save(t);时,报如下错误:
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.myHibernate.test.AssociationsTest.main(AssociationsTest.java:39)
Caused by: java.sql.BatchUpdateException: ORA-01400: 无法将 NULL 插入 ("SYSTEM"."STUDENTS"."TECH_ID")

at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:367)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:8739)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 8 more

[解决办法]
你设了级联,你在保存一这方,多的那方也会保存,
但是多的那方你要保证每个字段都有值,或可以为空,或者有默认值。
比如
a 表 e,f,d 三个字段,
b, 表有 x,y,e,e是引用a的e做外键,
你插人a时,
b,也会插,这是b表里只有e这个字段有值,x,y是空的,如果没有默认值,后值不允许为空,
这时 b表就会报错

热点排行