I keep getting the mapping exception error for a many to many project I'm trying to create. I have already tried everything that I found in previous answers on StackOverflow. I have also tried downloading a more recent version of hibernate.
Here are my models for Products and Categories. Please, if anyone has any idea of what I'm doing wrong. Please let me know.
PRODUCT MODEL
package com.cheryl.productgroups.models;
import java.util.Date;
import java.util.List;
import javax.management.relation.Role;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.JoinColumn;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name="products")
public class Product {
@Id
@GeneratedValue
private Long id;
private String name;
private String description;
private float price;
private Date createdAt;
private Date updatedAt;
@Access(AccessType.PROPERTY)
@ManyToMany(fetch = FetchType.LAZY)
@ElementCollection(targetClass=Role.class)
@JoinTable(
name = "categories_products",
joinColumns = @JoinColumn(name = "product_id"),
inverseJoinColumns = @JoinColumn(name = "category_id")
)
@PrePersist
protected void onCreate(){
this.createdAt = new Date();
}
@PreUpdate
protected void onUpdate(){
this.updatedAt = new Date();
}
private List<Category> categories;
public Product() {
}
public Product(String name, String description, float price) {
this.name = name;
this.description = description;
this.price = price;
this.createdAt = new Date();
this.updatedAt = new Date();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
@Transient
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
}
CATEGORY MODEL
package com.cheryl.productgroups.models;
import java.util.Date;
import java.util.List;
import javax.management.relation.Role;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.JoinColumn;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name="categories")
public class Category {
@Id
@GeneratedValue
private Long id;
private String name;
private Date createdAt;
private Date updatedAt;
@Access(AccessType.PROPERTY)
@ManyToMany(fetch = FetchType.LAZY)
@ElementCollection(targetClass=Role.class)
@JoinTable(
name = "categories_products",
joinColumns = @JoinColumn(name = "category_id"),
inverseJoinColumns = @JoinColumn(name = "product_id")
)
@PrePersist
protected void onCreate(){
this.createdAt = new Date();
}
@PreUpdate
protected void onUpdate(){
this.updatedAt = new Date();
}
private List<Product> products;
public Category() {
}
public Category(String name) {
this.name = name;
this.createdAt = new Date();
this.updatedAt = new Date();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
@Transient
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}