employee.java的代码
package Data;
import java.io.Serializable;
import java.util.*;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
@Entity
public class Employee implements Serializable {
private int empid;
private String empName;
private String empEmailAddress;
private boolean isActive;
private String empAddress;
private String empphone;
@OneToMany
private List<Department> deptobj;= new ArrayList<Department>();
private String emprole;
private Integer salary;
public void setdeptobj(List<Department> deptobj) {
this.deptobj; = deptobj;
}
public List<Department> getdeptobj() {
return deptobj;;
}
public void setempEmailAddress(String empEmailAddress) {
this.empEmailAddress = empEmailAddress;
}
public String getempEmailAddress() {
return empEmailAddress;
}
public boolean getisActive() {
return isActive;
}
public void setisActive(boolean isActive) {
this.isActive = isActive;
}
public String getempAddress() {
return empAddress;
}
public void setempAddress(String empAddress) {
this.empAddress = empAddress;
}
public String getempphone() {
return empphone;
}
public void setempphone(String empphone) {
this.empphone = empphone;
}
public String getemprole() {
return emprole;
}
public void setemprole(String emprole) {
this.emprole = emprole;
}
public int getsalary() {
return salary;
}
public void setsalary(int salary) {
this.salary = salary;
}
@Id
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
Department.java的代码
package Data;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
@Entity
public class Department {
private String dept;
@ManyToOne
private Employee employee;
@Id
private int deptid;
public String getdept() {
return dept;
}
public void setdept(String dept) {
this.dept = dept;
}
public int getdeptid() {
return deptid;
}
public void setdeptid(int deptid) {
this.deptid = deptid;
}
private Employee getemployee(){
return employee;
}
private void setemployee(Employee employee){
this.employee = employee;
}
}
中间层代码
package service;
import Data.*;
import static com.mchange.v2.log.MLog.config;
import java.io.Serializable;
import java.util.List;
import javax.imageio.spi.ServiceRegistry;
import javax.persistence.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class ServiceLayer {
SessionFactory sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
Session session = sessionFactory.openSession() ;
public void add(Employee emp){
session.beginTransaction();
System.out.println("hello");
session.save(emp);
System.out.println("hello");
session.getTransaction().commit();
}
public void delete(int empid){
session.beginTransaction();
Employee emp = (Employee) session.get(Employee.class, empid);
session.delete(emp);
session.getTransaction().commit();
}
public void update(Employee emp){
session.beginTransaction();
session.update(emp);
session.getTransaction().commit();
}
public void read(){
session.beginTransaction();
String hql = "select * from Employee;";
session.createSQLQuery(hql);
session.getTransaction().commit();
}
public Department check(String dept){
session.beginTransaction();
SQLQuery q = session.createSQLQuery("from Department d where d.dept=:deptname");
q.setParameter("deptname", dept);
Department d = (Department) q.uniqueResult();
session.getTransaction().commit();
return d;
}
public Department getDept(Department list){
session.beginTransaction();
Department deptobj= (Department) session.get(Department.class, (Serializable) list.getdeptid());
session.getTransaction().commit();
return deptobj;
}
public void addDept(Department dep){
session.beginTransaction();
session.save(dep);
session.getTransaction().commit();
}
}
主服务层代码
package mainService;
import Data.*;
import static java.sql.Types.NULL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import service.ServiceLayer;
public class Logic {
private int empid;
private String empName;
ServiceLayer sl = new ServiceLayer();
private String empPassword;
private String empEmailAddress;
private boolean isPermanent;
private Calendar empJoinDate;
public void add(String empName,String empEmailAddress,String empAddress,String empphone,String dept,String emprole,Integer salary,boolean active){
Employee emp = new Employee();
emp.setEmpName(empName);
emp.setempAddress(empAddress);
emp.setempEmailAddress(empEmailAddress);
emp.setempphone(empphone);
emp.setemprole(emprole);
emp.setsalary(salary);
emp.setisActive(active);
Department dep = null;
Department list = sl.check(dept);
if(list.getdeptid()==NULL){
dep = addDept(dept);
}
else{
dep = sl.getDept(list);
}
emp.getdeptobj().add((Department) dep);
sl.add(emp);
}
private Department addDept(String dept) {
Department dep = new Department();
dep.setdept(dept);
sl.addDept(dep);
Department list = sl.check(dept);
dep = (Department) sl.getDept(list);
return dep;
}
}
服务层从swing中创建的表单中获取值,然后将数据传递给ServiceLayer.java
文件,该文件创建会话并设置值以创建记录,然后保存该记录问题是我想检查是否部门已存在,然后链接与该搜索的链接将基于用户给出的部门名称,如果部门没有exixt然后首先创建新部门,然后链接到员工。
我无法解决问题所在,因为我之前没有使用过hibernate ...所以请帮助我。在运行文件时,我得到了
的错误Exception in thread "AWT-EventQueue-0" org.hibernate.MappingException:
Could not determine type for: java.util.List, for columns:
[org.hibernate.mapping.Column(deptobj)])
答案 0 :(得分:2)
您可以始终如一地将JPA注释放在getter上,或者将它们放在字段上。将两者混合在一个类中会使Hibernate忽略所有与@Id
注释放在同一位置的内容。
另外,你的映射很奇怪:一个部门有一个员工,也属于其他部门?这是一个单人公司做一切的映射吗?您可能希望在另一个方向上建立关联:一个部门有很多员工,每个员工都属于一个部门。