java - Spring Data JPA 与 H2 数据库 : Connect two Entities in different Projects

标签 java spring hibernate spring-boot spring-data-jpa

我有两个带有两个实体(客户和购物车)的 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/

相关文章:

java - 如何在 hibernate 中正确设置双向关联@OneToMany

java - JPA系统异常访问字段错误

java - 尝试在单击“取消”按钮时处理扩展 JXPanel 的类

java - JNLP 不在 Java 1.8 客户端上创建桌面快捷方式

java - 如何从文件中逐行读入 JTextArea (GUI)?

java - MessageConversionException 的通用处理程序

java - 在 Java 中使用 int a=a+1 和 a++ 的性能差异

java - Spring 应用程序最简单的 main() 方法是什么?

java - JDBC 复制驱动程序总是在没有 Activity 缓存的情况下返回相同的数据

java - JPQL CASE 语句中的 WHERE 子句问题