【发布时间】: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