【问题标题】:Joining of two table in objectDb在 objectDb 中连接两个表
【发布时间】:2018-06-30 01:19:12
【问题描述】:

我在 objectDb 员工和地址中有两个表,我想加入两个表并生成第三个表,如 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;
    }

}

地址.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();

    }
}

执行给定代码后,它会生成如下输出 输出: 包名.地址 包名.Employee 和内部员工:

【问题讨论】:

    标签: objectdb


    【解决方案1】:

    您的代码在设置 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&lt;Address&gt;)以避免混淆和错误。

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

    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 与 ORM JPA 实现不同,没有表,只有类。您可以为此目的定义第三个实体类 EmployeeAddress,但您为什么要这样做呢?将 ObjectDB 用作关系数据库毫无意义,因为您失去了它的主要优势。
    猜你喜欢
    • 2015-08-25
    • 2023-04-09
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2016-12-09
    相关资源
    最近更新 更多