English 中文(简体)
Mapping Relationships with Hibernate
原标题:

I m still learning how to set up these mappings using the JPA annotations so this may be a beginner mistake.

I have a User class and a Course class. In the database each Course has a User foreign key in each row (think of the User as the teacher teaching the Course). So each User can have multiple Courses, but each Course has only one User.

In mapping these classes I have written the following code below:

@Entity
public class User extends BaseModel {

 @OneToMany(mappedBy="course")
 private Collection<Course> courses;

 public void setCourses(Collection<Course> courses) { this.courses = courses; }

 public Collection<Course> getCourses() { return courses; }

}

@Entity
public class Course extends BaseModel {

 @ManyToOne
 @JoinColumn(name="user_id")
 private User user;

 public void setUser(User user) { this.user = user; }

 public User getUser() { return user; }

}

I have omitted the irrelevant fields and the BaseModel just accounts for the primary key in each table. Is there anything that is blatantly wrong here? If not what would be a good way to go about debugging a problem like this.

I am receiving a NullPointerException when I try to call a select on the user table. However, when I take out the references to the Course class in the User class everything works perfectly fine.

The line in question is in my DAO class. Perhaps the Entity Manager is failing to initialize. Here s the code.

Query query = Stripersist.getEntityManager().createQuery(getQuery(fieldName, null)).setParameter(fieldName, value);

createQuery() just returns:

String query = "FROM " + entityClass.getName() + " t WHERE t." + fieldName + " = :" + fieldName;

I am using the Stripes framework, Hibernate, and Stripersist if that provides any further clues as to what is wrong.

最佳回答

From the very first glance, it seems to be that you should write:

@OneToMany(mappedBy="user") 
private Collection<Course> courses; 

I. e. in @OneToMany mappedBy should point to the field which points to the current object.

问题回答

mappedBy indicates that the entity in this side is the inverse of the relationship, and the owner resides in the "other" entity. In your case, as axtavt answered should be:

@OneToMany(mappedBy="user")
private Collection<Course> courses;

states that user can opt for many courses and Course is carrying the ownership of this relation.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签