0
hibernateを使用してデータベースにエンティティを保存する際に問題が発生しました。 私が探しているのは、次の方法でレコードを取得したいということです。特権を保存しているHIbernate
Employee()。getProjects()[0] .getRole()---------> 従業員に割り当てられた役割をプロジェクトに割り当てる必要があります。
従業員は、それぞれプロジェクトに複数のプロジェクトを持ち、役割を果たします。従業員に が割り当てられている場合、その項目はemployeeid、projectid、およびroleidを持つEMPLOYEE_PROJECTテーブルに保存されます。 エンティティを保存するときに例外が発生しています。私はこの問題を理解していませんでした。 私はこれがEmployee-> Project-> Roleを取得する正しい方法であるとは確信していません。
Exception in thread "main" java.lang.ExceptionInInitializerError
at Client2.main(Client2.java:23)
Caused by: org.hibernate.AnnotationException: A Foreign key refering com.timesheet.entities.Role from com.timesheet.entities.Project has the wrong number of column. should be 1
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:273)
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:89)
at org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:456)
at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:438)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:309)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1319)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at com.timesheet.dao.BaseDao.<clinit>(BaseDao.java:18)
... 1 more
@Entity
@Table(name="ROLE")
public class Role implements Serializable {
@Id
@GeneratedValue
@Column(name = "ROLE_PKEY")
private Long id;
private String roleId;
private String roleName;
Getters and setters
}
@Entity
@Table(name="PROJECT")
public class Project {
@Id
@GeneratedValue
@Column(name = "PROJECT_PKEY")
private long projectKey;
//@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "PROJECT_ID")
private String projectId;
@Column(name = "PROJECT_NAME")
private String projectName;
@ManyToMany(mappedBy="projects", cascade = CascadeType.ALL ,fetch=FetchType.EAGER)
private Set<Employee> employee = new HashSet<Employee>();
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(name = "EMPLOYEE_PROJECT", joinColumns = { @JoinColumn(name = "PROJECT_PKEY")}, inverseJoinColumns = { @JoinColumn(name = "ROLE_PKEY") })
private Role role;
//gettters and setters
}
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id
@GeneratedValue
@Column(name = "EMPLOYEE_PKEY")
private long employeePkey;
@Column(nullable = false)
private String employeeId;
@Column(nullable = false)
private String firstName;
@Column(nullable = false)
private String lastName;
private String password;
private boolean active = true;
//@OneToMany(cascade = CascadeType.ALL ,fetch=FetchType.EAGER)
@ManyToMany(cascade = CascadeType.ALL ,fetch=FetchType.EAGER)
@JoinTable(name = "EMPLOYEE_PROJECT", joinColumns = { @JoinColumn(name = "EMPLOYEE_PKEY") }, inverseJoinColumns = { @JoinColumn(name = "PROJECT_PKEY") })
private Set<Project> projects = new HashSet<Project>(0);
//Getters and setters
}
public class Client2 {
public static void main(String[] args) {
try {
Employee emp = new Employee();
emp.setEmployeeId("abcd");
emp.setFirstName("fdfdfd");
emp.setLastName("dd");
emp.setActive(true);
emp.setPassword("dd");
Role role = new Role();
role.setRoleId("TL");
role.setRoleName("PROJECT MANAGER");
RoleDao dao = new RoleDao();
IEmployeeDao empDao = new EmployeeDao();
empDao.saveEmployee(emp);
dao.saveRole(role);
Role role2 = dao.findEntity(1, Role.class);
Project p = new Project();
p.setProjectId("dddd");
p.setProjectName("def");
p.setRole(role2);
new ProjectDao().saveProject(p);
Employee employee = empDao.findEmployee(1);
employee.getProjects().add(p);
empDao.updateEmployee(employee);
} catch (Exception e2) {
e2.printStackTrace();
}
}
ありがとうございました!!!それは魅力のように働いた!! – user414967