One question about association's column attribute

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

One question about association's column attribute

陈抒
Hi,All:
    The following words are from MyBatis manual,in page 34.

<association property="author" column="blog_author_id" javaType=" Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
</association>
The association element deals with a “has-one” type relationship. For example, in our example, a Blog
has one Author. An association mapping works mostly like any other result. You specify the target
property, the column to retrieve the value from, the javaType of the property (which MyBatis can figure
out most of the time), the jdbcType if necessary and a typeHandler if you want to override the retrieval
of the result values.

   I don't know which Java property has the value of column 'blog_ahthor_id' from database. It seems MyBatis creates a Blog object,also creates an Author object,then call this statement 
   Blog.setAuthor(author)
because it defines a map between property and javaType:  <association property="author" column="blog_author_id" javaType=" Author">
   What's the use of association's column attribute?


陈抒
Best regards
http://blog.csdn.net/sheismylife
Reply | Threaded
Open this post in threaded view
|

Re: One question about association's column attribute

Jackie Li
sample example might help you understand,

public class Blog{
String blogId;
Author author;
}

public class Author{
String author_id;
String author_username;
}

<select id="selectBlog" parameterType="int" resultMap="blogResult">
select blog.id as blogId, blog.author_id as blog_author_id, author.id
as author_id,
author.username as author_username
from blog, author where blog.author_id = author.id and blog.id = #{id}
</select>

"has-one" refers to the "has-a" relationship in java; and
association's column attribute for linking the objects, as foreign key
in database.

Cheers,
Jackie
On Nov 27, 5:28 am, 陈抒 <[hidden email]> wrote:

> Hi,All:
>     The following words are from MyBatis manual,in page 34.
>
> <association property="author" column="blog_author_id" javaType=" Author">
> <id property="id" column="author_id"/>
> <result property="username" column="author_username"/>
> </association>
> The association element deals with a “has-one” type relationship. For
> example, in our example, a Blog
> has one Author. An association mapping works mostly like any other result.
> You specify the target
> property, the column to retrieve the value from, the javaType of the
> property (which MyBatis can figure
> out most of the time), the jdbcType if necessary and a typeHandler if you
> want to override the retrieval
> of the result values.
>
>    I don't know which Java property has the value of column 'blog_ahthor_id'
> from database. It seems MyBatis creates a Blog object,also creates an Author
> object,then call this statement
>    Blog.setAuthor(author)
> because it defines a map between property and javaType:  <association
> property="author" column="blog_author_id" javaType=" Author">
>    What's the use of association's column attribute?
>
> 陈抒
> Best regardshttp://blog.csdn.net/sheismylife
Reply | Threaded
Open this post in threaded view
|

Re: One question about association's column attribute

陈抒
Hi,
   In database,the relationship between blog and author tables is 'has-one'.The blog.author_id is a foreign key which references the author.id column.
   In Java,Blog object 'has a' Author object.All these are very clear.
   But if I forget to write  column="blog_author_id",what will happen?

陈抒
Best regards
http://blog.csdn.net/sheismylife


On Mon, Nov 29, 2010 at 11:21 AM, lwpro2 <[hidden email]> wrote:
sample example might help you understand,

public class Blog{
String blogId;
Author author;
}

public class Author{
String author_id;
String author_username;
}

<select id="selectBlog" parameterType="int" resultMap="blogResult">
select blog.id as blogId, blog.author_id as blog_author_id, author.id
as author_id,
author.username as author_username
from blog, author where blog.author_id = author.id and blog.id = #{id}
</select>

"has-one" refers to the "has-a" relationship in java; and
association's column attribute for linking the objects, as foreign key
in database.

Cheers,
Jackie
On Nov 27, 5:28 am, 陈抒 <[hidden email]> wrote:
> Hi,All:
>     The following words are from MyBatis manual,in page 34.
>
> <association property="author" column="blog_author_id" javaType=" Author">
> <id property="id" column="author_id"/>
> <result property="username" column="author_username"/>
> </association>
> The association element deals with a “has-one” type relationship. For
> example, in our example, a Blog
> has one Author. An association mapping works mostly like any other result.
> You specify the target
> property, the column to retrieve the value from, the javaType of the
> property (which MyBatis can figure
> out most of the time), the jdbcType if necessary and a typeHandler if you
> want to override the retrieval
> of the result values.
>
>    I don't know which Java property has the value of column 'blog_ahthor_id'
> from database. It seems MyBatis creates a Blog object,also creates an Author
> object,then call this statement
>    Blog.setAuthor(author)
> because it defines a map between property and javaType:  <association
> property="author" column="blog_author_id" javaType=" Author">
>    What's the use of association's column attribute?
>
> 陈抒
> Best regardshttp://blog.csdn.net/sheismylife

Reply | Threaded
Open this post in threaded view
|

Re: One question about association's column attribute

zjliu
This post has NOT been accepted by the mailing list yet.
In reply to this post by 陈抒
I think this attribute is used only when the association has a select attribute, such as  
<association property="author" column="blog_author_id" javaType="Author" select="selectAuthor"/>
when  you have a nested select, you may need to pass some parameters to it so you can get the nested query result.