开发者

Hibernate ManyToOne and OneToMany i one Entity

I have 3 tables University(id,name), Group(id,name,university_id), Student(id,name,number,group_id). One university have many groups and one group have many students.

My POJOs look like this:

@Entity(name = "Student")
public class Student {
    @SequenceGenerator(name = "genStudent",sequenceName = "STUDENTSEQ")
    @Id
    @GeneratedValue(generator = "genStudent")
    private int id;
    @Column(name = "name")
    private String name;
    @Column(name = "facNum")
    private String facNum;
    @ManyToOne
    private UniGroup group;

    public UniGroup getGroup() {
        return group;
    }

    public void setGroup(UniGroup group) {
        this.group = group;
    }

    public Student(){
    }
... getters and setters
}

@Entity
public class UniGroup {
    @SequenceGenerator(name = "genGroup",sequenceName = "UNIGROUPSEQ")
    @Id
    @GeneratedValue(generator = "genGroup")
    private int id;
    @Column(name = "administrativeName")
    private String administrativeName;
    @OneToMany(mappedBy = "group",cascade=CascadeType.ALL)
    private List<Stude开发者_StackOverflownt> students;
    @ManyToOne
    private University university;
... getters and setters
}

@Entity
public class University {
    @SequenceGenerator(name = "genUniversity",sequenceName = "UNIVERSITYSEQ")
    @Id
    @GeneratedValue(generator = "genUniversity")
    private int id;
    @Column(name = "name")
    private String name;
    @OneToMany(mappedBy = "university",cascade=CascadeType.ALL)
    private List<UniGroup> groups;

    public University(){
        groups = new ArrayList<UniGroup>();
    }

My helper class:

public class StudentSystemUtil {
    private SessionFactory sessFac;

    public StudentSystemUtil(){
        sessFac = new Configuration().configure().buildSessionFactory();
    }

    @SuppressWarnings("unchecked")
    public void showGroups(){
        Session session = sessFac.getCurrentSession();

        try{
        session.beginTransaction(); 

        Query q = session.createQuery("from UniGroup");

        List<UniGroup> groups = (List<UniGroup>)q.list();

        for(UniGroup g:groups){
            System.out.println("Group: "+g.getId()+" "+g.getAdministrativeName());
            for(Student s:g.getStudents()){
                System.out.println(s.getName()+" "+s.getGroup().getAdministrativeName());
            }
        }
        session.getTransaction().commit();
        }catch(Exception e){
            System.out.println("Error: "+e.getMessage());
        }
    }

    public void saveGroup(UniGroup group){
        Session session = sessFac.getCurrentSession();
        try{
        session.beginTransaction();

        session.save(group);

        session.getTransaction().commit();
        }catch(Exception e){
            if(session.getTransaction() != null){
                session.getTransaction().rollback();
            }
            System.out.println("Error: "+e.getMessage());
        }
    }

    @SuppressWarnings("unchecked")
    public void showUniversityes(){
        Session session = sessFac.getCurrentSession();

        try{
        session.beginTransaction(); 

        Query q = session.createQuery("from University");

        List<University> universityes = (List<University>)q.list();

        for(University u:universityes){
            System.out.println("University: "+u.getId()+" "+u.getName());
            for(UniGroup g:u.getGroups()){
                System.out.println(g.getAdministrativeName());
                for(Student s:g.getStudents()){
                    System.out.println("Student "+s.getName());
                }
            }
        }
        session.getTransaction().commit();
        }catch(Exception e){
            System.out.println("Error: "+e.getMessage());
        }
    }

    public void saveUniversity(University uni){
        Session session = sessFac.getCurrentSession();
        try{
        session.beginTransaction();

        session.save(uni);

        session.getTransaction().commit();
        }catch(Exception e){
            if(session.getTransaction() != null){
                session.getTransaction().rollback();
            }
            System.out.println("Error: "+e.getMessage());
        }
    }

    public void closeSessionFactory(){
        sessFac.close();
    }

}

And i get

Error: Could not execute JDBC batch update
Error: could not execute query

when try to save or show something. Help please.


First thing you should do is to add @JoinColumn annotation where @ManyToOne is applied, because otherwise default will be applied. @JoinColumn has parameter name which specify which column in database is used for relationship. Have a look at documentation for more info

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜