简体   繁体   English

Java Spring-Hibernate映射问题?

[英]Java Spring-Hibernate mapping question?

Say I have domain objects corresponding to Posts and Users . 说我有对应于PostsUsers域对象。 Nevertheless, I have corresponding database tables containing information relevant to "posts" and "users". 不过,我有相应的数据库表,其中包含与“帖子”和“用户”有关的信息。

Currently I have properly set up the mapping in Hibernate so that I can pull the info from the "posts" table and save it as a Post object. 目前,我已经在Hibernate中正确设置了映射,以便可以从“ posts”表中提取信息并将其另存为Post对象。 However, I then added to my Posts domain object, a reference to a User object (so that each post can have a corresponding User object). 但是,然后,我在Posts域对象中添加了对User对象的引用(以便每个帖子可以具有一个对应的User对象)。

In my database structure, the "posts" table has a user_id column which is a foreign key into the "users" table. 在我的数据库结构中,“ posts”表具有一个user_id列,该列是“ users”表中的外键。

My question is: when querying the "posts" table in my DAO, do I have to join on the "users" table and then somehow cast the returned user data into a User object? 我的问题是:当查询我的DAO中的“ posts”表时,我是否必须加入“ users”表中,然后以某种方式将返回的用户数据转换为User对象? Or can I simply leave my query as it is (ie just querying the "posts" table), and somehow add a hibernate mapping so that the User attribute in the Posts table gets populated? 或者我可以直接保留查询(即仅查询“ posts”表),然后以某种方式添加休眠映射,以便填充Posts表中的User属性? (I guess I am wondering if Hibernate can automatically generate the join for me if I set up the mapping properly - examples would be great too!) (我想我想知道,如果我正确设置映射,Hibernate是否可以为我自动生成联接-示例也很棒!)

Thanks, and I hope I was clear enough. 谢谢,我希望我足够清楚。

Update : Just to clarify, here are some code snippets: 更新 :只是为了澄清,这是一些代码片段:

My Post Object: 我的帖子对象:

public class Posts {

  private String title;
  ...
  private User user;

  //getters and setters here
}

My Post table columns: 我的帖子表列:

post_id (primary key)
title
...
user_id (foreign key into User table)

My mapping (without taking into account the User attribute) currently looks like this: 我的映射(不考虑User属性)当前如下所示:

<class name="com...Post" table="post">
    <id name="pId" column="post_id" />
    <property name="title" column="title" type="java.lang.String" />
    ...
            <!-- Need to add mapping here to join with user table?? -->
</class>

So basically, my DAO currently fetches a Post object without the private User user attribute (since I just added this). 因此,基本上,我的DAO当前正在获取不具有private User user属性的Post对象(因为我刚刚添加了此属性)。 My question was how do I populate that attribute in the Post object ( so that the fetched Post object also contains a User object )? 我的问题是如何在Post对象中填充该属性( 以便获取的Post对象也包含User对象 )?

Sorry if the current posts already answered this...they were just slightly confusing to me.. 抱歉,如果当前的帖子已经回答了这个问题,那对我来说只是些困惑。

If I understand your question correctly, I believe you're looking for the many-to-one mapping (many Posts to one User). 如果我正确理解了您的问题,我相信您正在寻找多对一的映射(许多发给一个用户的帖子)。 Add the following to your mapping for the Post object: 将以下内容添加到Post对象的映射中:

<many-to-one name="user" class="User" column="user_id" lazy="false" />

Update: Well, you got a confusing answer first because you asked a confusing question... The answer to your renewed question is indeed to define a many to one mapping for your Post class (as others have already mentioned). 更新:嗯,首先您得到一个令人困惑的答案,因为您提出了一个令人困惑的问题……对您的新问题的答案确实是为Post类定义了多对一映射(就像其他人已经提到的那样)。 Now, if you want to fetch the whole stuff with a single join query, you write: 现在,如果您想通过单个联接查询获取全部内容,请编写:

<many-to-one name="user" class="User" column="user_id" fetch="join" />

Original post: 原始帖子:

By default, Hibernate fetches lazily. 默认情况下,Hibernate延迟获取。 In fact, you need to touch the lazy property only if you want eager fetching. 实际上,仅当您希望获取时才需要触摸lazy属性。 Rough example for the behaviour of the default lazy fetch plan: 默认懒惰获取计划的行为的粗略示例:

Post post = (Post) session.load(Post.class, new Long(123));
// at this point, post refers to a proxy object created by Hibernate
// in the background - no post or user data has been loaded from DB
post.getId();
// post still refers to the proxy object
User user = post.getUser();
// post is now loaded, but user not - it refers to a proxy object
String name = user.getName(); // Now the user data is loaded from DB

So if you are happy with multiple queries, you don't need to do anything special. 因此,如果您对多个查询感到满意,则无需执行任何特殊操作。 OTOH if you want to fetch all post and user data in a join query, you need to set the attribute fetch="join" in your mapping for the user` property. OTOH如果要fetch="join" in your mapping for the联接查询中获取所有帖子和用户数据,则需要fetch="join" in your mapping for the user`属性设置属性fetch="join" in your mapping for the

You can do that by using the lazy property which is not activated by default. 您可以使用默认情况下未激活的lazy属性来执行此操作。 See some examples here http://www.javalobby.org/java/forums/t20533.html 在此处查看一些示例http://www.javalobby.org/java/forums/t20533.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM