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:

4

1 回答 1

0

您的代码在设置 Employee 和 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 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);

代替:

  @ManyToMany(mappedBy = "employee")

它应该是:

  @ManyToMany(mappedBy = "address")

始终使用通用集合参数(例如Set<Address>)以避免混淆和错误。

以下是修复后的完整示例(以一种类格式,其中实体类是内部静态类):

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;
        }
    }
}
于 2018-01-21T08:17:41.013 回答