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..
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With