【问题标题】:JPA Composite PK referenced by FKs in One to Many RelationshipFK 在一对多关系中引用的 JPA 组合 PK
【发布时间】:2020-02-07 14:45:56
【问题描述】:

我正在尝试将下表映射到 JPA。 user_tax 和 taxuser_tax 和 user 之间的关系是一对多。我有一个复合主键,我需要将外键映射到这两个键,这让我感到困惑。

错误信息:org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: entity.Tax.user_tax in entity.UserTax.taxs

             tax           user_tax         user
           --------        --------        ------
         PK|t_id  |--------| t_id |PK-FK  |u_name|
           |t_name|   PK-FK| u_id |-------|u_id  | PK
           |      |        | name |       |      |

这是我的实体:

 @Entity
 @Table(name = "user")
 @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    public class User implements Serializable {

       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
         private Long id;

       @Column(name="u_name")
         private String uname;

          getters + setters
        }

 @Entity
 @Table(name = "tax")
 @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    public class Tax implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
          private Long id;

        @Column(name = "t_name")
          private String tname;

   @Embeddable
    public class UserTaxId implements Serializable {

      @Column(name="u_id")
        private Long uId;

        @Column(name="t_id")
        private Long tId;

  @Entity
  @Table(name = "user_tax")
  @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    public class UserTax implements Serializable {

      @EmbeddedId
        private UserTaxId userTaxId;

      @OneToMany(fetch = FetchType.LAZY, mappedBy = "user_tax")
        private List<User> users;

      @OneToMany(fetch = FetchType.LAZY, mappedBy = "user_tax")
        private List<Tax> taxs;

【问题讨论】:

    标签: spring jpa foreign-keys one-to-many composite-key


    【解决方案1】:

    您的 1:n 映射是向后的(即 UserTax 只能有一个 User 和一个 Tax)并且您使用的是派生标识。尝试像这样映射UserTax

    @Entity
    @Table(name = "user_tax")
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    public class UserTax implements Serializable {
    
        @EmbeddedId
        private UserTaxId userTaxId;
    
        @ManyToOne(fetch = FetchType.LAZY)
        @MapsId("uId") // maps uId attribute of embedded id
        private User user;
    
        @ManyToOne(fetch = FetchType.LAZY)
        @MapsId("tId") // maps tId attribute of embedded id
        private Tax tax;
    
        ...
    }
    

    在第 2.4.1 节的JPA 2.2 spec 中讨论了派生的身份(带有示例)。

    【讨论】:

      【解决方案2】:

      经过 3 天的研究,我将在这里发布对我有用的东西。

      Brian Vosburgh 正确发布了 UserTax 类:

       @Entity
      @Table(name = "user_tax")
      @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
      public class UserTax implements Serializable {
      
          @EmbeddedId
          private UserTaxId userTaxId;
      
          @ManyToOne(fetch = FetchType.LAZY)
          @MapsId("uId") // maps uId attribute of embedded id
          private User user;
      
          @ManyToOne(fetch = FetchType.LAZY)
          @MapsId("tId") // maps tId attribute of embedded id
          private Tax tax;
      
          ...
      }
      

      但是,我收到错误消息并且我的代码没有编译。然后我还必须编辑 User 和 Tax 类:

       @Entity
           @Table(name = "user")
           @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
              public class User implements Serializable {
      
                 @Id
                 @GeneratedValue(strategy = GenerationType.IDENTITY)
                   private Long id;
      
                 @OneToMany(
                  mappedBy = "tid",
                  cascade = CascadeType.ALL,
                  orphanRemoval = true
              )
              private List<UserTax> tax = new ArrayList<>();
      
                 @Column(name="u_name")
                   private String uname;
      
                    getters + setters
                  }
      
          @Entity
           @Table(name = "tax")
           @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
              public class Tax implements Serializable {
      
                  @Id
                  @GeneratedValue(strategy = GenerationType.IDENTITY)
                    private Long id;
      
                 @OneToMany(
                  mappedBy = "uid",
                  cascade = CascadeType.ALL,
                  orphanRemoval = true
              )
              private List<UserTax> taxs = new ArrayList<>();
      
                  @Column(name = "t_name")
                    private String tname;
      
          setters+getters
      
          }
      

      这是我找到问题解决方案的链接:https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/

      【讨论】:

        猜你喜欢
        • 2011-12-21
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-13
        • 2021-10-09
        相关资源
        最近更新 更多