我有两个带有两个实体(客户和购物车)的 Eclipse 项目。
项目客户微服务:
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int customerId;
private String name;
private String address;
@OneToOne
@JoinColumn(name = "cart_test")
private Cart cart;
public Customer(Cart cart) {
this.cart = cart;
项目 CartMicroService:
@Entity
public class Cart {
@Id
@JoinColumn(name = "cart_id")
private int cartId;
private Map<Integer, CartItem> cartItems;
private int numberOfArticles;
public Cart() {
cartItems = new HashMap<Integer, CartItem>();
numberOfArticles = 0;
}
我收到此错误:
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on de.leuphana.customer.component.behaviour.Customer.cart references an unknown entity: de.leuphana.cart.component.behaviour.Cart
如何连接这两个实体?我期望在我的数据库中出现类似的内容:
<item>
<customerId>1000</customerId>
<name>name</name>
<address>email@email.com</address>
<cart_test>Cart</cart_test>
<!-- OR something like -->
<cart_id>CartId</cart_id>
这是我的 Spring Boot 应用程序:
@SpringBootApplication
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
}
最佳答案
你不能这样做。 Hibernate 希望映射所使用的模型。将其映射到其他项目中不会给外部世界带来任何错误,因为它可以以任何方式密封。
解决方法是在某个模块中定义您的模型,该模型将由您的项目共享。
关于java - Spring Data JPA 与 H2 数据库 : Connect two Entities in different Projects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55069706/