<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"> 
        <base href="https://hibernate.atlassian.net"> 
        <title>Message Title</title> 
    </head> 
    <body class="jira" style="color: #333333; font-family: Arial, sans-serif; font-size: 14px; line-height: 1.429"> 
        <table id="background-table" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #f5f5f5; border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt" bgcolor="#f5f5f5"> 
            <!-- header here --> 
            <tbody>
                <tr> 
                    <td id="header-pattern-container" style="padding: 0px; border-collapse: collapse; padding: 10px 20px"> 
                        <table id="header-pattern" cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt"> 
                            <tbody>
                                <tr> 
                                    <td id="header-avatar-image-container" valign="top" style="padding: 0px; border-collapse: collapse; vertical-align: top; width: 32px; padding-right: 8px" width="32"> <img id="header-avatar-image" class="image_fix" src="cid:jira-generated-image-avatar-c9743314-405a-4212-a89f-15d59741613d" height="32" width="32" border="0" style="border-radius: 3px; vertical-align: top"> </td> 
                                    <td id="header-text-container" valign="middle" style="padding: 0px; border-collapse: collapse; vertical-align: middle; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 1px"> <a class="user-hover" rel="rame" id="email_rame" href="https://hibernate.atlassian.net/secure/ViewProfile.jspa?name=rame" style="color:#6c797f;; color: #3b73af; text-decoration: none">Rame Shan</a> <strong>created</strong> an issue </td> 
                                </tr> 
                            </tbody>
                        </table> </td> 
                </tr> 
                <tr> 
                    <td id="email-content-container" style="padding: 0px; border-collapse: collapse; padding: 0 20px"> 
                        <table id="email-content-table" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-spacing: 0; border-collapse: separate"> 
                            <tbody>
                                <tr> 
                                    <!-- there needs to be content in the cell for it to render in some clients --> 
                                    <td class="email-content-rounded-top mobile-expand" style="padding: 0px; border-collapse: collapse; color: #ffffff; padding: 0 15px 0 16px; height: 15px; background-color: #ffffff; border-left: 1px solid #cccccc; border-top: 1px solid #cccccc; border-right: 1px solid #cccccc; border-bottom: 0; border-top-right-radius: 5px; border-top-left-radius: 5px; height: 10px; line-height: 10px; padding: 0 15px 0 16px; mso-line-height-rule: exactly" height="10" bgcolor="#ffffff">&nbsp;</td> 
                                </tr> 
                                <tr> 
                                    <td class="email-content-main mobile-expand " style="padding: 0px; border-collapse: collapse; border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #ffffff" bgcolor="#ffffff"> 
                                        <table class="page-title-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt"> 
                                            <tbody>
                                                <tr> 
                                                    <td class="page-title-pattern-first-line " style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; padding-top: 10px"> <a href="https://hibernate.atlassian.net/browse/HHH" style="color: #3b73af; text-decoration: none">Hibernate ORM</a> / <a href="https://hibernate.atlassian.net/browse/HHH-10590" style="color: #3b73af; text-decoration: none"><img src="cid:jira-generated-image-avatar-5abdbbef-c685-42f8-afd0-603b1d1cf2be" height="16" width="16" border="0" align="absmiddle" alt="Bug" style="vertical-align: text-bottom"></a> <a href="https://hibernate.atlassian.net/browse/HHH-10590" style="color: #3b73af; text-decoration: none">HHH-10590</a> </td> 
                                                </tr> 
                                                <tr> 
                                                    <td style="vertical-align: top;; padding: 0px; border-collapse: collapse; padding-right: 5px; font-size: 20px; line-height: 30px; mso-line-height-rule: exactly" class="page-title-pattern-header-container"> <span class="page-title-pattern-header" style="font-family: Arial, sans-serif; padding: 0; font-size: 20px; line-height: 30px; mso-text-raise: 2px; mso-line-height-rule: exactly; vertical-align: middle"> <a href="https://hibernate.atlassian.net/browse/HHH-10590" style="color: #3b73af; text-decoration: none">@MapKeyJoinColumn is creating un-necessary DB update</a> </span> </td> 
                                                </tr> 
                                            </tbody>
                                        </table> </td> 
                                </tr> 
                                <tr> 
                                    <td class="email-content-main mobile-expand  wrapper-special-margin" style="padding: 0px; border-collapse: collapse; border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #ffffff; padding-top: 10px; padding-bottom: 5px" bgcolor="#ffffff"> 
                                        <table class="keyvalue-table" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt"> 
                                            <tbody>
                                                <tr> 
                                                    <th style="color: #707070; font: normal 14px/20px Arial, sans-serif; text-align: left; vertical-align: top; padding: 2px 0">Issue Type:</th> 
                                                    <td class="has-icon" style="padding: 0px; border-collapse: collapse; font: normal 14px/20px Arial, sans-serif; padding: 2px 0 2px 5px; vertical-align: top"> <img src="cid:jira-generated-image-avatar-5abdbbef-c685-42f8-afd0-603b1d1cf2be" height="16" width="16" border="0" align="absmiddle" alt="Bug" style="vertical-align: text-bottom"> Bug </td> 
                                                </tr> 
                                                <tr> 
                                                    <th style="color: #707070; font: normal 14px/20px Arial, sans-serif; text-align: left; vertical-align: top; padding: 2px 0">Affects Versions:</th> 
                                                    <td style="padding: 0px; border-collapse: collapse; font: normal 14px/20px Arial, sans-serif; padding: 2px 0 2px 5px; vertical-align: top"> 4.3.11 </td> 
                                                </tr> 
                                                <tr> 
                                                    <th style="color: #707070; font: normal 14px/20px Arial, sans-serif; text-align: left; vertical-align: top; padding: 2px 0">Assignee:</th> 
                                                    <td style="padding: 0px; border-collapse: collapse; font: normal 14px/20px Arial, sans-serif; padding: 2px 0 2px 5px; vertical-align: top"> Unassigned </td> 
                                                </tr> 
                                                <tr> 
                                                    <th style="color: #707070; font: normal 14px/20px Arial, sans-serif; text-align: left; vertical-align: top; padding: 2px 0">Components:</th> 
                                                    <td style="padding: 0px; border-collapse: collapse; font: normal 14px/20px Arial, sans-serif; padding: 2px 0 2px 5px; vertical-align: top"> hibernate-core </td> 
                                                </tr> 
                                                <tr> 
                                                    <th style="color: #707070; font: normal 14px/20px Arial, sans-serif; text-align: left; vertical-align: top; padding: 2px 0">Created:</th> 
                                                    <td style="padding: 0px; border-collapse: collapse; font: normal 14px/20px Arial, sans-serif; padding: 2px 0 2px 5px; vertical-align: top"> 04/Mar/2016 12:04 PM </td> 
                                                </tr> 
                                                <tr> 
                                                    <th style="color: #707070; font: normal 14px/20px Arial, sans-serif; text-align: left; vertical-align: top; padding: 2px 0">Environment:</th> 
                                                    <td style="padding: 0px; border-collapse: collapse; font: normal 14px/20px Arial, sans-serif; padding: 2px 0 2px 5px; vertical-align: top"> Hibernate 4.3.11, Oracle 11g </td> 
                                                </tr> 
                                                <tr> 
                                                    <th style="color: #707070; font: normal 14px/20px Arial, sans-serif; text-align: left; vertical-align: top; padding: 2px 0">Labels:</th> 
                                                    <td style="padding: 0px; border-collapse: collapse; font: normal 14px/20px Arial, sans-serif; padding: 2px 0 2px 5px; vertical-align: top"> hibernate jpa21 java-8 </td> 
                                                </tr> 
                                                <tr> 
                                                    <th style="color: #707070; font: normal 14px/20px Arial, sans-serif; text-align: left; vertical-align: top; padding: 2px 0">Priority:</th> 
                                                    <td class="has-icon" style="padding: 0px; border-collapse: collapse; font: normal 14px/20px Arial, sans-serif; padding: 2px 0 2px 5px; vertical-align: top"> <img src="cid:jira-generated-image-static-major-ac8dafb9-3aef-407d-8f0e-0bbd7f4d43d5" height="16" width="16" border="0" align="absmiddle" alt="Major" style="vertical-align: text-bottom"> Major </td> 
                                                </tr> 
                                                <tr> 
                                                    <th style="color: #707070; font: normal 14px/20px Arial, sans-serif; text-align: left; vertical-align: top; padding: 2px 0">Reporter:</th> 
                                                    <td style="padding: 0px; border-collapse: collapse; font: normal 14px/20px Arial, sans-serif; padding: 2px 0 2px 5px; vertical-align: top"> <a class="user-hover" rel="rame" id="email_rame" href="https://hibernate.atlassian.net/secure/ViewProfile.jspa?name=rame" style="color:#6c797f;; color: #3b73af; text-decoration: none">Rame Shan</a> </td> 
                                                </tr> 
                                            </tbody>
                                        </table> </td> 
                                </tr> 
                                <tr> 
                                    <td class="email-content-main mobile-expand  issue-description-container" style="padding: 0px; border-collapse: collapse; border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #ffffff; padding-top: 5px; padding-bottom: 10px" bgcolor="#ffffff"> 
                                        <table class="text-paragraph-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 2px"> 
                                            <tbody>
                                                <tr> 
                                                    <td class="text-paragraph-pattern-container mobile-resize-text " style="padding: 0px; border-collapse: collapse; padding: 0 0 10px 0"> <p style="margin-top:0;margin-bottom:10px;; margin: 10px 0 0 0; margin-top: 0">We have the below entity mapping in our project. When we add a new phone to an existing employee then we see hibernate is generating un-necessary DB update - "update PHONE set PHONE_TYPE_ID=? where PHONE_TYPE_ID=? and EMPLOYEE_ID=?". It updates PHONE_TYPE_ID to itself. This update seems like harmless but it adds additional updates to DB. We are using hibernate 4.3.11 and Oracle DB.</p> <p style="margin-top:0;margin-bottom:10px;; margin: 10px 0 0 0">Would like to see if there is any way we can avoid this un-necessary DB update. I think changing the association from Map to some other collection may resolve the issue but we would like to use Map.</p> <p style="margin-top:0;margin-bottom:10px;; margin: 10px 0 0 0">Any config, mapping, annotation change would be helpful.</p> <p style="margin-top:0;margin-bottom:10px;; margin: 10px 0 0 0">-------------------</p> 
                                                        <div class="code panel" style="border-width: 1px;; border: 1px solid #cccccc; background: #f5f5f5; font-size: 12px; line-height: 1.333; font-family: monospace; border: 1px solid #cccccc; -moz-border-radius: 3px 3px 3px 3px; border-radius: 3px 3px 3px 3px; margin: 9px 0">
                                                            <div class="codeContent panelContent" style="padding: 9px 12px"> 
                                                                <pre class="code-java" style="margin: 10px 0 0 0; margin-top: 0; max-height: 30em; overflow: auto; white-space: pre-wrap; word-wrap: normal">
@Entity
@Table(name = <span class="code-quote" style="color: #009100">"EMPLOYEE"</span>)
<span class="code-keyword" style="color: #000091">public</span> class Employee <span class="code-keyword" style="color: #000091">implements</span> Serializable
{
      @Id
      @Column(name = <span class="code-quote" style="color: #009100">"EMPLOYEE_ID"</span>)
      <span class="code-keyword" style="color: #000091">private</span> <span class="code-object" style="color: #910091">Integer</span> id;

      @OneToMany(mappedBy=<span class="code-quote" style="color: #009100">"owner"</span>, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      @MapKeyJoinColumn(name = <span class="code-quote" style="color: #009100">"PHONE_TYPE_ID"</span>)
      <span class="code-keyword" style="color: #000091">private</span> Map&lt;PhoneType, Phone&gt; phones = <span class="code-keyword" style="color: #000091">new</span> HashMap&lt;PhoneType, Phone&gt;();

      @NotNull
      @Column(name = <span class="code-quote" style="color: #009100">"FIRST_NAME"</span>)
      <span class="code-keyword" style="color: #000091">private</span> <span class="code-object" style="color: #910091">String</span> firstName;

      @NotNull
      @Column(name = <span class="code-quote" style="color: #009100">"LAST_NAME"</span>)
      <span class="code-keyword" style="color: #000091">private</span> <span class="code-object" style="color: #910091">String</span> lastName;

      <span class="code-keyword" style="color: #000091">protected</span> Employee()
      {
          <span class="code-comment" style="color: #808080">//<span class="code-keyword" style="color: #000091; color: #808080">for</span> ORM
</span>      }

      <span class="code-keyword" style="color: #000091">public</span> Employee(<span class="code-object" style="color: #910091">Integer</span> id, <span class="code-object" style="color: #910091">String</span> firstName, <span class="code-object" style="color: #910091">String</span> lastName)
      {
          <span class="code-keyword" style="color: #000091">this</span>.id = id;
          <span class="code-keyword" style="color: #000091">this</span>.firstName = firstName;
          <span class="code-keyword" style="color: #000091">this</span>.lastName = lastName;
      }

    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">long</span> getId() 
    {
        <span class="code-keyword" style="color: #000091">return</span> id;
    }

    <span class="code-keyword" style="color: #000091">public</span> Map&lt;PhoneType, Phone&gt; getPhones() 
    {
        <span class="code-keyword" style="color: #000091">return</span> phones;
    }

    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">String</span> getFirstName() 
    {
        <span class="code-keyword" style="color: #000091">return</span> firstName;
    }

    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">String</span> getLastName() 
    {
        <span class="code-keyword" style="color: #000091">return</span> lastName;
    }

    <span class="code-keyword" style="color: #000091">public</span> void addPhones(Phone phone) 
    {
        phones.put(phone.getPhoneType(), phone);
    }

    @Override
    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">boolean</span> equals(<span class="code-object" style="color: #910091">Object</span> o)
    {
        <span class="code-keyword" style="color: #000091">if</span> (<span class="code-keyword" style="color: #000091">this</span> == o)
        {
            <span class="code-keyword" style="color: #000091">return</span> <span class="code-keyword" style="color: #000091">true</span>;
        }

        <span class="code-keyword" style="color: #000091">if</span> (o == <span class="code-keyword" style="color: #000091">null</span> || getClass() != o.getClass())
        {
            <span class="code-keyword" style="color: #000091">return</span> <span class="code-keyword" style="color: #000091">false</span>;
        }

        Employee other = (Employee) o;
        <span class="code-keyword" style="color: #000091">return</span> <span class="code-keyword" style="color: #000091">new</span> EqualsBuilder()
            .append(id, other.id)
            .append(firstName, other.firstName)
            .append(lastName, other.lastName)
            .isEquals();
    }

    @Override
    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">int</span> hashCode()
    {
        <span class="code-keyword" style="color: #000091">return</span> <span class="code-keyword" style="color: #000091">new</span> HashCodeBuilder()
            .append(id)
            .append(firstName)
            .append(lastName)
            .toHashCode();
    }

    @Override
    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">String</span> toString()
    {
        <span class="code-keyword" style="color: #000091">return</span> <span class="code-object" style="color: #910091">String</span>.format(<span class="code-quote" style="color: #009100">"id[%s], firstName[%s], lastName[%s]"</span>,  id, firstName, lastName);
    }


}

@Entity
@Table(name = <span class="code-quote" style="color: #009100">"PHONE"</span>)
<span class="code-keyword" style="color: #000091">public</span> class Phone <span class="code-keyword" style="color: #000091">implements</span> Serializable 
{
      @Id
      @ManyToOne
      @JoinColumn(name = <span class="code-quote" style="color: #009100">"EMPLOYEE_ID"</span>, referencedColumnName = <span class="code-quote" style="color: #009100">"EMPLOYEE_ID"</span>)
      @NotNull
      <span class="code-keyword" style="color: #000091">private</span> Employee owner;

      @Id
      @ManyToOne
      @JoinColumn(name = <span class="code-quote" style="color: #009100">"PHONE_TYPE_ID"</span>, nullable = <span class="code-keyword" style="color: #000091">false</span>, updatable=<span class="code-keyword" style="color: #000091">false</span>, insertable=<span class="code-keyword" style="color: #000091">false</span>)
      @NotNull
      @Fetch(FetchMode.SELECT)
      <span class="code-keyword" style="color: #000091">private</span> PhoneType phoneType;

      @NotNull
      @Column(name = <span class="code-quote" style="color: #009100">"PHONE_NUMBER"</span>)
      <span class="code-keyword" style="color: #000091">private</span> <span class="code-object" style="color: #910091">String</span> phoneNumber;

      <span class="code-keyword" style="color: #000091">protected</span> Phone()
      {
          <span class="code-comment" style="color: #808080">//<span class="code-keyword" style="color: #000091; color: #808080">for</span> ORM
</span>      }

      <span class="code-keyword" style="color: #000091">public</span> Phone(Employee owner, PhoneType phoneType, <span class="code-object" style="color: #910091">String</span> phoneNumber)
      {
          <span class="code-keyword" style="color: #000091">this</span>.owner = owner;
          <span class="code-keyword" style="color: #000091">this</span>.phoneType = phoneType;
          <span class="code-keyword" style="color: #000091">this</span>.phoneNumber = phoneNumber;
      }

    <span class="code-keyword" style="color: #000091">public</span> Employee getOwner() 
    {
        <span class="code-keyword" style="color: #000091">return</span> owner;
    }

    <span class="code-keyword" style="color: #000091">public</span> PhoneType getPhoneType() 
    {
        <span class="code-keyword" style="color: #000091">return</span> phoneType;
    }

    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">String</span> getPhoneNumber() 
    {
        <span class="code-keyword" style="color: #000091">return</span> phoneNumber;
    }

    @Override
    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">boolean</span> equals(<span class="code-object" style="color: #910091">Object</span> o)
    {
        <span class="code-keyword" style="color: #000091">if</span> (<span class="code-keyword" style="color: #000091">this</span> == o)
        {
            <span class="code-keyword" style="color: #000091">return</span> <span class="code-keyword" style="color: #000091">true</span>;
        }

        <span class="code-keyword" style="color: #000091">if</span> (o == <span class="code-keyword" style="color: #000091">null</span> || getClass() != o.getClass())
        {
            <span class="code-keyword" style="color: #000091">return</span> <span class="code-keyword" style="color: #000091">false</span>;
        }

        Phone other = (Phone) o;
        <span class="code-keyword" style="color: #000091">return</span> <span class="code-keyword" style="color: #000091">new</span> EqualsBuilder()
            .append(owner, other.owner)
            .append(phoneType, other.phoneType)
            .append(phoneNumber, other.phoneNumber)
            .isEquals();
    }

    @Override
    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">int</span> hashCode()
    {
        <span class="code-keyword" style="color: #000091">return</span> <span class="code-keyword" style="color: #000091">new</span> HashCodeBuilder()
            .append(owner)
            .append(phoneType)
            .append(phoneNumber)
            .toHashCode();
    }

    @Override
    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">String</span> toString()
    {
        <span class="code-keyword" style="color: #000091">return</span> <span class="code-object" style="color: #910091">String</span>.format(<span class="code-quote" style="color: #009100">"owner[%s], phoneType[%s], phoneNumber[%s]"</span>,  owner, phoneType, phoneNumber);
    }

}

@Entity
@Table(name = <span class="code-quote" style="color: #009100">"PHONE_TYPE"</span>)
<span class="code-keyword" style="color: #000091">public</span> class PhoneType <span class="code-keyword" style="color: #000091">implements</span> Serializable 
{
      @Id
      @Column(name = <span class="code-quote" style="color: #009100">"PHONE_TYPE_ID"</span>)
      <span class="code-keyword" style="color: #000091">private</span> <span class="code-object" style="color: #910091">Integer</span> id;

      @NotNull
      @Column(name = <span class="code-quote" style="color: #009100">"PHONE_TYPE"</span>)
      <span class="code-keyword" style="color: #000091">private</span> <span class="code-object" style="color: #910091">String</span> phoneType;

      <span class="code-keyword" style="color: #000091">protected</span> PhoneType()
      {
          <span class="code-comment" style="color: #808080">//<span class="code-keyword" style="color: #000091; color: #808080">for</span> ORM
</span>      }

      <span class="code-keyword" style="color: #000091">public</span> PhoneType(<span class="code-object" style="color: #910091">Integer</span> id, <span class="code-object" style="color: #910091">String</span> phoneType)
      {
          <span class="code-keyword" style="color: #000091">this</span>.id = id;
          <span class="code-keyword" style="color: #000091">this</span>.phoneType = phoneType;
      }

    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">long</span> getId() {
        <span class="code-keyword" style="color: #000091">return</span> id;
    }

    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">String</span> getPhoneType() {
        <span class="code-keyword" style="color: #000091">return</span> phoneType;
    }

    @Override
    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">boolean</span> equals(<span class="code-object" style="color: #910091">Object</span> o)
    {
        <span class="code-keyword" style="color: #000091">if</span> (<span class="code-keyword" style="color: #000091">this</span> == o)
        {
            <span class="code-keyword" style="color: #000091">return</span> <span class="code-keyword" style="color: #000091">true</span>;
        }

        <span class="code-keyword" style="color: #000091">if</span> (o == <span class="code-keyword" style="color: #000091">null</span> || getClass() != o.getClass())
        {
            <span class="code-keyword" style="color: #000091">return</span> <span class="code-keyword" style="color: #000091">false</span>;
        }

        PhoneType other = (PhoneType) o;
        <span class="code-keyword" style="color: #000091">return</span> <span class="code-keyword" style="color: #000091">new</span> EqualsBuilder()
            .append(id, other.id)
            .append(phoneType, other.phoneType)
            .isEquals();
    }

    @Override
    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">int</span> hashCode()
    {
        <span class="code-keyword" style="color: #000091">return</span> <span class="code-keyword" style="color: #000091">new</span> HashCodeBuilder()
            .append(id)
            .append(phoneType)
            .toHashCode();
    }

    @Override
    <span class="code-keyword" style="color: #000091">public</span> <span class="code-object" style="color: #910091">String</span> toString()
    {
        <span class="code-keyword" style="color: #000091">return</span> <span class="code-object" style="color: #910091">String</span>.format(<span class="code-quote" style="color: #009100">"id[%s], phoneType[%s]"</span>,  id, phoneType);
    }

}
</pre> 
                                                            </div>
                                                        </div> <p style="margin-top:0;margin-bottom:10px;; margin: 10px 0 0 0">DB table scripts:</p> 
                                                        <div class="code panel" style="border-width: 1px;; border: 1px solid #cccccc; background: #f5f5f5; font-size: 12px; line-height: 1.333; font-family: monospace; border: 1px solid #cccccc; -moz-border-radius: 3px 3px 3px 3px; border-radius: 3px 3px 3px 3px; margin: 9px 0">
                                                            <div class="codeContent panelContent" style="padding: 9px 12px"> 
                                                                <pre class="code-java" style="margin: 10px 0 0 0; margin-top: 0; max-height: 30em; overflow: auto; white-space: pre-wrap; word-wrap: normal">
create table EMPLOYEE
(
  EMPLOYEE_ID   number(10),
  FIRST_NAME    varchar(50),
  LAST_NAME     varchar2(50)
);

alter table EMPLOYEE
add constraint EMPLOYEE_PK primary key (EMPLOYEE_ID);

create table PHONE
(
  EMPLOYEE_ID       number(10),
  PHONE_TYPE_ID     number(10),
  PHONE_NUMBER      varchar2(50)
);

alter table PHONE
add constraint PHONE_PK primary key (EMPLOYEE_ID, PHONE_TYPE_ID);

create table PHONE_TYPE
(
  PHONE_TYPE_ID       number(10),
  PHONE_TYPE          varchar2(50)
);

alter table PHONE_TYPE
add constraint PHONE_TYPE_PK primary key (PHONE_TYPE_ID);

insert into EMPLOYEE (EMPLOYEE_ID, FIRST_NAME, LAST_NAME)
values(1, 'first-1', 'last-1');

insert into PHONE (EMPLOYEE_ID, PHONE_TYPE_ID, PHONE_NUMBER)
values(1, 10, '123-123-1234');

insert into PHONE_TYPE (PHONE_TYPE_ID, PHONE_TYPE)
values(10, 'Mobile');

insert into PHONE_TYPE (PHONE_TYPE_ID, PHONE_TYPE)
values(11, 'Home');
</pre> 
                                                            </div>
                                                        </div> <p style="margin-top:0;margin-bottom:10px;; margin: 10px 0 0 0">Test Class:</p> 
                                                        <div class="code panel" style="border-width: 1px;; border: 1px solid #cccccc; background: #f5f5f5; font-size: 12px; line-height: 1.333; font-family: monospace; border: 1px solid #cccccc; -moz-border-radius: 3px 3px 3px 3px; border-radius: 3px 3px 3px 3px; margin: 9px 0">
                                                            <div class="codeContent panelContent" style="padding: 9px 12px"> 
                                                                <pre class="code-java" style="margin: 10px 0 0 0; margin-top: 0; max-height: 30em; overflow: auto; white-space: pre-wrap; word-wrap: normal">
@Transactional(timeout = 60)
<span class="code-keyword" style="color: #000091">public</span> class EmployeeTest <span class="code-keyword" style="color: #000091">extends</span> AbstractTransactionalTestNGSpringContextTests
{
    @Autowired
    EmployeeRepository employeeRepository;

    @Autowired
    PhoneTypeRepository phoneTypeRepository;

    @PersistenceContext(unitName = <span class="code-quote" style="color: #009100">"base"</span>)
    <span class="code-keyword" style="color: #000091">private</span> EntityManager entityManager;

    @Test
    <span class="code-keyword" style="color: #000091">public</span> void addPhoneType()
    {
        Employee employee = employeeRepository.findById(1);
        PhoneType phoneType = phoneTypeRepository.findById(11);

        Assert.assertEquals(employee.getPhones().size(), 1);

        Phone phone = <span class="code-keyword" style="color: #000091">new</span> Phone(employee, phoneType, <span class="code-quote" style="color: #009100">"123-456-7890"</span>);
        employee.addPhones(phone);

        employeeRepository.persist(employee);
        entityManager.flush();

        Assert.assertEquals(employee.getPhones().size(), 2);
    }

}
</pre> 
                                                            </div>
                                                        </div> <p style="margin-top:0;margin-bottom:10px;; margin: 10px 0 0 0">For the above test case, the employee with id=1 already exists in the DB. This employee has 'Mobile' phone - 123-123-1234. The test is adding a new phone 'Home' phone - 123-456-7890.</p> <p style="margin-top:0;margin-bottom:10px;; margin: 10px 0 0 0">The test case succeeds but generates additional SQL update - "update PHONE set PHONE_TYPE_ID=? where PHONE_TYPE_ID=? and EMPLOYEE_ID=?", which updates PHONE_TYPE_ID to itself. Listed blow are the SQLs executed while running the test (We use P6Spy which shows the SQLs with parameter values) :</p> 
                                                        <div class="code panel" style="border-width: 1px;; border: 1px solid #cccccc; background: #f5f5f5; font-size: 12px; line-height: 1.333; font-family: monospace; border: 1px solid #cccccc; -moz-border-radius: 3px 3px 3px 3px; border-radius: 3px 3px 3px 3px; margin: 9px 0">
                                                            <div class="codeContent panelContent" style="padding: 9px 12px"> 
                                                                <pre class="code-java" style="margin: 10px 0 0 0; margin-top: 0; max-height: 30em; overflow: auto; white-space: pre-wrap; word-wrap: normal">
Hibernate: select employee0_.EMPLOYEE_ID as EMPLOYEE_ID1_43_0_, employee0_.FIRST_NAME as FIRST_NAME2_43_0_, employee0_.LAST_NAME as LAST_NAME3_43_0_, phones1_.EMPLOYEE_ID as EMPLOYEE_ID3_43_1_, phones1_.PHONE_TYPE_ID as PHONE_TYPE_ID2_65_1_, phones1_.EMPLOYEE_ID as EMPLOYEE_ID3_65_1_, phones1_.PHONE_TYPE_ID as PHONE_TYPE_ID2_1_, phones1_.PHONE_TYPE_ID as PHONE_TYPE_ID2_65_2_, phones1_.EMPLOYEE_ID as EMPLOYEE_ID3_65_2_, phones1_.PHONE_NUMBER as PHONE_NUMBER1_65_2_, phonetype3_.PHONE_TYPE_ID as PHONE_TYPE_ID1_66_4_, phonetype3_.PHONE_TYPE as PHONE_TYPE2_66_4_ from EMPLOYEE employee0_ left <span class="code-keyword" style="color: #000091">outer</span> join PHONE phones1_ on employee0_.EMPLOYEE_ID=phones1_.EMPLOYEE_ID left <span class="code-keyword" style="color: #000091">outer</span> join PHONE_TYPE phonetype2_ on phones1_.PHONE_TYPE_ID=phonetype2_.PHONE_TYPE_ID left <span class="code-keyword" style="color: #000091">outer</span> join PHONE_TYPE phonetype3_ on phones1_.PHONE_TYPE_ID=phonetype3_.PHONE_TYPE_ID where employee0_.EMPLOYEE_ID=?
1457118857445|6|1|statement|select employee0_.EMPLOYEE_ID as EMPLOYEE_ID1_43_0_, employee0_.FIRST_NAME as FIRST_NAME2_43_0_, employee0_.LAST_NAME as LAST_NAME3_43_0_, phones1_.EMPLOYEE_ID as EMPLOYEE_ID3_43_1_, phones1_.PHONE_TYPE_ID as PHONE_TYPE_ID2_65_1_, phones1_.EMPLOYEE_ID as EMPLOYEE_ID3_65_1_, phones1_.PHONE_TYPE_ID as PHONE_TYPE_ID2_1_, phones1_.PHONE_TYPE_ID as PHONE_TYPE_ID2_65_2_, phones1_.EMPLOYEE_ID as EMPLOYEE_ID3_65_2_, phones1_.PHONE_NUMBER as PHONE_NUMBER1_65_2_, phonetype3_.PHONE_TYPE_ID as PHONE_TYPE_ID1_66_4_, phonetype3_.PHONE_TYPE as PHONE_TYPE2_66_4_ from EMPLOYEE employee0_ left <span class="code-keyword" style="color: #000091">outer</span> join PHONE phones1_ on employee0_.EMPLOYEE_ID=phones1_.EMPLOYEE_ID left <span class="code-keyword" style="color: #000091">outer</span> join PHONE_TYPE phonetype2_ on phones1_.PHONE_TYPE_ID=phonetype2_.PHONE_TYPE_ID left <span class="code-keyword" style="color: #000091">outer</span> join PHONE_TYPE phonetype3_ on phones1_.PHONE_TYPE_ID=phonetype3_.PHONE_TYPE_ID where employee0_.EMPLOYEE_ID=?|select employee0_.EMPLOYEE_ID as EMPLOYEE_ID1_43_0_, employee0_.FIRST_NAME as FIRST_NAME2_43_0_, employee0_.LAST_NAME as LAST_NAME3_43_0_, phones1_.EMPLOYEE_ID as EMPLOYEE_ID3_43_1_, phones1_.PHONE_TYPE_ID as PHONE_TYPE_ID2_65_1_, phones1_.EMPLOYEE_ID as EMPLOYEE_ID3_65_1_, phones1_.PHONE_TYPE_ID as PHONE_TYPE_ID2_1_, phones1_.PHONE_TYPE_ID as PHONE_TYPE_ID2_65_2_, phones1_.EMPLOYEE_ID as EMPLOYEE_ID3_65_2_, phones1_.PHONE_NUMBER as PHONE_NUMBER1_65_2_, phonetype3_.PHONE_TYPE_ID as PHONE_TYPE_ID1_66_4_, phonetype3_.PHONE_TYPE as PHONE_TYPE2_66_4_ from EMPLOYEE employee0_ left <span class="code-keyword" style="color: #000091">outer</span> join PHONE phones1_ on employee0_.EMPLOYEE_ID=phones1_.EMPLOYEE_ID left <span class="code-keyword" style="color: #000091">outer</span> join PHONE_TYPE phonetype2_ on phones1_.PHONE_TYPE_ID=phonetype2_.PHONE_TYPE_ID left <span class="code-keyword" style="color: #000091">outer</span> join PHONE_TYPE phonetype3_ on phones1_.PHONE_TYPE_ID=phonetype3_.PHONE_TYPE_ID where employee0_.EMPLOYEE_ID=1
1457118857462|-1||resultset|select employee0_.EMPLOYEE_ID as EMPLOYEE_ID1_43_0_, employee0_.FIRST_NAME as FIRST_NAME2_43_0_, employee0_.LAST_NAME as LAST_NAME3_43_0_, phones1_.EMPLOYEE_ID as EMPLOYEE_ID3_43_1_, phones1_.PHONE_TYPE_ID as PHONE_TYPE_ID2_65_1_, phones1_.EMPLOYEE_ID as EMPLOYEE_ID3_65_1_, phones1_.PHONE_TYPE_ID as PHONE_TYPE_ID2_1_, phones1_.PHONE_TYPE_ID as PHONE_TYPE_ID2_65_2_, phones1_.EMPLOYEE_ID as EMPLOYEE_ID3_65_2_, phones1_.PHONE_NUMBER as PHONE_NUMBER1_65_2_, phonetype3_.PHONE_TYPE_ID as PHONE_TYPE_ID1_66_4_, phonetype3_.PHONE_TYPE as PHONE_TYPE2_66_4_ from EMPLOYEE employee0_ left <span class="code-keyword" style="color: #000091">outer</span> join PHONE phones1_ on employee0_.EMPLOYEE_ID=phones1_.EMPLOYEE_ID left <span class="code-keyword" style="color: #000091">outer</span> join PHONE_TYPE phonetype2_ on phones1_.PHONE_TYPE_ID=phonetype2_.PHONE_TYPE_ID left <span class="code-keyword" style="color: #000091">outer</span> join PHONE_TYPE phonetype3_ on phones1_.PHONE_TYPE_ID=phonetype3_.PHONE_TYPE_ID where employee0_.EMPLOYEE_ID=1|EMPLOYEE_ID3_43_1_ = 1, EMPLOYEE_ID3_65_1_ = 1, EMPLOYEE_ID3_65_2_ = 1, FIRST_NAME2_43_0_ = first-1, LAST_NAME3_43_0_ = last-1, PHONE_NUMBER1_65_2_ = 123-123-1234, PHONE_TYPE2_66_4_ = Mobile, PHONE_TYPE_ID1_66_4_ = 10, PHONE_TYPE_ID2_1_ = 10, PHONE_TYPE_ID2_65_1_ = 10, PHONE_TYPE_ID2_65_2_ = 10
Hibernate: select phonetype0_.PHONE_TYPE_ID as PHONE_TYPE_ID1_66_0_, phonetype0_.PHONE_TYPE as PHONE_TYPE2_66_0_ from PHONE_TYPE phonetype0_ where phonetype0_.PHONE_TYPE_ID=?
1457118857476|2|1|statement|select phonetype0_.PHONE_TYPE_ID as PHONE_TYPE_ID1_66_0_, phonetype0_.PHONE_TYPE as PHONE_TYPE2_66_0_ from PHONE_TYPE phonetype0_ where phonetype0_.PHONE_TYPE_ID=?|select phonetype0_.PHONE_TYPE_ID as PHONE_TYPE_ID1_66_0_, phonetype0_.PHONE_TYPE as PHONE_TYPE2_66_0_ from PHONE_TYPE phonetype0_ where phonetype0_.PHONE_TYPE_ID=11
1457118857477|-1||resultset|select phonetype0_.PHONE_TYPE_ID as PHONE_TYPE_ID1_66_0_, phonetype0_.PHONE_TYPE as PHONE_TYPE2_66_0_ from PHONE_TYPE phonetype0_ where phonetype0_.PHONE_TYPE_ID=11|PHONE_TYPE2_66_0_ = Home
Hibernate: insert into PHONE (PHONE_NUMBER, PHONE_TYPE_ID, EMPLOYEE_ID) values (?, ?, ?)
1457118857605|3|1|statement|insert into PHONE (PHONE_NUMBER, PHONE_TYPE_ID, EMPLOYEE_ID) values (?, ?, ?)|insert into PHONE (PHONE_NUMBER, PHONE_TYPE_ID, EMPLOYEE_ID) values ('123-456-7890', 11, 1)
Hibernate: update PHONE set PHONE_TYPE_ID=? where PHONE_TYPE_ID=? and EMPLOYEE_ID=?
1457118857611|2|1|statement|update PHONE set PHONE_TYPE_ID=? where PHONE_TYPE_ID=? and EMPLOYEE_ID=?|update PHONE set PHONE_TYPE_ID=10 where PHONE_TYPE_ID=10 and EMPLOYEE_ID=1
Hibernate: update PHONE set PHONE_TYPE_ID=? where PHONE_TYPE_ID=? and EMPLOYEE_ID=?
1457118857613|2|1|statement|update PHONE set PHONE_TYPE_ID=? where PHONE_TYPE_ID=? and EMPLOYEE_ID=?|update PHONE set PHONE_TYPE_ID=11 where PHONE_TYPE_ID=11 and EMPLOYEE_ID=1
1457118857635|21|1|rollback||
PASSED: addPhoneType
</pre> 
                                                            </div>
                                                        </div> </td> 
                                                </tr> 
                                            </tbody>
                                        </table> </td> 
                                </tr> 
                                <tr> 
                                    <td class="email-content-main mobile-expand " style="padding: 0px; border-collapse: collapse; border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #ffffff" bgcolor="#ffffff"> 
                                        <table id="actions-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 1px"> 
                                            <tbody>
                                                <tr> 
                                                    <td id="actions-pattern-container" valign="middle" style="padding: 0px; border-collapse: collapse; padding: 10px 0 10px 24px; vertical-align: middle; padding-left: 0"> 
                                                        <table align="left" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt"> 
                                                            <tbody>
                                                                <tr> 
                                                                    <td class="actions-pattern-action-icon-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 0px; vertical-align: middle"> <a href="https://hibernate.atlassian.net/browse/HHH-10590#add-comment" target="_blank" title="Add Comment" style="color: #3b73af; text-decoration: none"> <img class="actions-pattern-action-icon-image" src="cid:jira-generated-image-static-comment-icon-87e93e61-c9e5-4dd9-b8b2-7fc389633041" alt="Add Comment" title="Add Comment" height="16" width="16" border="0" style="vertical-align: middle"> </a> </td> 
                                                                    <td class="actions-pattern-action-text-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 4px; padding-left: 5px"> <a href="https://hibernate.atlassian.net/browse/HHH-10590#add-comment" target="_blank" title="Add Comment" style="color: #3b73af; text-decoration: none">Add Comment</a> </td> 
                                                                </tr> 
                                                            </tbody>
                                                        </table> </td> 
                                                </tr> 
                                            </tbody>
                                        </table> </td> 
                                </tr> 
                                <!-- there needs to be content in the cell for it to render in some clients --> 
                                <tr> 
                                    <td class="email-content-rounded-bottom mobile-expand" style="padding: 0px; border-collapse: collapse; color: #ffffff; padding: 0 15px 0 16px; height: 5px; line-height: 5px; background-color: #ffffff; border-top: 0; border-left: 1px solid #cccccc; border-bottom: 1px solid #cccccc; border-right: 1px solid #cccccc; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; mso-line-height-rule: exactly" height="5" bgcolor="#ffffff">&nbsp;</td> 
                                </tr> 
                            </tbody>
                        </table> </td> 
                </tr> 
                <tr> 
                    <td id="footer-pattern" style="padding: 0px; border-collapse: collapse; padding: 12px 20px"> 
                        <table id="footer-pattern-container" cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt"> 
                            <tbody>
                                <tr> 
                                    <td id="footer-pattern-text" class="mobile-resize-text" width="100%" style="padding: 0px; border-collapse: collapse; color: #999999; font-size: 12px; line-height: 18px; font-family: Arial, sans-serif; mso-line-height-rule: exactly; mso-text-raise: 2px"> This message was sent by Atlassian JIRA <span id="footer-build-information">(v7.2.0-OD-03-010#72000-<span title="d4f0e55382250d79af2a76d8b0ff23d12af94da2" data-commit-id="d4f0e55382250d79af2a76d8b0ff23d12af94da2}">sha1:d4f0e55</span>)</span> </td> 
                                    <td id="footer-pattern-logo-desktop-container" valign="top" style="padding: 0px; border-collapse: collapse; padding-left: 20px; vertical-align: top"> 
                                        <table style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt"> 
                                            <tbody>
                                                <tr> 
                                                    <td id="footer-pattern-logo-desktop-padding" style="padding: 0px; border-collapse: collapse; padding-top: 3px"> <img id="footer-pattern-logo-desktop" src="cid:jira-generated-image-static-footer-desktop-logo-e80ee7c7-a56f-4775-877c-6a0cea8dc26d" alt="Atlassian logo" title="Atlassian logo" width="169" height="36" class="image_fix"> </td> 
                                                </tr> 
                                            </tbody>
                                        </table> </td> 
                                </tr> 
                            </tbody>
                        </table> </td> 
                </tr> 
            </tbody>
        </table>   
    </body>
</html>