Many to Many Relationship Unmapping issue

Hi,

Because of not appearing hibernate annotations by using hibernate reverse engineering, I have to define all classes based on database schemas in the database.

I defined all mapping class in the hibernate.cfg.xml .

?

1 Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.bookstore.entity.OrderDetail.pk.BOOK in com.bookstore.entity.Book.orderDetails

Here are the essential files including book,orders,orderdetail and orderdetailid.

Book class

package com.bookstore.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.Arrays;
import java.util.Base64;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name="BOOK",catalog = "JSPPROJECTDATABASE")
public class Book implements Serializable{

	@Id
	@SequenceGenerator(name="BOOK_SEQ", sequenceName="BOOK_SEQ", allocationSize=1)
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="BOOK_SEQ")
	@Column(name="BOOK_ID", nullable = false)
	private int id;
	
	@Column(name="TITLE")
	private String title;
	
	@Column(name="AUTHOR")
	private String author;
	
	@Column(name="DESCRIPTION")
	private String description;
	
	@Column(name="ISBN")
	private String isbn;
	
	@Column(name="DESCRIPTION")
	private byte[] image;
	
	@Transient
	private String base64Image;
	
	@Column(name="PRICE")
	private float price;
	
	@Column(name="PUBLISH_DATE")
	private Date publishDate;
	
	@Column(name="LAST_UPDATED_DATE")
	private Date lastUpdatedDate;
	
	@Column(name="QUANTITY")
	private int quantity;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "CATEGORY_ID", nullable = false)
	private Category category;
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "bookReview")
	private Set<Review> reviews = new HashSet<Review>();
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.BOOK", cascade=CascadeType.ALL)
	private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();

	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Book(String title, String author, String description, String isbn, byte[] image, float price,
			Date publishDate, Date lastUpdatedDate, int quantity, Category category) {
		super();
		this.title = title;
		this.author = author;
		this.description = description;
		this.isbn = isbn;
		this.image = image;
		this.price = price;
		this.publishDate = publishDate;
		this.lastUpdatedDate = lastUpdatedDate;
		this.quantity = quantity;
		this.category = category;
	}
	
	public Book(String title, String author, String description, String isbn, byte[] image, String base64Image,
			float price, Date publishDate, Date lastUpdatedDate, int quantity, Category category, Set<Review> reviews,
			Set<OrderDetail> orderDetails) {
		super();
		this.title = title;
		this.author = author;
		this.description = description;
		this.isbn = isbn;
		this.image = image;
		this.base64Image = base64Image;
		this.price = price;
		this.publishDate = publishDate;
		this.lastUpdatedDate = lastUpdatedDate;
		this.quantity = quantity;
		this.category = category;
		this.reviews = reviews;
		this.orderDetails = orderDetails;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public byte[] getImage() {
		return image;
	}

	public void setImage(byte[] image) {
		this.image = image;
	}

	public String getBase64Image() {
		this.base64Image = Base64.getEncoder().encodeToString(this.image);
	    return base64Image;
	}

	public void setBase64Image(String base64Image) {
		this.base64Image = base64Image;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public Date getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(Date publishDate) {
		this.publishDate = publishDate;
	}

	public Date getLastUpdatedDate() {
		return lastUpdatedDate;
	}

	public void setLastUpdatedDate(Date lastUpdatedDate) {
		this.lastUpdatedDate = lastUpdatedDate;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}
	

	public Set<Review> getReviews() {
		return reviews;
	}

	public void setReviews(Set<Review> reviews) {
		this.reviews = reviews;
	}

	public Set<OrderDetail> getOrderDetails() {
		return orderDetails;
	}

	public void setOrderDetails(Set<OrderDetail> orderDetails) {
		this.orderDetails = orderDetails;
	}

	@Override
	public String toString() {
		return "Book [id=" + id + ", title=" + title + ", author=" + author + ", description=" + description + ", isbn="
				+ isbn + ", image=" + Arrays.toString(image) + ", base64Image=" + base64Image + ", price=" + price
				+ ", publishDate=" + publishDate + ", lastUpdatedDate=" + lastUpdatedDate + ", quantity=" + quantity
				+ ", category=" + category + ", reviews=" + reviews + ", orderDetails=" + orderDetails + "]";
	}

	
}

OrderDetail class

package com.bookstore.entity;

import java.io.Serializable;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "ORDER_DETAIL", catalog = "JSPPROJECTDATABASE")
@AssociationOverrides({
		@AssociationOverride(name = "pk.ORDERS", 
			joinColumns = @JoinColumn(name = "ORDER_ID")),
		@AssociationOverride(name = "pk.BOOK", 
			joinColumns = @JoinColumn(name = "BOOK_ID")) })
public class OrderDetail implements Serializable{

	private OrderDetailId pk = new OrderDetailId();
	
	@Column(name="QUANTITY")
	private int quantity;
	
	@Column(name="SUBTOTAL")
	private float subTotal;
	
	public OrderDetail() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	@EmbeddedId
	public OrderDetailId getPk() {
		return pk;
	}

	public void setPk(OrderDetailId pk) {
		this.pk = pk;
	}
	
	@Transient
	public Book getBook() {
		return getPk().getBooks();
	}

	public void setBook(Book book) {
		getPk().setBooks(book);
	}

	@Transient
	public Orders getOrders() {
		return getPk().getOrder();
	}

	public void setOrders(Orders orders) {
		getPk().setOrder(orders);
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public float getSubTotal() {
		return subTotal;
	}

	public void setSubTotal(float subTotal) {
		this.subTotal = subTotal;
	}

}

Orders class

package com.bookstore.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="ORDERS",catalog = "JSPPROJECTDATABASE")
public class Orders implements Serializable{

	@Id
	@SequenceGenerator(name="ORDERS_SEQ", sequenceName="ORDERS_SEQ", allocationSize=1)
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ORDERS_SEQ")
	@Column(name="ORDER_ID", nullable = false)
	private int id;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "CUSTOMER_ID", nullable = false)
	private Customer customerOrders;
	
	@Column(name="ORDERDATE")
	private Date orderDate;
	
	@Column(name="SHIPPING_ADDRESS")
	private String shippingAddress;
	
	@Column(name="RECIPIENT_NAME")
	private String recipentName;
	
	@Column(name="RECIPIENT_PHONE")
	private String recipentPhone;
	
	@Column(name="PAYMENT_METHOD")
	private String paymentMethod;
	
	@Column(name="ORDER_TOTAL")
	private float orderTotal;
	
	@Column(name="ORDER_STATUS")
	private String orderStatus;
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.ORDERS", cascade=CascadeType.ALL)
	private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();

	public Orders() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Orders(Customer customer, Date orderDate, String shippingAddress, String recipentName, String recipentPhone,
			String paymentMethod, float orderTotal, String orderStatus) {
		super();
		this.customerOrders = customer;
		this.orderDate = orderDate;
		this.shippingAddress = shippingAddress;
		this.recipentName = recipentName;
		this.recipentPhone = recipentPhone;
		this.paymentMethod = paymentMethod;
		this.orderTotal = orderTotal;
		this.orderStatus = orderStatus;
	}
	
	
	public Orders(Customer customerOrders, Date orderDate, String shippingAddress, String recipentName,
			String recipentPhone, String paymentMethod, float orderTotal, String orderStatus,
			Set<OrderDetail> orderDetails) {
		super();
		this.customerOrders = customerOrders;
		this.orderDate = orderDate;
		this.shippingAddress = shippingAddress;
		this.recipentName = recipentName;
		this.recipentPhone = recipentPhone;
		this.paymentMethod = paymentMethod;
		this.orderTotal = orderTotal;
		this.orderStatus = orderStatus;
		this.orderDetails = orderDetails;
	}

	public Customer getCustomerOrders() {
		return customerOrders;
	}

	public void setCustomerOrders(Customer customerOrders) {
		this.customerOrders = customerOrders;
	}

	public Set<OrderDetail> getOrderDetails() {
		return orderDetails;
	}

	public void setOrderDetails(Set<OrderDetail> orderDetails) {
		this.orderDetails = orderDetails;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Customer getCustomer() {
		return customerOrders;
	}

	public void setCustomer(Customer customer) {
		this.customerOrders = customer;
	}

	public Date getOrderDate() {
		return orderDate;
	}

	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}

	public String getShippingAddress() {
		return shippingAddress;
	}

	public void setShippingAddress(String shippingAddress) {
		this.shippingAddress = shippingAddress;
	}

	public String getRecipentName() {
		return recipentName;
	}

	public void setRecipentName(String recipentName) {
		this.recipentName = recipentName;
	}

	public String getRecipentPhone() {
		return recipentPhone;
	}

	public void setRecipentPhone(String recipentPhone) {
		this.recipentPhone = recipentPhone;
	}

	public String getPaymentMethod() {
		return paymentMethod;
	}

	public void setPaymentMethod(String paymentMethod) {
		this.paymentMethod = paymentMethod;
	}

	public float getOrderTotal() {
		return orderTotal;
	}

	public void setOrderTotal(float orderTotal) {
		this.orderTotal = orderTotal;
	}

	public String getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(String orderStatus) {
		this.orderStatus = orderStatus;
	}

	@Override
	public String toString() {
		return "Orders [id=" + id + ", customerOrders=" + customerOrders + ", orderDate=" + orderDate
				+ ", shippingAddress=" + shippingAddress + ", recipentName=" + recipentName + ", recipentPhone="
				+ recipentPhone + ", paymentMethod=" + paymentMethod + ", orderTotal=" + orderTotal + ", orderStatus="
				+ orderStatus + ", orderDetails=" + orderDetails + "]";
	}
	
	
}

OrderDetailId class

package com.bookstore.entity;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Embeddable
public class OrderDetailId implements Serializable{

	@ManyToOne(cascade = CascadeType.ALL)
	private Book books;
	
	@ManyToOne(cascade = CascadeType.ALL)
	private Orders order;
	
	public Book getBooks() {
		return books;
	}
	public void setBooks(Book books) {
		this.books = books;
	}
	public Orders getOrder() {
		return order;
	}
	public void setOrder(Orders order) {
		this.order = order;
	}
	
}

Error
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.bookstore.entity.OrderDetail.pk.BOOK in com.bookstore.entity.Book.orderDetails

Where did I do thing wrong.

How can I fix it?

I’ll appreciate if you can help me.

Thank you.