Spring-data-JPA 外键引用 "UserID" 复合键试图映射到列 "user" 而不是 UserID
Spring-data-JPA with foreign key reference to "UserID" within Composite Key attempting to map to column "user" instead of UserID
我正在尝试在 Spring MVC 应用程序中连接到我的数据库。有两个 table。用户和订单,用户有一个主键列:“userID”,订单有一个来自列的复合键:“userID”和“orderID”,其中 userID 是引用 Users [=32= 中的“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: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to find properties (orderID, user) in entity annotated with @IdClass:com.ex.evemarketback.domain.Order
...
Caused by: org.hibernate.AnnotationException: Unable to find properties (orderID, user) in entity annotated with @IdClass:com.ex.evemarketback.domain.Order
有什么建议吗?
由于您正在使用 @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;
我正在尝试在 Spring MVC 应用程序中连接到我的数据库。有两个 table。用户和订单,用户有一个主键列:“userID”,订单有一个来自列的复合键:“userID”和“orderID”,其中 userID 是引用 Users [=32= 中的“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: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to find properties (orderID, user) in entity annotated with @IdClass:com.ex.evemarketback.domain.Order
...
Caused by: org.hibernate.AnnotationException: Unable to find properties (orderID, user) in entity annotated with @IdClass:com.ex.evemarketback.domain.Order
有什么建议吗?
由于您正在使用 @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;