Hi everyone
i will make the question straight: how can i make a OneToMany bidirectional mapping without this excepcion:
Code:
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: org.cnio.appform.entity.User.interviews[org.cnio.appform.entity.Interview]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1033)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:576)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:517)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1136)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:316)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
... 3 more
and without using the @CollectionOfElements annotation, which is not a JPA standard?
any idea, suggestion... will be very welcome and i'd appreciate, as i didnt find any solution.
Thanks in advance. Following, the configuration and the code
Hibernate version: is 3.2.6 and hibernate annotations 3.3.0
Mapping documents: done via annotations
Code between sessionFactory.openSession() and session.close():Code:
User owner = (User)session.createCriteria(User.class).
add(Restrictions.eq("nickname", nickname)).uniqueResult();
Interview myInt = new Interview (intName, intDesc, owner);
session.save(myInt);
although this code never gets to run as the exception is thrown previously. The code of the problematic classes (User and Interview in my case, assuming one user has many interviews, user is the One side of the relationship):
Code:
@Entity
@Table (name="appuser")
@SequenceGenerator(name="UserSeq", sequenceName="appuser_iduser_seq")
// allocationSize=1, initialValue=1)
public class User {
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="UserSeq")
@Column (name="iduser")
private Integer id;
@Column (name="name")
private String name;
@Column (name="midname")
private String midname;
@Column (name="lastname")
private String lastname;
@Column (name="nickname")
private String nickname;
@Column (name="passwd")
private String passwd;
// this is to implement the 1-N relathioship between user and interview
@OneToMany (mappedBy="owner",targetEntity=Interview.class)
private Set<Interview> interviews;
// link to the join table to implement the M-N relationship user-role
@OneToMany (mappedBy="user")
private Set<UserRole> userRoles;
public User () {
name = "";
midname = "";
lastname = "";
nickname = "";
passwd = "";
userRoles = new HashSet<UserRole> ();
interviews = new HashSet<Interview> ();
}
// ... various getter an setter methods
}
Code:
@Entity
@Table (name="interview")
@SequenceGenerator(name="InterviewSeq",
sequenceName="interview_idinterview_seq")
// allocationSize=1, initialValue=1)
public class Interview {
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="InterviewSeq")
@Column (name="idinterview")
private Integer id;
@Column (name="name")
private String name;
@Column (name="description")
private String description;
@ManyToOne (targetEntity=User.class)
@JoinColumn(name="iduser")
private User owner;
public Interview () {
name = "";
description = "";
owner = new User ();
}
/**
* Build an interview with the owner, which is just an user
* @param anUser the owner of this interview
*/
public Interview (String name, String description, User anUser) {
// guarantee referential integrity
this.owner = anUser;
this.name = name;
this.description = description;
// important to make the relationship inverse
owner.getInterviews().add(this);
}
//... varios getter and setter methods again
}
Name and version of the database you are using: Postgresql 8.1