2017-07-21 46 views
1

我一直在爲我嘗試創建的多對多項目獲取映射異常錯誤。我已經嘗試了在StackOverflow上的以前的答案中找到的所有內容。我也嘗試下載更新版本的hibernate。錯誤:org.hibernate.MappingException:無法確定類型爲:java.util.List

這裏是我的產品和類別的模型。請,如果任何人有任何想法我做錯了什麼。請告訴我。

產品型號

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

分類模型

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

,你爲什麼和@ManyToMany的onCreate方法代替的getProducts和getCategories註釋? – NoDataFound

回答

0

anotate您的多對多連接的類別

1

的List變量這裏的問題是,你是用@ManyToMany註解onCreate方法。

只能通過直接註釋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; 
+1

好方法,加上一個雖然:) – Yahya

相關問題