0

I have two table in objectDb employee and address, i want to join two tables and generate third table like emp_add.

Employee.java

@Entity
public class Employee
{

    int empid;
    String empname;
    private Set<Address> address;

    public Employee(int empid, String empname, Set address)
    {
        this.empid = empid;
        this.empname = empname;
        this.address = address;
    }

    @Id
    public int getId()
    {
        return empid;
    }

    public void setId(int empid)
    {
        this.empid = empid;
    }

    public String getName()
    {
        return empname;
    }

    public void setName(String empname)
    {
        this.empname = empname;
    }

    @ManyToMany
    @JoinTable(name = "emp_add",
            joinColumns
            = @JoinColumn(name = "empid", referencedColumnName = "empid"),
            inverseJoinColumns
            = @JoinColumn(name = "addid", referencedColumnName = "addid")
    )
    public Set getAddress()
    {
        return address;
    }

    public void setAddress(Set address)
    {
        this.address = address;
    }

}

Address.java

@Entity
public class Address
{

    int addid;
    String city;
    String houseno;
    private Set<Employee> employee;
    public Address(int addid, String city, String houseno,Set employee)
    {
        this.addid = addid;
        this.city = city;
        this.houseno = houseno;
    }

    public int getAdd()
    {
        return addid;
    }

    public void setAdd(int addid)
    {
        this.addid = addid;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getHouse()
    {
        return houseno;
    }

    public void setHouse(String houseno)
    {
        this.houseno = houseno;
    }

    @ManyToMany(mappedBy = "employee")
    public Set getEmployee()
    {
        return employee;
    }

    public void setEmployee(Set employee)
    {
        this.employee = employee;
    }

}

EmployyAdd.java
---------------
public class EmployyAdd
{

    public static void main(String[] args)
    {
        EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("$objectdb/db/empadd.odb");
        EntityManager entitymanager = emfactory.createEntityManager();
        entitymanager.getTransaction().begin();
        Set<Employee> employee = new HashSet<Employee>();
        Set<Address> address = new HashSet<Address>();
        Employee emp = new Employee(1, "ram", employee);
        Employee emp2 = new Employee(2, "john", employee);

        Address add = new Address(1, "bbsr", "444", address);
        Address add2 = new Address(2, "delhi", "747",address);
        employee.add(emp);
        employee.add(emp2);
        address.add(add);
        address.add(add2);
        entitymanager.persist(emp);
        entitymanager.persist(emp2);

        entitymanager.persist(add);
        entitymanager.persist(add2);

        entitymanager.getTransaction().commit();
        entitymanager.close();
        emfactory.close();

    }
}

After execute given codes it will generate output like output: packagename.Address packagename.Employee and inside Employee:

Hexfire
  • 5,945
  • 8
  • 32
  • 42

1 Answers1

0

Your code includes several errors in setting the bidirectional relationship between Employee and Address.

Instead of:

  Employee emp = new Employee(1, "ram", employee);
  Employee emp2 = new Employee(2, "john", employee);
  Address add = new Address(1, "bbsr", "444", address);
  Address add2 = new Address(2, "delhi", "747",address);

it should be:

  Employee emp = new Employee(1, "ram", address);
  Employee emp2 = new Employee(2, "john", address);
  Address add = new Address(1, "bbsr", "444", employee);
  Address add2 = new Address(2, "delhi", "747",employee);

Instead of:

  @ManyToMany(mappedBy = "employee")

it should be:

  @ManyToMany(mappedBy = "address")

Always user generic collection parameters (e.g. Set<Address>) to avoid confusion and errors.

Following is the complete example after fixes (in one class format, in which the entity classes are inner static classes):

import java.util.*;

import javax.persistence.*;


public class F2221 {

    public static void main(String[] args) {

        EntityManagerFactory emfactory =
            Persistence.createEntityManagerFactory(
                "objectdb:$objectdb/db/empadd.tmp;drop");
        EntityManager entitymanager = emfactory.createEntityManager();
        entitymanager.getTransaction().begin();
        Set<Employee> employee = new HashSet<Employee>();
        Set<Address> address = new HashSet<Address>();
        Employee emp = new Employee(1, "ram", address);
        Employee emp2 = new Employee(2, "john", address);

        Address add = new Address(1, "bbsr", "444", employee);
        Address add2 = new Address(2, "delhi", "747",employee);
        employee.add(emp);
        employee.add(emp2);
        address.add(add);
        address.add(add2);
        entitymanager.persist(emp);
        entitymanager.persist(emp2);

        entitymanager.persist(add);
        entitymanager.persist(add2);

        entitymanager.getTransaction().commit();
        entitymanager.close();
        emfactory.close();
    }

    @Entity
    public static class Employee
    {

        int empid;
        String empname;
        private Set<Address> address;

        public Employee(int empid, String empname, Set<Address> address)
        {
            this.empid = empid;
            this.empname = empname;
            this.address = address;
        }

        @Id
        public int getId()
        {
            return empid;
        }

        public void setId(int empid)
        {
            this.empid = empid;
        }

        public String getName()
        {
            return empname;
        }

        public void setName(String empname)
        {
            this.empname = empname;
        }

        @ManyToMany
        //@JoinTable(name = "emp_add",
        //        joinColumns
        //        = @JoinColumn(name = "empid", referencedColumnName = "empid"),
        //        inverseJoinColumns
        //        = @JoinColumn(name = "addid", referencedColumnName = "addid")
        //)
        public Set<Address> getAddress()
        {
            return address;
        }

        public void setAddress(Set<Address> address)
        {
            this.address = address;
        }

    }

    @Entity
    public static class Address
    {

        int addid;
        String city;
        String houseno;
        private Set<Employee> employee;
        public Address(int addid, String city, String houseno, Set<Employee> employee)
        {
            this.addid = addid;
            this.city = city;
            this.houseno = houseno;
        }

        public int getAdd()
        {
            return addid;
        }

        public void setAdd(int addid)
        {
            this.addid = addid;
        }

        public String getCity()
        {
            return city;
        }

        public void setCity(String city)
        {
            this.city = city;
        }

        public String getHouse()
        {
            return houseno;
        }

        public void setHouse(String houseno)
        {
            this.houseno = houseno;
        }

        @ManyToMany(mappedBy = "address")
        public Set<Employee> getEmployee()
        {
            return employee;
        }

        public void setEmployee(Set<Employee> employee)
        {
            this.employee = employee;
        }
    }
}
ObjectDB
  • 1,312
  • 8
  • 9
  • But is does not create third table. Thank you – Bimal Kumar Dalei Jan 21 '18 at 13:46
  • No it doesn't. ObjectDB is different from ORM JPA implementation, There are no tables but classes. You may define a 3rd entity class, EmployeeAddress for this purpose, but why should you? There is no point in using ObjectDB as a relational database as you lose its main benefits. – ObjectDB Jan 21 '18 at 17:04