Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Issue with Hibernate

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using the following instead when mapping the types which would otherwise not have a primary key.
 
Bartender
Posts: 21756
148
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not, that's not an error message from Tomcat. It may be in the Tomcat log, but the error message is from Hibernate.

We have a Hibernate forum, and that's a better place to ask Hibernate questions, since it's where the Hibernate experts like to hang out.

But actually, despite the somewhat misleading error message, I think the actual problem is that you probably haven't defined your join table as a Hibernate (mapped) Entity.
 
Bartender
Posts: 1251
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Bartender
Posts: 21756
148
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Claude Moore
Bartender
Posts: 1251
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ...
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!