Cenasa
Cenasa

Reputation: 601

Spring Data JPA with H2 Database: Connect two Entities in different Projects

I have two Eclipse Projects with two Entities (Customer and Cart).

Projekt CustomerMicroService:

    @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;

Project 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;
    }

I am getting this error:

org.hibernate.AnnotationException: @OneToOne or @ManyToOne on de.leuphana.customer.component.behaviour.Customer.cart references an unknown entity: de.leuphana.cart.component.behaviour.Cart

How do I connect those two Entities? I am expecting something like this in my Database:

<item>
  <customerId>1000</customerId>
  <name>name</name>
  <address>[email protected]</address>
  <cart_test>Cart</cart_test>
  <!-- OR something like -->
  <cart_id>CartId</cart_id>

This is my Spring Boot Application:

@SpringBootApplication
public class CustomerApplication {

    public static void main(String[] args) {
        SpringApplication.run(CustomerApplication.class, args);
    }

}

Upvotes: 0

Views: 424

Answers (1)

Andronicus
Andronicus

Reputation: 26066

You cannot do this. Hibernate expects used model to be mapped. Mapping it in other project doesn't give any error to the outer world as it can be sealed in any way.

The workaround would be to define your model in some module, that would be shared by your projects.

Upvotes: 1

Related Questions