C. Wilson
C. Wilson

Reputation: 11

Error: org.hibernate.MappingException: Could not determine type for: java.util.List

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

Upvotes: 1

Views: 291

Answers (2)

cнŝdk
cнŝdk

Reputation: 32145

The problem here is that you are annotating the onCreate method with @ManyToMany.

Only class fields can be mapped, either by annotating the field directly or annotating its getter method.

And don't mix both field level and accessors level mapping, if you decided to choose one of them then do it with all your fields.

So in your case use the @ManyToMany and the relative annotations with the categories list:

@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")
)
private List<Category> categories;

Upvotes: 1

shreyansh jogi
shreyansh jogi

Reputation: 2102

anotate your many to many join on List variable of categories

Upvotes: 0

Related Questions