【问题标题】:Spring-data-JPA with foreign key reference to "UserID" within Composite Key attempting to map to column "user" instead of UserIDSpring-data-JPA,外键引用复合键中的“UserID”,试图映射到列“user”而不是 UserID
【发布时间】:2018-05-12 09:21:43
【问题描述】:

我正在尝试在 Spring MVC 应用程序中连接到我的数据库。有两张桌子。 Users 和 Orders,Users 有一个主键列:“userID”,orders 有一个来自列的组合键:“userID”和“orderID”,其中 userID 是引用 Users 表中“userID”列的外键。

这是我的课程:

订单:

@Entity
@Table(name = "Orders")
@IdClass(OrderPK.class)
public class Order implements Serializable{

    private static final Long serialVersionUID = 1L;

    @EmbeddedId
    private OrderPK orderPK;

    //other properties
    
    //no args and full args constructor

    //getters and setters

    //toString

}

订单PK:

@Embeddable
public class OrderPK implements Serializable {
    @Column(name = "orderID")
    private Long orderID;

    @ManyToOne
    @JoinColumn(name = "userID")
    private User user;

    public OrderPK() {
    }

    public OrderPK(Long orderID, User user) {
        this.orderID = orderID;
        this.user = user;
    }

    public Long getOrderID() {
        return orderID;
    }

    public void setOrderID(Long orderID) {
        this.orderID = orderID;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof OrderPK)) return false;
        OrderPK that = (OrderPK) o;
        return Objects.equals(getOrderID(), that.getOrderID()) &&
            Objects.equals(getUser(), that.getUser());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getOrderID(), getUser());
    }
}

用户:

@Entity
@Table(name = "USERS")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="USER_SEQUENCE", sequenceName="USER_SEQUENCE")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, 
    generator="USER_SEQUENCE")
    @Column(name = "userid")
    private Long userId;

    //other properties
    
    //no args and full args constructor

    //getters and setters

    //toString
}

当我尝试连接到数据库时,出现以下异常:

org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class] 中定义名称为“entityManagerFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException: Unable to find properties (orderID, user) in annotated with @IdClass:com.ex.evemarketback.domain.Order

...

原因:org.hibernate.AnnotationException:无法在使用@IdClass:com.ex.evemarketback.domain.Order 注释的实体中找到属性(orderID、用户)

有什么建议吗?

【问题讨论】:

    标签: spring-mvc jpa foreign-keys spring-data composite-primary-key


    【解决方案1】:

    当您使用@EmbeddedId 时,您不需要@IdClass 注释:

    @Entity
    @Table(name = "Orders")
    public class Order implements Serializable{
    

    或者如果你想保留@IdClass:

    // @Embeddable - no need for that
    public class OrderPK implements Serializable {
        private Long orderID;
        private Long userId;
    
        ...
    }
    

    实体:

    @Entity
    @Table(name = "Orders")
    @IdClass(OrderPK.class)
    public class Order implements Serializable{
    
        @Id
        @Column(name = "orderID")
        private Long orderID;
    
        @Id
        @Column(name = "userId", insertable=false, updatable=false)
        private Long userId;
    
        @ManyToOne
        @JoinColumn(name = "userID")
        private User user;
    

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 2017-02-21
      • 2019-04-22
      • 2011-03-20
      • 2017-01-30
      • 2016-11-08
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      相关资源
      最近更新 更多