mappedBy引用一个未知的目标实体属性--hibernate错误ManytoMany

时间:2016-09-20 15:50:41

标签: java hibernate

我无法弄清楚如何解决我的问题,尽管已经讨论过这个错误。我已经读过问题可以映射,所以我尝试了不同的替代方案,但问题仍然存在

控制台:

Exception in thread "main" org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: oodb.Model.articles in oodb.Article.models
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:775)
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:725)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
    at testoodb.CreaArticolo.main(CreaArticolo.java:20)

Model.java:

package oodb;

import static javax.persistence.GenerationType.IDENTITY;

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.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;



@Entity
@Table(name = "model", catalog = "oogvg", uniqueConstraints = {
        @UniqueConstraint(columnNames = "code")})
public class Model {
    private Integer ModelId;
    private String code;
    private String description;
    private Double cost;
    private Double price;  //selling price
    private Set<Article> articles= new HashSet<Article>(0); // lista di articoli

    public Model(){};

    public Model(String code, String description, Double cost, Double price) {
        super();
        this.code = code;
        this.description = description;
        this.cost = cost;
        this.price = price;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "model_id", unique = true, nullable = false)
    public Integer getModelId() {
        return ModelId;
    }

    public void setModelId(Integer modelId) {
        ModelId = modelId;
    }

    @Column(name = "code", unique = true, nullable = false, length = 10)
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Column(name = "description")
    public String getDescription() {
        return description;
    }

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

    @Column(name = "cost")
    public Double getCost() {
        return cost;
    }

    public void setCost(Double cost) {
        this.cost = cost;
    }

    @Column(name = "price")
    public Double getPrice() {
        return price;
    }

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


    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "model_article", catalog = "oogvg", joinColumns = {
            @JoinColumn(name = "model_id", nullable = false, updatable = false) },
            inverseJoinColumns = { @JoinColumn(name = "article_id",
                    nullable = false, updatable = false) })
    public Set<Article> getArticle() {
        return articles;
    }

    public void setArticle(Set<Article> article) {
        this.articles = article;
    }

    @Override
    public String toString() {
        return "Model [ModelId=" + ModelId + ", code=" + code + ", description=" + description + ", cost=" + cost
                + ", price=" + price + "]";
    }




}

Article.java

package oodb;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;


@Entity
@Table(name = "article", catalog = "oogvg")
public class Article {
    private Integer id;
    private String ean;
    private Integer quantity;
    private String color; // stringa exa
    private String size;
    private Integer pos;
    private Set<Model> models= new HashSet<Model>(0);

    public Article(){};

    public Article(String ean, Integer quantity, String color, String size, Integer pos) {
        super();
        this.ean = ean;
        this.quantity = quantity;
        this.color = color;
        this.size = size;
        this.pos = pos;

    }


    public Article(String ean, Integer quantity, String color, String size, Integer pos, Set<Model> models) {
        super();
        this.ean = ean;
        this.quantity = quantity;
        this.color = color;
        this.size = size;
        this.pos = pos;
        this.models = models;
    }




    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "article_id", unique = true, nullable = false)
    public Integer getId() {
        return id;
    }

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

    @Column(name = "ean")
    public String getEan() {
        return ean;
    }

    public void setEan(String ean) {
        this.ean = ean;
    }

    @Column(name = "quantity")
    public Integer getQuantity() {
        return quantity;
    }

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

    @Column(name = "color")
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }



    @Column(name = "size")
    public String getTaglia() {
        return size;
    }




    public void setTaglia(String size) {
        this.size = size;
    }



    @Column(name = "posizione")
    public Integer getPos() {
        return pos;
    }




    public void setPos(Integer pos) {
        this.pos = pos;
    }




    @ManyToMany(fetch = FetchType.EAGER, mappedBy = "articles")
    public Set<Model> getModels() {
        return models;
    }

    public void setModels(Set<Model> models) {
        this.models = models;
    }

    @Override
    public String toString() {
        return "Article [id=" + id + ", ean=" + ean + ", quantity=" + quantity + ", color=" + color + ", models="
                + models + "]";
    }



}

1 个答案:

答案 0 :(得分:0)

这是解决方案

Model.java:

package oodb;

import static javax.persistence.GenerationType.IDENTITY;

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.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;



@Entity
@Table(name = "model", catalog = "oogvg", uniqueConstraints = {
        @UniqueConstraint(columnNames = "code")})
public class Model {
    private Integer ModelId;
    private String code;
    private String description;
    private Double cost;
    private Double price;  //selling price
    private Set<Article> articles= new HashSet<Article>(0); // lista di articoli

    public Model(){};

    public Model(String code, String description, Double cost, Double price) {
        super();
        this.code = code;
        this.description = description;
        this.cost = cost;
        this.price = price;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "model_id", unique = true, nullable = false)
    public Integer getModelId() {
        return ModelId;
    }

    public void setModelId(Integer modelId) {
        ModelId = modelId;
    }

    @Column(name = "code", unique = true, nullable = false, length = 10)
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Column(name = "description")
    public String getDescription() {
        return description;
    }

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

    @Column(name = "cost")
    public Double getCost() {
        return cost;
    }

    public void setCost(Double cost) {
        this.cost = cost;
    }

    @Column(name = "price")
    public Double getPrice() {
        return price;
    }

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


    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "model_article", catalog = "oogvg", joinColumns = {
            @JoinColumn(name = "model_id", nullable = false, updatable = false) },
            inverseJoinColumns = { @JoinColumn(name = "article_id",
                    nullable = false, updatable = false) })
    public Set<Article> getArticles() {
        return articles;
    }

    public void setArticles(Set<Article> articles) {
        this.articles = articles;
    }

    @Override
    public String toString() {
        return "Model [ModelId=" + ModelId + ", code=" + code + ", description=" + description + ", cost=" + cost
                + ", price=" + price + "]";
    }




}

错误的部分:

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "model_article", catalog = "oogvg", joinColumns = {
            @JoinColumn(name = "model_id", nullable = false, updatable = false) },
            inverseJoinColumns = { @JoinColumn(name = "article_id",
                    nullable = false, updatable = false) })
    public Set<Article> getArticle() {
        return articles;
    }

    public void setArticle(Set<Article> article) {
        this.articles = article;
    }

其中函数的名称与文章不匹配(如JB Nized所说)