Hibernate多对多关联失败,关系不存在

时间:2014-11-11 12:32:10

标签: hibernate

我正在开发一个涂鸦类型的项目。在这里,我有3个班级(也有3个班级)作为教授,活动,学生。 每个教授都可以有多个活动,活动与学生有多对多的关系。教授的所有CRUD操作都运行良好。通过事件,我可以添加/删除事件,但更新失败。当我试图创建一个学生时,会发生同样的错误。我正在粘贴域代码。

活动:

@Entity
@Table(name = "events")
public class Events {
    @Id
    @Column(name="eventid")
    private int eventid;

     @ManyToMany()
   @JoinColumn(name = "studentid")
    private Set<Students> students2 = new HashSet<Students>();

    public Set<Students> getStudents2() {
        return students2;
    }

    public void setStudents2(Set<Students> students2) {
        this.students2 = students2;
    }

    public int getEventid() {
        return eventid;
    }

    public void setEventid(int eventid) {
        this.eventid = eventid;
    }
}

学生:

  @Entity
    @Table(name="students")
    public class Students {

     @Id
        @Column(name="studentid")
        @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "student_gen")
        @SequenceGenerator(name = "student_gen",sequenceName = "student_seq")
        private int studentid;

     @ManyToMany(mappedBy = "students2")
        private Set<Events> events2;

        public void setEvents2(Events events2){this.setEvents2(events2);}  // Here it shows I might have recursion problem

        public Set<Events> getEvents2(){return events2;}// Here it shows I might have recursion problem

        public int getEvents2Id(){
            return this.getEvents2Id();
    }
        }

EventsDAOImpl:添加事件有效,更新未通过IDmethod使用get事件并再次使用add来更新

@Transactional
@Repository
public class EventsDAOImpl implements EventsDAO{

 @Override
    public void addEvent(Events events, Professor professor) {
        int id = professor.getId();
        if( session == null) {
            session = this.sessionFactory.openSession();
        }else {
            session = this.sessionFactory.getCurrentSession();
        }
        Professor professor1 = (Professor) session.get(Professor.class,id);
        professor1.getEvents1().add(events);
        events.setProfessor1(professor);
        session.merge(events);
        session.flush();
    }

 @Override
    public Events getEventByid(int eventid) {
        if( session == null) {
            session = this.sessionFactory.openSession();
        }else {
            session = this.sessionFactory.getCurrentSession();
        }

        Events events = (Events) session.get(Events.class,eventid);
        session.saveOrUpdate(events);
        session.flush();
        return events;
    }
}

StudentsDAOImpl:

@Transactional
@Repository
public class  StudentsDAOImpl implements StudentsDAO{

 @Override
public void addStudent(Students students, int eventid) {

    if (session == null) {
        session = this.sessionFactory.openSession();

    } else {
        session = this.sessionFactory.getCurrentSession();
    }
    Events events = (Events) session.get(Events.class, eventid);
    System.out.println("Canvas id here is" + eventid);

    events.getStudents2().add(students);
    students.setEvents2(events);
    session.merge(students);
    session.flush();
}

}

编辑事件时的StackTrace:

org.postgresql.util.PSQLException: ERROR: relation "events_students" does not exist
  Position: 573
    org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
    org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927)
    org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:561)
    org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:419)
    org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:304)
    org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:449)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:202)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)

StackTrace,当我添加学生时:

org.postgresql.util.PSQLException: ERROR: relation "events_students" does not exist
  Position: 573
    org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
    org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927)
    org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:561)
    org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:419)
    org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:304)
    org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:449)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:202)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
    org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:100)
    org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:693)
    org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:92)

0 个答案:

没有答案