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.