0

The error: hibernate.AnnotationException: mappedBy reference an unknown target entity property

I know why the error is here but the longer i stare at it the more I cannot find it :). I just need another persons perspective.

Here are the MySQL tables: enter image description here

Then I have entities for User, Company and Transaction.

The relationships are shown here:

Transaction:

@ManyToOne
    @JoinColumn(name = "userId")
    User user;

    @ManyToOne
    @JoinColumn(name = "companyId")
    Company company;

Company

@JsonIgnore
    @OneToMany(mappedBy = "transaction")
    List<Transaction> transactions;

User

@JsonIgnore
    @OneToMany(mappedBy = "transaction")
    List<Transaction> transactions;

Here is the full error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: top100.models.Transaction.transaction in top100.models.User.transactions

So the error is to do with the relationship but I cannot spot my mistake.

Thanks :)

jackabe
  • 345
  • 9
  • 23

3 Answers3

1

Maybe you try mapped by field "user". Probably you may see asked this question in there Understanding mappedBy annotation in Hibernate

Aleksandr Zorin
  • 47
  • 2
  • 11
1

Company

@JsonIgnore
@OneToMany(mappedBy = "company")
List<Transaction> transactions;

User

@JsonIgnore
@OneToMany(mappedBy = "user")
List<Transaction> transactions;

The mappedBy is referring to the name of the field that is connected to. The link that @Aleksandr Zorin provided has more details. I recommand you to take a look :)

GabLeg
  • 352
  • 4
  • 14
1

It should be

Company

@JsonIgnore
@OneToMany(mappedBy = "company")
List<Transaction> transactions;

and

User

@JsonIgnore
@OneToMany(mappedBy = "user")
List<Transaction> transactions;

In mappedBy property you are specifying field in referenced table by which it should be mapped.

Glaud
  • 723
  • 5
  • 9