映射与 Hibernate 的关系

发布于 2024-08-16 21:06:29 字数 1299 浏览 7 评论 0原文

我仍在学习如何使用 JPA 注释设置这些映射,因此这可能是初学者的错误。

我有一个用户类和一个课程类。在数据库中,每个课程的每一行都有一个用户外键(将用户视为教授课程的老师)。所以每个User可以有多个Course,但是每个Course只有一个User。

在映射这些类时,我编写了以下代码:

@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; }

}

我省略了不相关的字段,并且 BaseModel 仅说明每个表中的主键。 这里有什么明显的错误吗?如果没有,那么调试此类问题的好方法是什么。

当我尝试在用户表上调用 select 时,收到 NullPointerException。但是,当我在 User 类中取出对 Course 类的引用时,一切都工作得很好。

有问题的行位于我的 DAO 类中。也许实体管理器无法初始化。这是代码。

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

createQuery() 只是返回:

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

我正在使用 Stripes 框架、Hibernate 和 Stripersist(如果这提供了有关问题所在的任何进一步线索)。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无风消散 2024-08-23 21:06:29

乍一看,似乎你应该写:

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

I. e. @OneToMany mappedBy 中应该指向指向当前对象的字段。

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.

旧时浪漫 2024-08-23 21:06:29

mappedBy 表示这一方的实体是关系的逆,所有者驻留在“另一方”实体中。在您的情况下,axtavt 的回答应该是:

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

指出用户可以选择许多课程,并且课程拥有此关系的所有权。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文