简体   繁体   中英

Hibernate Mapping Classes

The scenario is following: Im implementing a shoppingcart, where a Customer can choose products from a productcatalog.

I have issues with the mapping of my Java classes in Hibernate correctly, maybe you can help me.

public class Shoppingcart { 
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private Customer customer;
    @OneToMany(mappedBy="cartID")
    private Set<ShoppingcartItem> cartItems = new HashSet<>();
    //...
}
public class Customer{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @OneToOne(mappedBy="CUSTID")
    private long id;
    @Column(name="CUSTOMERFIRSTNAME")
    private String firstname;
    @Column(name="CUSTOMERLASTNAME")
    private String lastname;
    //...
}

public class Product {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private double price;
    private String product;
    //...
}
public class ShoppingcartItem {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long itemId; 
    private Shoppingcart shoppingcart; 
    @ManyToOne
    private Product product;
    private int amount; 
    //...
}

My DB structure is the following:

Customer

  • id
  • fistname
  • lastname

Shoppingcart

  • id
  • customer

Shoppingcart_item

  • id
  • cartId
  • productId
  • amount

Product

  • id
  • price
  • name

When i run my code I get the following exception: Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.OrderManagementMaven.bo.ShoppingcartItem.product references an unknown entity: com.OrderManagementMaven.bo.Product

What are my Mistakes / what do I need to change?

Your entities are missing the @Entity annotation:

@Entity
public class Shoppingcart { 
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private Customer customer;
    @OneToMany(mappedBy="cartID")
    private Set<ShoppingcartItem> cartItems = new HashSet<>();
    //...
}

@Entity
public class Customer{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @OneToOne(mappedBy="CUSTID")
    private long id;
    @Column(name="CUSTOMERFIRSTNAME")
    private String firstname;
    @Column(name="CUSTOMERLASTNAME")
    private String lastname;
    //...
}

@Entity
public class Product {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private double price;
    private String product;
    //...
}

@Entity
public class ShoppingcartItem {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long itemId; 
    private Shoppingcart shoppingcart; 
    @ManyToOne
    private Product product;
    private int amount;    
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM