Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

: mappedBy reference an unknown target entity property

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.

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

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.mobileDTOs The 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..

like image 586
SandeepKumar Avatar asked Oct 06 '13 10:10

SandeepKumar


1 Answers

Change mappedBy value, so that it refers the manufacturer property on the @ManyToOne side of the association:

@OneToMany(fetch=FetchType.LAZY,mappedBy="manufacturer")
public Set<MobileDTO> getMobileDTOs() {
    return mobileDTOs;
}
like image 185
Debojit Saikia Avatar answered Nov 12 '22 23:11

Debojit Saikia