2017-01-20 4 views
0

Ich bin neu in JPA und jetzt studieren, wie die beiden Tabellen durch manytoone Beziehung zu verbinden. Die Entität, die ich bekomme, stammt aus der Datenbank. Ich habe zwei Tische, Abteilung und Angestellte. Viele Mitarbeiter gehören zu einer Abteilung.ManyToOne Beziehung in JPA namedQuery

Abteilung

@Entity 
@Table(name = "DEPARTMENT") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "Department.findAll", query = "SELECT d FROM Department d") 
    , @NamedQuery(name = "Department.findById", query = "SELECT d FROM Department d WHERE d.id = :id") 
    , @NamedQuery(name = "Department.findByName", query = "SELECT d FROM Department d WHERE d.name = :name")}) 
public class Department implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @Basic(optional = false) 
    @Column(name = "ID") 
    private Integer id; 
    @Column(name = "NAME") 
    private String name; 
    @OneToMany(mappedBy = "departmentId") 
    private Collection<Employee> employeeCollection; 

    public Department() { 
    } 

    public Department(Integer id) { 
     this.id = id; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

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

    @XmlTransient 
    public Collection<Employee> getEmployeeCollection() { 
     return employeeCollection; 
    } 

    public void setEmployeeCollection(Collection<Employee> employeeCollection) { 
     this.employeeCollection = employeeCollection; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (id != null ? id.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Department)) { 
      return false; 
     } 
     Department other = (Department) object; 
     if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "entity.Department[ id=" + id + " ]"; 
    } 

} 

Mitarbeiter

@Entity 
@Table(name = "EMPLOYEE") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e") 
    , @NamedQuery(name = "Employee.findByEid", query = "SELECT e FROM Employee e WHERE e.eid = :eid") 
    , @NamedQuery(name = "Employee.findByDeg", query = "SELECT e FROM Employee e WHERE e.deg = :deg") 
    , @NamedQuery(name = "Employee.findByEname", query = "SELECT e FROM Employee e WHERE e.ename = :ename") 
    , @NamedQuery(name = "Employee.findBySalary", query = "SELECT e FROM Employee e WHERE e.salary = :salary")}) 
public class Employee implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @Basic(optional = false) 
    @Column(name = "EID") 
    private Integer eid; 
    @Column(name = "DEG") 
    private String deg; 
    @Column(name = "ENAME") 
    private String ename; 
    // @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation 
    @Column(name = "SALARY") 
    private Double salary; 
    @JoinColumn(name = "DEPARTMENT", referencedColumnName = "ID") 
    @ManyToOne 
    private Department department; 

    public Employee() { 
    } 

    public Employee(Integer eid) { 
     this.eid = eid; 
    } 

    public Integer getEid() { 
     return eid; 
    } 

    public void setEid(Integer eid) { 
     this.eid = eid; 
    } 

    public String getDeg() { 
     return deg; 
    } 

    public void setDeg(String deg) { 
     this.deg = deg; 
    } 

    public String getEname() { 
     return ename; 
    } 

    public void setEname(String ename) { 
     this.ename = ename; 
    } 

    public Double getSalary() { 
     return salary; 
    } 

    public void setSalary(Double salary) { 
     this.salary = salary; 
    } 

    public Department getDepartment() { 
     return department; 
    } 

    public void setDepartment(Department department) { 
     this.department = department; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (eid != null ? eid.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Employee)) { 
      return false; 
     } 
     Employee other = (Employee) object; 
     if ((this.eid == null && other.eid != null) || (this.eid != null && !this.eid.equals(other.eid))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "entity.Employee[ eid=" + eid + " ]"; 
    } 
} 

ManyToOne

public class ManyToOne { 

    public static void main(String[] args) { 
     EntityManagerFactory emfactory = Persistence. 
       createEntityManagerFactory("JoinTablePU"); 
     EntityManager entitymanager = emfactory. 
       createEntityManager(); 
     entitymanager.getTransaction().begin(); 

     //Create Department Entity 
     Department department = new Department(); 
     department.setName("Development"); 
     //Store Department 
     entitymanager.persist(department); 

     //Create Employee1 Entity 
     Employee employee1 = new Employee(); 
     employee1.setEname("Satish"); 
     employee1.setSalary(45000.0); 
     employee1.setDeg("Technical Writer"); 
     employee1.setDepartment(department); 

     //Create Employee2 Entity 
     Employee employee2 = new Employee(); 
     employee2.setEname("Krishna"); 
     employee2.setSalary(45000.0); 
     employee2.setDeg("Technical Writer"); 
     employee2.setDepartment(department); 

     //Create Employee3 Entity 
     Employee employee3 = new Employee(); 
     employee3.setEname("Masthanvali"); 
     employee3.setSalary(50000.0); 
     employee3.setDeg("Technical Writer"); 
     employee3.setDepartment(department); 

     //Store Employees 
     entitymanager.persist(employee1); 
     entitymanager.persist(employee2); 
     entitymanager.persist(employee3); 

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

Fehler

Exception Description: The attribute [employeeCollection] in entity class [class entity.Department] has a mappedBy value of [departmentId] which does not exist in its owning entity class [class entity.Employee]. If the owning entity class is a @MappedSuperclass, this is invalid, and your attribute should reference the correct subclass. 
    at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127) 
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:107) 
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) 
    at jointable.ManyToOne.main(ManyToOne.java:22) 

Antwort

1

Es sollte

@OneToMany(mappedBy = "department") 
    private Collection<Employee> employeeCollection; 

mappedBy = "department" Das Attribut gibt an, dass seine private Department department; das Feld in Mitarbeitern die Beziehung besitzt (d.h. enthält den Fremdschlüssel für die Abfrage zu finden Sie alle Mitarbeiter für eine Abteilung.

Hier können Sie ähnliche example

1

mappedBy Eigenschaft finden wird verwendet, um das inverse Feld in einer bidirektionalen Beziehung zu bezeichnen. Er gibt an, dass employeeCollection automatisch ausgefüllt wird, wenn die Abteilungseinheit aus der DB abgerufen wird. In Ihrem Fall sollte es mappedBy = department

prüfen this Link sein, die genaue Darstellung Ihrer Mitarbeiter-dept Modell zu finden, um eine bidirektionale Beziehung und ihre ausführliche Beschreibung bilden.

Verwandte Themen