-->
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.  [ 11 posts ] 
Author Message
 Post subject: Problems with a ManyToMany relation with a java.util.Set
PostPosted: Tue Jan 30, 2007 6:09 pm 
Newbie

Joined: Tue Jan 30, 2007 5:58 pm
Posts: 14
I am a newbie with Hibernate and JPA.

I have two simples classes: Film and Actor.

I want to store the relation ManyToMany between in the two classes: "Film and Actor" and "Actor to Film".

I have the following exception:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.MappingException: Could not determine type for: java.util.Set, for columns: [org.hibernate.mapping.Column(actors)]

What is the problem ?
Thanks for your help.


import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class Film implements Serializable{
@Id
@Column(name = "FILM_ID")
@GeneratedValue(strategy = GenerationType.TABLE)
private long id;

@Column(name = "title")
private String title;

@Column(name = "nationality")
private String nationality;

@Column(name = "year")
private int year;

private Set<Actor> actors = new HashSet<Actor>();

@ManyToMany(targetEntity = Actor.class, cascade = { CascadeType.PERSIST,
CascadeType.MERGE })
@JoinTable(name = "FILM_ACTOR", joinColumns = { @JoinColumn(name = "FILM_ID") }, inverseJoinColumns = { @JoinColumn(name = "ACTOR_ID") })
public Set<Actor> getActors() {
return actors;
}

public void setActors(Set<Actor> actors) {
this.actors = actors;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getNationality() {
return nationality;
}

public void setNationality(String nationality) {
this.nationality = nationality;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}



import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Actor implements Serializable {
@Id
@Column(name = "ACTOR_ID")
@GeneratedValue(strategy = GenerationType.TABLE)
private long id;

private String name;

private String firstname;

private Set<Film> films = new HashSet<Film>();

@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "actors", targetEntity = Film.class)
public Set<Film> getFilms() {
return films;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public void setFilms(Set<Film> films) {
this.films = films;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 6:22 pm 
Newbie

Joined: Tue Jan 30, 2007 5:26 pm
Posts: 7
Try moving the annotations on your id property declarations to the id accessor methods [getId()].


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 6:29 pm 
Newbie

Joined: Tue Jan 30, 2007 5:58 pm
Posts: 14
I have moved the annotations to getId().
@Id
@Column(name = "FILM_ID")
@GeneratedValue(strategy = GenerationType.TABLE)
public long getId() {
return id;
}
and
@Id
@Column(name = "ACTOR_ID")
@GeneratedValue(strategy = GenerationType.TABLE)
public long getId() {
return id;
}


I have another message:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: Film.actors[Actor]
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:252)

Any advices to solve this problem ?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 6:53 pm 
Newbie

Joined: Tue Jan 30, 2007 5:26 pm
Posts: 7
Moving those annotations certainly shouldn't have caused that. Did you accidently remove the @Entity annotation or something? Maybe you could repaste your classes.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 7:01 pm 
Newbie

Joined: Tue Jan 30, 2007 5:58 pm
Posts: 14
These are my classes:

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Actor implements Serializable {

private long id;

private String name;

private String firstname;

private Set<Film> films = new HashSet<Film>();

@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "actors", targetEntity = Film.class)
public Set<Film> getFilms() {
return films;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Id
@Column(name = "ACTOR_ID")
@GeneratedValue(strategy = GenerationType.TABLE)
public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public void setFilms(Set<Film> films) {
this.films = films;
}
}

AND

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class Film implements Serializable{

private long id;

@Column(name = "title")
private String title;

@Column(name = "nationality")
private String nationality;

@Column(name = "year")
private int year;

private Set<Actor> actors = new HashSet<Actor>();

@ManyToMany(targetEntity = Actor.class, cascade = { CascadeType.PERSIST,
CascadeType.MERGE })
@JoinTable(name = "FILM_ACTOR", joinColumns = { @JoinColumn(name = "FILM_ID") }, inverseJoinColumns = { @JoinColumn(name = "ACTOR_ID") })
public Set<Actor> getActors() {
return actors;
}

public void setActors(Set<Actor> actors) {
this.actors = actors;
}

@Id
@Column(name = "FILM_ID")
@GeneratedValue(strategy = GenerationType.TABLE)
public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getNationality() {
return nationality;
}

public void setNationality(String nationality) {
this.nationality = nationality;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

}

I have read the documentation on this page:
http://www.hibernate.org/hib_docs/annot ... ml_single/

And I have this problem:
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: Film.actors[Actor]
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:252)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 7:13 pm 
Newbie

Joined: Tue Jan 30, 2007 5:26 pm
Posts: 7
I copied your code (adding only a package) into a test project and it works fine for me. I've no idea what's wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 7:22 pm 
Newbie

Joined: Tue Jan 30, 2007 5:58 pm
Posts: 14
Please, can you paste me your client ?

maybe I have a bad client code. This is my client code:

Map myProperties = new HashMap();
myProperties.put("hibernate.hbm2ddl.auto", "create");

// Start EntityManagerFactory
EntityManagerFactory emf = Persistence.createEntityManagerFactory(
"helloworld", myProperties);

EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();

Film film = new Film();
film.setYear(1994);
film.setNationality("usa");
film.setTitle("Pulp Fiction");
film.setId(1);

Actor a1, a2;
a1 = new Actor();
a1.setId(1);
a1.setFirstname("john");
a1.setName("travolta");

a2 = new Actor();
a2.setId(2);
a2.setFirstname("bruce");
a2.setName("willis");
Set<Actor> actors = new HashSet<Actor>();
actors.add(a1);
actors.add(a2);

film.setActors(actors);
d.setFilm(film);

em.persist(film);
tx.commit();
em.close();
// Shutting down the application
emf.close();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 7:31 pm 
Newbie

Joined: Tue Jan 30, 2007 5:26 pm
Posts: 7
I don't get the error that you were, but I do get an error. I get:

javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.myapp.struts.Film


That's from setting the id yourself, which you should be letting Hibernate handle. This works for me:

Code:
...

tx.begin();

Film film = new Film();
film.setYear(1994);
film.setNationality("usa");
film.setTitle("Pulp Fiction");

Actor a1, a2;
a1 = new Actor();
a1.setFirstname("john");
a1.setName("travolta");

a2 = new Actor();
a2.setFirstname("bruce");
a2.setName("willis");
Set<Actor> actors = new HashSet<Actor>();
actors.add(a1);
actors.add(a2);

film.setActors(actors);


em.persist(film);
tx.commit();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 7:41 pm 
Newbie

Joined: Tue Jan 30, 2007 5:58 pm
Posts: 14
ok. thanks for your help.

Your client code doesn't work on my computer. I have the same exception (Use of @OneToMany or @ManyToMany targeting an unmapped class...)

Maybe it's a problem of Hibernate's version ?
Which version of Hibernate do you have ?

I have downloaded and used the latest version of Hibernate:

hibernate-3.2.2.ga
hibernate-annotations-3.2.1.GA
hibernate-entitymanager-3.2.1.GA


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 7:48 pm 
Newbie

Joined: Tue Jan 30, 2007 5:26 pm
Posts: 7
Oh. Are all those classes in the default package? If they are, I don't think Hibernate would be able to access them. Try adding them to a package to see if that fixes things. That appears to be the only difference between my code an yours.

Also, I'm using the same versions you are.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 8:03 pm 
Newbie

Joined: Tue Jan 30, 2007 5:58 pm
Posts: 14
the code works fine !!!

Thank you for your useful help...

I found my mistake, an xml element was missing in the persistence.xml file. (only one of the two classes was present. I have added the second class element and it works) .

<persistence-unit name="helloworld">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>beans.Actor</class>
<class>beans.Film</class>
<properties>
...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.