'Many To One' attribute type should not be 'Persistence Entity'
Asked Answered
C

4

43

I'm trying out IntelliJ IDEA and it's warning me of a Hibernate association that I don't quite understand.

One Side:

@Entity
@Table(name = "MY_REQ_ASSIGNEE")
public class MyRequestAssignee extends BaseUser {
    //...
    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, mappedBy = "myRequestAssignee")
    private Collection<MyRequest> myRequests = new ArrayList<>();
    //...
}

Many Side:

@Entity
@Table(name = "MY_REQUEST")
public class MyRequest implements Persistable {

   //...
   @ManyToOne(fetch=FetchType.EAGER)
   @JoinColumn(name="ASSIGNEE_ID")
   private MyRequestAssignee myRequestAssignee;
   //...
}

(Persistable is just an interface that has the id property to make sure Hibernate has access to it.)

I see the MyRequestAssignee type underlined in red and the message reads 'Many To One' attribute type should not be 'Persistence Entity'. Are there something wrong with my relationships?

For a sanity check, I looked at this post and this post too.

Chromous answered 20/8, 2015 at 16:38 Comment(1)
I have a similar problem with my @ManyToMany relationship. The code works, thoughKreg
E
113

I found this to be caused by the child entity not being defined in hibernate.cfg.xml. The error message could be improved.

Elle answered 5/11, 2015 at 16:58 Comment(6)
Adding <mapping class="com.company.domain.MyRequestAssignee"/> to the hibernate.cfg.xml resolved this.Chromous
Awesome. I should have been more explicit about what to add to hibernate.cfg.xml.Elle
If you are using JPA, it might be an entry in the persistence.xml that's missing.House
@House Correct. Or it could be in your annotatedClasses property of your LocalSessionFactoryBean bean config for Spring.Chromous
Using JPA, it means that the entity MyRequestAssignee has to be added in persistence.xmlPuton
If you are using annotations instead of XML, you need to add a @ManyToOne annotation to your field. See this answer: #3927591Gettysburg
L
3

In my case, the error appears because I omitted to declare @Entity en the other side class

Locksmith answered 12/1 at 16:50 Comment(0)
A
1

I'm getting this issue in my multi-module Gradle project with Quarkus in IntelliJ, where I have a OneToOne reference from an entity of module A to an entity of module B.

enter image description here

The code works, so I guess it could be a false flag of IntelliJ

Aurthur answered 1/2, 2022 at 7:40 Comment(2)
Did you check if your AuthUser had the @Entity annotation?Accept
@V.Dimitrov yes, the issue was explicitly related to the multi-Module structureLethia
C
0

This question is a bit old, but I just wanted to add that this can also be caused by a conflicting hibernate .hbm mapping file and JPA annotations. I ran into this error message when converting old mapping files to annotations and forgot to comment out one of the old mapping files.

Cornaceous answered 29/1, 2018 at 14:47 Comment(2)
Another reason for this error can be if you forget to add the @Entity attribute to your target class. I ended up here when searching for this (even though that wasn't OP's problem)Doughboy
@Doughboy you are a saviourChemistry

© 2022 - 2024 — McMap. All rights reserved.