我正在研究休眠和Java项目,需要帮助。
我正在建立学生数据库,用户可以在其中添加学生,他的成绩,设置课程组和主题。
当我将所有这些东西(课程组等)添加到学生中时,就可以了,但是当我想为同一学生设置同一学科的另一个分数时,我有
从我的phpmyadmin看这里:
此刻我被困住了,我不知道如何解决这个问题。
addButton.setOnAction((ActionEvent event) -> {
if (textFieldStudentName.getText() != null && !TextFieldStudentUserName.getText().isEmpty()) {
int selectedMark = setMark.getSelectionModel().getSelectedIndex();
selectedMark++;
String selectedGroup = (String) setGroup.getSelectionModel().getSelectedItem();
Student student = new Student();
Subject subject = new Subject();
Marks mark = new Marks();
Group group = new Group();
group.setGroup_name(selectedGroup);
mark.setMark(selectedMark);
subject.setSubject_name(subjectField.getText());
student.setName(textFieldStudentName.getText());
student.setUserName(TextFieldStudentUserName.getText());
mark.setStudent(student);
mark.setSubject(subject);
student.setGroup(group);
subjectDAO.create(subject);
groupDAO.create(group);
studentDAO.create(student);
marksDAO.create(mark);
textFieldStudentName.clear();
TextFieldStudentUserName.clear();
subjectField.clear();
}
});
学生班
@Entity
public class Student implements Serializable {
private static final long serialVersionUID = 4L;
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(unique = true)
private int student_id;
@Column(length = 15, nullable = false,name="imie")
private String name;
@Column(length = 15, nullable = false,name="nazwisko")
private String UserName;
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "group_id")
private Group group;
@OneToMany(targetEntity = Marks.class, mappedBy = "student", cascade = CascadeType.REMOVE)
private Set<Marks> marks;
public Student(int student_id, String name, String UserName, Group group, Set<Marks> marks) {
this.student_id = student_id;
this.name = name;
this.UserName = UserName;
this.group = group;
this.marks = marks;
}
public Student(){};
public int getStudent_id() {
return student_id;
}
public void setStudent_id(int student_id) {
this.student_id = student_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUserName() {
return UserName;
}
public void setUserName(String UserName) {
this.UserName = UserName;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public Set<Marks> getMarks() {
return marks;
}
public void setMarks(Set<Marks> marks) {
this.marks = marks;
}
public int setMark(int selectedMark) {
return selectedMark;
}
}
标记类
@Entity
@Table(name = "Oceny")
public class Marks implements Serializable {
private static final long serialVersionUID = 2L;
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(unique = true)
private long ocena_id;
@Column(name = "ocena")
private int mark;
@ManyToOne(cascade = CascadeType.ALL)
@GeneratedValue
@JoinColumn(name = "subject_id")
private Subject subject;
@ManyToOne(cascade = CascadeType.ALL)
@GeneratedValue
@JoinColumn(name = "student_id")
private Student student;
public Marks(long ocena_id, int mark, Subject subject, Student student) {
this.ocena_id = ocena_id;
this.mark = mark;
this.subject = subject;
this.student = student;
}
public Marks(){};
public long getOcena_id() {
return ocena_id;
}
public void setOcena_id(long ocena_id) {
this.ocena_id = ocena_id;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
除了看到XYZ带有多个标记,而且科目,课程组和学生没有重复之外,我没有。 how I want to see in database 我是在简单的类中手动完成此操作的,但是我需要在javafx
中执行此操作public class ProjektPORun {
public static void main(String[] args) {
Student student1=new Student();
student1.setName("Adam");
student1.setUserName("Malysz");
Student student2=new Student();
student2.setName("Robert");
student2.setUserName("Kubica");
Group grupa=new Group();
grupa.setGroup_name("LAB1");
Subject subject=new Subject();
subject.setSubject_name("History");
Marks mark=new Marks();
mark.setMark(1);
Marks mark1=new Marks();
mark1.setMark(2);
Marks mark2=new Marks();
mark2.setMark(3);
student1.setGroup(grupa);
mark.setStudent(student1);
mark1.setStudent(student1);
mark.setSubject(subject);
student2.setGroup(grupa);
mark1.setStudent(student2);
mark2.setStudent(student2);
mark1.setSubject(subject);
mark2.setSubject(subject);
Configuration config = new Configuration().configure("META-INF\\hibernate.cfg.xml").addAnnotatedClass(Student.class).addAnnotatedClass(Group.class).addAnnotatedClass(Marks.class).addAnnotatedClass(Subject.class);
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
SessionFactory factory = config.buildSessionFactory(serviceRegistry);
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
session.save(student2);
session.save(subject);
session.save(mark);
session.save(mark1);
session.save(mark2);
session.save(grupa);
session.save(student1);
transaction.commit();
session.close();
factory.close();
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
}