Unknown mappedBy in:… referenced property unknown

I tried everything what I found on the internet but it isn’t want to work. I have to entities: User and Car. Both of them in package hiber.model. And after invoke code below, I get Unknown mappedBy in: hiber.model.User.car, referenced property unknown: hiber.model.Car.user. But when I delete mappedBy option then I get @OneToOne or @ManyToOne on hiber.model.User.car references an unknown entity: hiber.model.Car. Both of classes put in properties. What is it can be? This code is already like on every site on the internet. Project on github

@Table(name = "users")
public class User {

   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;

   @Column(name = "name")
   private String firstName;

   @Column(name = "last_name")
   private String lastName;

   @Column(name = "email")
   private String email;

   @OneToOne(mappedBy = "user")
   private Car car;

Constructors, getters, setters...


    public class Car {
        private String model;
        private String series;
        @JoinColumn(name = "id")
        private User user;

Getters, setters...


Copying your Github project I could see you have this Bean, which is throwing the exception:

@Bean(name = "ses")
public LocalSessionFactoryBean getSessionFactory() {
    LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();

    Properties props = new Properties();
    props.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
    props.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
    props.put("hibernate.dialect", env.getProperty("hibernate.dialect"));

    return factoryBean;

The problem here is that when you call factoryBean.setAnnotatedClasses twice, you are overwriting the first class you setted with the second. So when you are mapping User, you are no longer mapping Car. You need to pass every class you’ll use like this:

factoryBean.setAnnotatedClasses(Car.class, User.class);

Leave a Reply

Your email address will not be published. Required fields are marked *