当前位置: 首页 > 知识库问答 >
问题:

映射通过引用一个未知的目标实体属性-Hibernate错误Manyto很多

钮安歌
2023-03-14

尽管已经讨论了这个错误,但我不知道如何解决我的问题。我读到这个问题可能会被映射,所以我尝试了不同的选择,但问题仍然存在

控制台:

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个答案

令狐增
2023-03-14

这就是解决办法

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 say)

 类似资料:
  • 我的模型由不同类型的预算组成。这些预算具有不同的属性。我想在这些不同类型的预算之间建立一种“一对一”的关系。假设我有一个BudgetLevel1,它有很多BudgetLevel2,它也有很多BucketLevel3。所有这些BudgetLevel扩展了类预算 我的预算课程 预算级别1类 预算级别2类 预算级别3 看起来类似 这是我得到的错误 org.springframework.beans.fa

  • 我试图通过从域对象创建表来测试一对多映射,但我看到错误mappedBy引用未知的目标实体属性。谁能看一下吗? 谢谢 hibernate.cfg.xml 错误StackTrace 谢谢你的回复。我做了您提到的更改,还做了一些其他更改,比如向main.java添加session.save(flightvenive)和insertable=false,updatable=false以及@JoinColu

  • 首先,我的课程: 使用者 角色地图。JAVA 当我尝试在服务器上运行这个我有这样的异常:错误创建bean与名称'SessionFactory'定义在ServletContext资源[/WEB-INF/类/base Beans.xml]:调用init方法失败;嵌套的异常org.hibernate.注释异常:映射通过引用一个未知的目标实体属性:com.patpuc.model.ap.users在com

  • 问题内容: 我在被注释的对象中建立一对多关系时遇到问题。 我有以下几点: 然后这个 和这个 我在这里做错了什么 问题答案: 该属性正在引用,而该属性是,因此出现错误消息。因此,请将您的映射更改为: 或者将实体属性更改为(这就是我要做的)。 mapledBy参考指示“在我有一个用于查找配置的集合的东西上查找名为’customer’的bean属性。”

  • 我不熟悉Hibernate和JPA,正在尝试连接一个电影、评分员和评分的数据库。我一直收到由org.hibernate引起的错误。注释异常:通过引用映射未知目标实体属性:com.stephenalexander.projects.movierecommender.rating.rating。com.stephenalexander.projects.movierecommender.movie.m

  • 我正在做一个测试项目来学习Hibernate,不幸的是,我得到了这个错误,并查看了其他类似的错误,但是遵循它们并没有解决我的问题。我仍然得到这个错误。 如果有人能检查出哪里出了问题,我真的很想知道我的错误是什么。 我的模型类: } 这是第二个模型类: } 这是我的主要: 这是我的hibernate util类: 公共类HibernateUtil{私有静态最终会议工厂会议工厂=构建会议工厂(); }