These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: [SOLVED]Use of @OneToMany or @ManyToMany
PostPosted: Tue May 03, 2011 12:36 am 
Newbie

Joined: Tue May 03, 2011 12:28 am
Posts: 2
Hi, I'm new to Hibernate, recently I've been learning Hibernate with Struts and Spring,
when I tried to write my first app, I encountered the problem:
Quote:
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: data.Movie.actors[data.Actor]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1185)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:710)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:645)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:65)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1689)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1396)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1348)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:717)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1479)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)


I googled a lot and found nothing to help, my code is like:
Code:
package data;

import java.util.Set;
import javax.persistence.*;

@Entity
@Table(name="actors")
public class Actor {
   @Id @GeneratedValue
   
   Integer id;
   public Integer getId() {
      return id;
   }
   
   public void setId(Integer id) {
      this.id = id;
   }
   
   String first_name;
   public String getFirstName() {
      return first_name;
   }
   
   public void setFirstName(String firstName) {
      this.first_name = firstName;
   }
   
   String last_name;
   public String getLastName() {
      return last_name;
   }
   
   public void setLastName(String lastName) {
      this.last_name = lastName;
   }
   
   String middle_name;
   public String getMiddleName() {
      return middle_name;
   }
   
   public void setMiddleName(String middleName) {
      this.middle_name = middleName;
   }
   
   @ManyToMany(targetEntity = Movie.class)
   @JoinTable(
      name="movies_actors",
      joinColumns=@JoinColumn(name="id"),
      inverseJoinColumns=@JoinColumn(name="movie_id")
   )
   
   Set<Movie> movies;
   public Set<Movie> getMovies() {
      return movies;
   }
   
}

and the other model object:
Code:
package data;

import java.util.Set;
import javax.persistence.*;

@Entity
@Table(name="movies")
public class Movie {
   @Id @GeneratedValue
   
   Integer id;
   public Integer getId() {
      return id;
   }
   
   public void setId(Integer id) {
      this.id = id;
   }
   
   String title;
   public String getTitle() {
      return title;
   }
   
   public void setTitle(String title) {
      this.title = title;
   }
   
   @ManyToMany(targetEntity = Actor.class)
   @JoinTable(
      name="movies_actors",
      joinColumns=@JoinColumn(name="id"),
      inverseJoinColumns=@JoinColumn(name="actor_id")
   )
   Set<Actor> actors;
   public Set<Actor> getActors() {
      return actors;
   }
}

also with my hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <!-- Database connection settings -->
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="connection.url">jdbc:mysql://localhost/cma</property>
      <property name="connection.username">xxx</property>
      <property name="connection.password">xxx</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      
      <!--
         Enable c3p0 connection pooling, cuz hibernate pooling is prod-ready.
         Apparently connection.provider_class is needed in hibernate 3+
       -->
       <property name="c3p0.max_size">100</property>
       <property name="c3p0.min_size">1</property>
       <property name="c3p0.idle_test_period">30</property>
      
       <!-- Echo all executed SQL to stdout for debugging -->
       <property name="show_sql">true</property>
      
       <!-- All the entity classes for hibernate to check for annotations here -->
       <mapping class="data.Actor" />
       <mapping class="data.Movie" />
   </session-factory>
</hibernate-configuration>


There is a guy in stackoverflow said he solved the problem because the relation he used is wrong, he changed the manytomany to manytoone, which solved his problem. but in my app, I can't see that helps.

I can't see anything wrong, so would anyone help me with it?
the weirdest thing is I commented the
Code:
@ManyToMany(targetEntity = Actor.class)
block and the content under it, the exception was thrown as well.

Thanks a lot.


Last edited by hjbolide on Tue May 03, 2011 5:10 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Use of @OneToMany or @ManyToMany targeting an unmapped class
PostPosted: Tue May 03, 2011 5:10 am 
Newbie

Joined: Tue May 03, 2011 12:28 am
Posts: 2
Well, I solved the problem, I went through the log output in the console window, and found that the mapping relation wasn't updated even I republished the app on my server, that's one thing I need to figure out, :p

I re-created a brand new project and then copied all the files into it, things worked just fine.

The length of the subject is limited, so I need to cut it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.