java - Initial SessionFactory creation failed.org.hibernate.AnnotationException: @OneToOne or @ManyToOne on net.viralpatel.hibernate.Employee.department -
i new hibernate. developed onetomany relation example, see following example. please me how solve below error?
info: bind entity net.viralpatel.hibernate.employee on table employee initial sessionfactory creation failed.org.hibernate.annotationexception: @onetoone or @manytoone on net.viralpatel.hibernate.employee.department references unknown entity: net.viralpatel.hibernate.department exception in thread "main" java.lang.exceptionininitializererror @ net.viralpatel.hibernate.hibernateutil.buildsessionfactory(hibernateutil.java:18) @ net.viralpatel.hibernate.hibernateutil.<clinit>(hibernateutil.java:8) @ net.viralpatel.hibernate.main.main(main.java:12) caused by: org.hibernate.annotationexception: @onetoone or @manytoone on net.viralpatel.hibernate.employee.department references unknown entity: net.viralpatel.hibernate.department @ org.hibernate.cfg.toonefksecondpass.dosecondpass(toonefksecondpass.java:81) @ org.hibernate.cfg.annotationconfiguration.processfksecondpassinorder(annotationconfiguration.java:499) @ org.hibernate.cfg.annotationconfiguration.secondpasscompile(annotationconfiguration.java:304) @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:1292) @ org.hibernate.cfg.annotationconfiguration.buildsessionfactory(annotationconfiguration.java:859) @ net.viralpatel.hibernate.hibernateutil.buildsessionfactory(hibernateutil.java:15) ... 2 more i developed code far:
@entity @table(name="employee") public class employee { @id @generatedvalue @column(name="employee_id") private long employeeid; @column(name="firstname") private string firstname; @column(name="lastname") private string lastname; @column(name="birth_date") private date birthdate; @column(name="cell_phone") private string cellphone; @manytoone @joincolumn(name="departmentid") private department department; public employee(){} public employee(string firstname, string lastname, string phone) { this.firstname = firstname; this.lastname = lastname; this.birthdate = new date(system.currenttimemillis()); this.cellphone = phone; } // setters , getters } department
public class department { @id @generatedvalue @column(name="department_id") private long departmentid; @column(name="dept_name") private string departmentname; @onetomany private list<employee> employees; public long getdepartmentid() { return departmentid; } public void setdepartmentid(long departmentid) { this.departmentid = departmentid; } public string getdepartmentname() { return departmentname; } public void setdepartmentname(string departmentname) { this.departmentname = departmentname; } public list<employee> getemployees() { return employees; } public void setemployees(list<employee> employees) { this.employees = employees; } } hibernateutil
public class hibernateutil { private static final sessionfactory sessionfactory = buildsessionfactory(); private static sessionfactory buildsessionfactory() { try { // create sessionfactory hibernate.cfg.xml return new annotationconfiguration().configure().buildsessionfactory(); } catch (throwable ex) { system.err.println("initial sessionfactory creation failed." + ex); throw new exceptionininitializererror(ex); } } public static sessionfactory getsessionfactory() { return sessionfactory; } } main
public class main { public static void main(string[] args) { sessionfactory sf = hibernateutil.getsessionfactory(); session session = sf.opensession(); session.begintransaction(); department department = new department(); department.setdepartmentname("sales"); employee emp1 = new employee("nina", "mayers", "111"); employee emp2 = new employee("tony", "almeida", "222"); department.setemployees(new arraylist<employee>()); department.getemployees().add(emp1); department.getemployees().add(emp2); session.save(department); session.gettransaction().commit(); session.close(); } }
mark department class entity @entity annotation.
Comments
Post a Comment