首頁>Program>source

我正在尝試在JPA 2.0(JBoss 7.1.1)中建立一个ManyToMany關係,並在该關係中添加了一个額外的列(以下以粗體顯示),例如:

Employer           EmployerDeliveryAgent             DeliveryAgent
(id,...)   (employer_id, deliveryAgent_id, **ref**)  (id,...)

我不想有重複的屬性,所以我想應用http://giannigar.wordpress.com/2009/09/04/mapping-a-many-to- 许多使用jpa /的表具有額外的列.但是我無法正常工作,但出現了一些錯誤,例如:

  1. 嵌入式ID類不應包含關係對映(實際上規範中是這樣的);
  2. 在屬性" employerDeliveryAgent"中,"對映"值" pk.deliveryAgent"無法解析為目標實體上的屬性;
  3. 在屬性" employerDeliveryAgent"中,"對映"值" pk.employer"無法解析為目標實體上的屬性;
  4. 無法解決替代屬性" pk.deliveryAgent"的持久型別;
  5. 持久屬性" pk.employer"的型別無法解析;

该鏈接上的许多人都說它工作正常,因此我认為我的環境中有些不同,也许是JPA或Hibernate版本.所以我的問题是:如何使用JPA 2.0(Jboss 7.1.1 /使用Hibernate作為JPA實現)實現這種情况? 並補充這个問题:我應该避免使用複合键,而是使用普通生成的ID和唯一約束吗?

谢谢。

糟糕:我没有在這裏複製源代碼,因為它本質上是上面鏈接中的源代碼的副本,只是具有不同的類和屬性名稱,所以我认為没有必要。

最新回復
  • 5月前
    1 #

    首先,您需要生成一个 EmployerDeliveryAgentPK 類,因為它具有多个PK:

    @Embeddable
    public class EmployerDeliveryAgentPK implements Serializable {
        @Column(name = "EMPLOYER_ID")
        private Long employer_id;
         @Column(name = "DELIVERY_AGENT_ID")
        private Long deliveryAgent_id;
    }
    

    下一步,您需要建立一个 EmployerDeliveryAgent 類.此類代表 Employer的多對多表 和 DeliveryAgent

    @Entity
    @Table(name = " EmployerDeliveryAgent")
    public class EmployerDeliveryAgent implements Serializable {
       @EmbeddedId
        private EmployerDeliveryAgentPK id;
       @ManyToOne
        @MapsId("employer_id") //This is the name of attr in EmployerDeliveryAgentPK class
        @JoinColumn(name = "EMPLOYER_ID")
        private Employer employer;
        @ManyToOne
        @MapsId("deliveryAgent_id")
        @JoinColumn(name = "DELIVERY_AGENT_ID")
        private DeliveryAgent deliveryAgent;    
    }
    

    之後,在 Employer Class程您需要添加:

    @OneToMany(mappedBy = "deliveryAgent")
        private Set<EmployerDeliveryAgent> employerDeliveryAgent = new HashSet<EmployerDeliveryAgent>();
    

    DeliveryAgent Class程您需要添加:

    @OneToMany(mappedBy = "employer")
            private Set<EmployerDeliveryAgent> employer = new HashSet<EmployerDeliveryAgent>();
    

    這就是全部! 祝您好運!

  • 5月前
    2 #

    Eric Lucio和Renan的迴答都對您有所帮助,但是在關聯表中使用id是多餘的.您在類中同時拥有關聯的實體.這不是必需的.您可以使用 @Id簡單對映關聯類中的關聯實體 在相關實體欄位上。

    @Entity
    public class Employer {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
        @OneToMany(mappedBy = "employer")
        private List<EmployerDeliveryAgent> deliveryAgentAssoc;
        // other properties and getters and setters
    }
    @Entity
    public class DeliveryAgent {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
        @OneToMany(mappedBy = "deliveryAgent")
        private List<EmployerDeliveryAgent> employerAssoc;
        // other properties and getters and setters
    }
    

    關聯類

    @Entity
    @Table(name = "employer_delivery_agent")
    @IdClass(EmployerDeliveryAgentId.class)
    public class EmployerDeliveryAgent {
        @Id
        @ManyToOne
        @JoinColumn(name = "employer_id", referencedColumnName = "id")
        private Employer employer;
        @Id
        @ManyToOne
        @JoinColumn(name = "delivery_agent_id", referencedColumnName = "id")
        private DeliveryAgent deliveryAgent;
        @Column(name = "is_project_lead")
        private boolean isProjectLead;
    }
    

    仍然需要關聯PK類.請註意,欄位名稱應与關聯類中的欄位名稱完全對應,但型別應為關聯型別中ID的型別。

    public class EmployerDeliveryAgentId implements Serializable {
        private int employer;
        private int deliveryAgent;
        // getters/setters and most importantly equals() and hashCode()
    }
    

  • 5月前
    3 #

    好,我根据

    提供的解決方案使它正常工作

    http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns。

    此解決方案不会在資料庫上生成重複的屬性,但是会在我的JPA實體中生成重複的屬性(這是可以接受的,因為您可以將額外的工作傳遞给構造函式或方法-最终是透明的).資料庫中生成的主键和外键都是100%正確的。

    如鏈接上所述,我不能使用@PrimaryKeyJoinColumn而是使用@JoinColumn(name =" projectId",updatable = false,insertable = false,referencedColumnName =" id").值得一提的另一件事是:我不得不使用EntityManager.persist(association),该示例在鏈接上丢失了。

    所以我的最终解決方案是:

    @Entity
    public class Employee {
      @Id
      private long id;
      ...
      @OneToMany(mappedBy="employee")
      private List<ProjectAssociation> projects;
      ...
    }
    
    @Entity
    public class Project {
      @PersistenceContext
      EntityManager em;
      @Id
      private long id;
      ...
      @OneToMany(mappedBy="project")
      private List<ProjectAssociation> employees;
      ...
      // Add an employee to the project.
      // Create an association object for the relationship and set its data.
      public void addEmployee(Employee employee, boolean teamLead) {
        ProjectAssociation association = new ProjectAssociation();
        association.setEmployee(employee);
        association.setProject(this);
        association.setEmployeeId(employee.getId());
        association.setProjectId(this.getId());
        association.setIsTeamLead(teamLead);
        em.persist(association);
        this.employees.add(association);
        // Also add the association object to the employee.
        employee.getProjects().add(association);
      }
    }
    
    @Entity
    @Table(name="PROJ_EMP")
    @IdClass(ProjectAssociationId.class)
    public class ProjectAssociation {
      @Id
      private long employeeId;
      @Id
      private long projectId;
      @Column(name="IS_PROJECT_LEAD")
      private boolean isProjectLead;
      @ManyToOne
      @JoinColumn(name = "employeeId", updatable = false, insertable = false,
              referencedColumnName = "id")
      private Employee employee;
      @ManyToOne
      @JoinColumn(name = "projectId", updatable = false, insertable = false,
              referencedColumnName = "id")
      private Project project;
      ...
    }
    
    public class ProjectAssociationId implements Serializable {
      private long employeeId;
      private long projectId;
      ...
      public int hashCode() {
        return (int)(employeeId + projectId);
      }
      public boolean equals(Object object) {
        if (object instanceof ProjectAssociationId) {
          ProjectAssociationId otherId = (ProjectAssociationId) object;
          return (otherId.employeeId == this.employeeId) 
                  && (otherId.projectId == this.projectId);
        }
        return false;
      }
    }
    

  • python:如何在windows命令列的当前行上覆盖/print?
  • if statement:PHP-如果/其他,for,foreach,而-没有花括號?