springjpah2many-to-onemappedby

Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property?


Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.vitoria.models.Show.user_profile in com.vitoria.models.UserProfile.show

Hi! I'm trying to develop a system that associates UserProfile types to Show recommendations and right now I'm doing the basics: trying to make table relationships and I've been really stuck in this problem! I have tried so many combinations of mappedBy and different annotations but none of it seem to work.

Here's my code:

UserProfile:

package com.vitoria.models;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="user_profile")
public class UserProfile implements Serializable{
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="user_profile_id")
    private Integer id;
    
    @Column(name="user_profile_type")
    private String type;
    
    @OneToOne(mappedBy="user_profile")
    private User user;
    
    @OneToMany(mappedBy="user_profile")
    private List<Show> show;

    public UserProfile() {
        super();
    }

    public UserProfile(Integer id, String type, User user, List<Show> show) {
        super();
        this.id = id;
        this.type = type;
        this.user = user;
        this.show = show;
    }

    public Integer getId() {
        return id;
    }

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

    public String getType() {
        return type;
    }

    public void ListType(String type) {
        this.type = type;
    }

    public User getUser() {
        return user;
    }

    public void ListUser(User user) {
        this.user = user;
    }

    public List<Show> getshow() {
        return show;
    }

    public void ListShow(List<Show> show) {
        this.show= show;
    }
}

Show:

package com.vitoria.models;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
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="show")
public class Show implements Serializable{
    private static final long serialVersionUID = 1L;
    
    @Column(name="show_name", unique=true)
    private String name;
    
    @Column(name="show_genre")
    private String genre;
    
    @Column(name="show_synopsis")
    private String synopsis;
    
    @Column(name="show_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Id
    private Integer id;
    
    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name="user_profile_id")
    private UserProfile userProfile;

    public Show() {
    }

    public Show(String name, String genre, String synopsis, Integer id, UserProfile userProfile) {
        this.name = name;
        this.genre = genre;
        this.synopsis = synopsis;
        this.id = id;
        this.userProfile = userProfile;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getSynopsis() {
        return synopsis;
    }

    public void setSynopsis(String synopsis) {
        this.synopsis = synopsis;
    }

    public Integer getId() {
        return id;
    }

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

    public UserProfile getUserProfile() {
        return userProfile;
    }

    public void setUserProfile(UserProfile userProfile) {
        this.userProfile = userProfile;
    }
}

I know the problem is in the annotations but have NO idea where. If anyone has any tips of how I could solve this problem I'd be very grateful! <3


Solution

  • It is a misuse of mappedBy attribute. It must contain the name of a java class attribute. Not a name of a column.