objectDb の従業員と住所に 2 つのテーブルがあり、2 つのテーブルを結合して、emp_add のような 3 番目のテーブルを生成したいと考えています。
従業員.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();
}
}
指定されたコードを実行すると、output: packagename.Address packagename.Employee および Employee 内のような出力が生成されます。