Объединение двух таблиц в objectDb
У меня есть две таблицы в 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;
}
}
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();
}
}
После выполнения заданных кодов будет сгенерирован вывод: packagename.Address packagename.Employee и внутри Employee:
1 ответ
Ваш код содержит несколько ошибок при настройке двунаправленных отношений между Сотрудником и Адресом.
Вместо:
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;
}
}
}