I'm getting an error message in Tomcat. It's saying a field isn't mapped to a table in one of my classes. Here's the exception message. I can provide the rest of the stacktrace later, if you want.
nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.revature.movie.model.MovieLikes.users[java.lang.Integer]
I'm trying to create a join table between users and movies to represent movies that people like. Hibernate seems to be telling me the List I've created in my MovieLikes class isn't mapped to a table. I only want the field included in a join table, though. Here's the code for MovieLikes.java:
Tim is right, and honestly I presume you should recap some concepts about entities and their relationships. It looks like you're trying to map the relationship table as an entity , working with a list of integers - the IDs of the users.This approach isn't correct:you should define a relationship between 'movies' and 'users' and let hibernate create and maintain a relationship table for you. Also, whenever possible avoid @Manytomany relationships, and use JPA queries to load related entities explicitly whenever needed.
Claude Moore wrote:... and use JPA queries to load related entities explicitly whenever needed.
I wouldn't agree. Part of the benefit of an ORM is that it reduces the need for SQL (or JPQL) expertise. It's OK to relate stuff like that, Hibernate/JPA will make the queries for you. Just avoid eager fetches unless you really, really want to suck the entire database (or large parts of it) into memory.
But if you plan to operate on detached entities, be sure you pull them in before detaching. A single field reference to the related object will suffice with no explicit query required.
I've worked with many/many relationships before in JPA, and it's a challenge to set up, but it does work.
Got idle CPU cycles? Join the war on COVID-19 by donating them to find the coronavirus' weak spots. folding@home Runs in the background. https://foldingathome.org
Tim Holloway wrote: I've worked with many/many relationships before in JPA, and it's a challenge to set up, but it does work.
You're right, my fault. I remembered to have read in a book about JPA that many-to-many relationship are difficult to maintain, often unnecessary, and that DBAs aren't usually happy with them (I presume for performance related reasons).
But the workaround suggested was to rely on two different one-to-many relationships, not using JPQL.
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...