错误:org.hibernate.MappingException:无法确定类型:java.util.List

时间:2017-07-21 14:39:19

标签: java hibernate spring-boot

我一直在尝试创建的多对多项目中出现映射异常错误。我已经尝试过在StackOverflow上的前一个答案中找到的所有内容。我也尝试下载更新版本的hibernate。

以下是我的产品和类别模型。如果有人知道我做错了什么,请。请告诉我。

产品型号

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

2 个答案:

答案 0 :(得分:1)

此处的问题是您使用@ManyToMany注释field方法。

只能通过直接注释getter或注释其field方法来映射类字段。

并且不要混合accessors级别和@ManyToMany级别的映射,如果您决定选择其中一个,那么请使用您的所有字段。

因此,在您的情况下,使用categories和相对注释与@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; 列表:

json.dump

答案 1 :(得分:0)

将您的多对多加入类别列表变量