我有3个表大学(id,name),Group(id,name,university_id),Student(id,name,number,group_id)。 一所大学有很多小组,一组有很多学生。
我的POJO看起来像这样:
@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<Student> 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>();
}
我的帮助班:
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();
}
}
我得到了
Error: Could not execute JDBC batch update
Error: could not execute query
尝试保存或显示某些内容时。请帮助。
答案 0 :(得分:3)
首先应该添加@JoinColumn注释,其中应用@ManyToOne,否则将应用默认值。 @JoinColumn具有参数名称,用于指定数据库中的哪一列用于关系。 有关详细信息,请查看documentation