java - Only mapping from department side is mapped and not from inverse side.How can i say this is a Many to Many -
below code
package com.hibernate.mapping.mtm; import java.util.arraylist; import java.util.list; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.transaction; import org.hibernate.cfg.configuration; /** * @author prajapati * */ public class main { /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub sessionfactory sf= new configuration().configure("com/hibernate/mapping/mtm/hibernate.cfg.xml").buildsessionfactory(); session sn = sf.opensession(); transaction tx = sn.begintransaction(); department dept1 = new department(); dept1.setid(20l); dept1.setdepartmentname("development"); department dept2 = new department(); dept2.setid(10l); dept2.setdepartmentname("accounts"); department dept3 = new department(); dept3.setid(30l); dept3.setdepartmentname("qa"); department dept4 = new department(); dept4.setid(40l); dept4.setdepartmentname("designing"); employee emp1 = new employee(); emp1.setemployeeid(30l); emp1.setemployeename("michele"); employee emp2 = new employee(); emp2.setemployeeid(40l); emp2.setemployeename("devendra"); employee emp3 = new employee(); emp3.setemployeeid(50l); emp3.setemployeename("prabhat"); employee emp4 = new employee(); emp4.setemployeeid(60l); emp4.setemployeename("himanshi"); employee emp5 = new employee(); emp5.setemployeeid(70l); emp5.setemployeename("kanika"); list<employee> employeesqa = new arraylist<employee>(); employeesqa.add(emp3); employeesqa.add(emp2); list<employee> employeesdev = new arraylist<>(); employeesdev.add(emp3); employeesdev.add(emp4); list<employee> employeesaccounts = new arraylist<>(); employeesaccounts.add(emp5); employeesaccounts.add(emp1); dept3.setemployees(employeesqa); dept1.setemployees(employeesdev); list<department> departments = new arraylist<department>(); departments.add(dept1); departments.add(dept2); departments.add(dept3); departments.add(dept4); //emp5 works in departments emp5.setdepartments(departments); /*list<department> departments3 = new arraylist<department>(); departments3.add(dept1); departments3.add(dept2); emp3.setdepartments(departments3); */ sn.save(emp5); sn.save(dept3); sn.save(dept1); /** * when running program. * mapping department side mapped , not inverse side.how can m2m * while unable map both sides in terms of hibernate */ // sn.save(p); tx.commit(); sn.close(); } }
package com.hibernate.mapping.mtm; import java.util.list; import javax.persistence.cascadetype; import javax.persistence.entity; import javax.persistence.id; import javax.persistence.manytomany; @entity public class department { @id private long id; private string departmentname; //@manytomany(cascade=cascadetype.all,mappedby="departments") @manytomany(cascade=cascadetype.all) private list<employee> employees; public long getid() { return id; } public void setid(long id) { this.id = id; } 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; } }
package com.hibernate.mapping.mtm; import java.util.list; import javax.persistence.cascadetype; import javax.persistence.entity; import javax.persistence.id; import javax.persistence.manytomany; @entity public class employee { @id private long employeeid; private string employeename; @manytomany(mappedby="employees",cascade=cascadetype.all) //mapped says here go employees in opposite table details of departments employees , key of relationship on other side. //i.e. considering department side owner side saying mappedby. //@manytomany(cascade=cascadetype.all) private list<department> departments; public long getemployeeid() { return employeeid; } public void setemployeeid(long employeeid) { this.employeeid = employeeid; } public string getemployeename() { return employeename; } public void setemployeename(string employeename) { this.employeename = employeename; } public list<department> getdepartments() { return departments; } public void setdepartments(list<department> departments) { this.departments = departments; } }
<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.driver</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/mapping</property> <property name="hibernate.connection.username">root</property> <!-- <property name="hibernate.dialect">org.hibernate.dialect.mysql5dialect</property> --> <property name="hibernate.dialect">org.hibernate.dialect.mysqlinnodbdialect</property> <property name="hbm2ddl.auto">create</property> <property name="show_sql">true</property> <mapping class="com.hibernate.mapping.mtm.employee"/> <mapping class="com.hibernate.mapping.mtm.department"/> </session-factory> </hibernate-configuration>
i confused on why didn't hibernate saved entries emp5 in departments , relationship many many there no entry done employee emp5.
below tables respective data:
mysql> select * employee; | employeeid | employeename | | 40 | devendra | | 50 | prabhat | | 60 | himanshi | | 70 | kanika | mysql> select * department; | id | departmentname | | 10 | accounts | | 20 | development | | 30 | qa | | 40 | designing | mysql> select * department_employee; | departments_id | employees_employeeid | | 20 | 50 | | 20 | 60 | | 30 | 50 | | 30 | 40 |
the association bidirectional. bidirectional association has 2 sides: owner side, , inverse side. hibernate doesn't care inverse side when persisting. considers owner side. owner side side not have mappedby
attribute. so, owner side here department
.
your code adds dept1, dept2, etc. emp5. doesn't add emp5 dept1, dept2, etc.
so not code make object graph incoherent (if dept1 in emp5's departments, dept1 should have emp5 in list of employees), initializes side hibernate doesn't care about.
Comments
Post a Comment