Java :mappedBy 引用一个未知的目标实体属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19207939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:03:22  来源:igfitidea点击:

: mappedBy reference an unknown target entity property

javaspringhibernatejpa

提问by SandeepKumar

I am working on a simple practice application for using hibernate. It has simple mapping like a manufacturer can have many mobiles. But a mobile can only be manufactured by single manufacturer. Here is what I think the code should be.

我正在开发一个使用 hibernate 的简单练习应用程序。它具有简单的映射,就像制造商可以拥有许多手机一样。但是手机只能由单一制造商制造。这是我认为的代码应该是什么。

package mobileconsumers.entity.dto;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;


@Entity
@Table(name="ms_ref_mobile")
public class MobileDTO {

private Long id;
private String model;
private ManufacturerDTO manufacturerDTO;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="MOBILE_ID")
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getModel() {
    return model;
}
public void setModel(String model) {
    this.model = model;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="MANUFACTURER_ID")
public ManufacturerDTO getManufacturer() {
    return manufacturerDTO;
}
public void setManufacturer(ManufacturerDTO manufacturer) {
    this.manufacturerDTO = manufacturer;
}


}

this is the second dto

这是第二个 dto

package mobileconsumers.entity.dto;

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.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="ms_ref_manufacturer")
public class ManufacturerDTO {

private Long id;
private String name;
private Set<MobileDTO> mobileDTOs = new HashSet<>(0);

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="MANUFACTURER_ID")
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
@Column(name="NAME")
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@OneToMany(fetch=FetchType.LAZY,mappedBy="manufacturerDTO")
public Set<MobileDTO> getMobileDTOs() {
    return mobileDTOs;
}
public void setMobileDTOs(Set<MobileDTO> mobileDTOs) {
    this.mobileDTOs = mobileDTOs;
}


}

When I try to start my server it gives me an error saying.. org.hibernate.AnnotationException: mappedBy reference anunknown target entity property: mobileconsumers.entity.dto.MobileDTO.manufacturerDTO in mobileconsumers.entity.dto.ManufacturerDTO.mobileDTOsThe mapping seems to be fine to me. there must be something really silly that I am missing out. Just needed fresh pair of eyes to look at my code and figure out whats going wrong..

当我尝试启动我的服务器时,它给了我一个错误说.. org.hibernate.AnnotationException: mappedBy reference anunknown target entity property: mobileconsumers.entity.dto.MobileDTO.manufacturerDTO in mobileconsumers.entity.dto.ManufacturerDTO.mobileDTOs映射对我来说似乎很好。我错过了一些非常愚蠢的东西。只需要一双新的眼睛来看看我的代码并找出出了什么问题..

采纳答案by Debojit Saikia

Change mappedByvalue, so that it refers the manufacturerproperty on the @ManyToOneside of the association:

更改mappedBy值,使其引用关联侧的manufacturer属性@ManyToOne

@OneToMany(fetch=FetchType.LAZY,mappedBy="manufacturer")
public Set<MobileDTO> getMobileDTOs() {
    return mobileDTOs;
}