title: hibernate many to one mapping, should I insert this way?
body: <p>Let's say we have two tables, <code>user</code> and <code>domain</code>. User may have one Domain, one Domain may be for many users. So we will do unidirectional many to one:</p>
  
  <pre><code>@Entity
  @Table(name = "DOMAIN")
  public class Domain implements Serializable
  {
      @Id
      @Column(name = "DOMAIN_ID")
      private Integer domainId;
  
      @Column(name = "NAME")
      private String domainName;
  }
  
  @Entity
  @Table(name = "\"USER\"")
  public class User implements Serializable
  {
      @Id
      @GeneratedValue(generator = "USER_SEQ")
      @GenericGenerator(name = "USER_SEQ", strategy = "sequence", parameters = @Parameter(name = "sequence", value = "SEQ_USER_ID"))
      @Column(name = "USER_ID", nullable = false)
      private Long userId;
  
      @Column(name = "FIRST_NAME")
      private String firstName;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "DOMAIN_ID")
      @ForeignKey(name = "DOMAIN_ID")
      private Domain domain;
  }
  </code></pre>
  
  <p><code>Domain</code> table is something unchangable, dictionary. While <code>User</code> is editable table.</p>
  
  <p>I'm creating a basic form where user selects domain in which new user will be created. Lets say that my controller received those data:</p>
  
  <pre><code>Integer domainId = 1;
  String firstName = "aaa";
  </code></pre>
  
  <p>So I'm creating new user:</p>
  
  <pre><code>User newUser = new User();
  newUser.setFirstName( firstName );
  </code></pre>
  
  <p>Now comes my question, should I do this way?</p>
  
  <pre><code>Domain domain = somthingThatWillFetchObjectFromDb.getDomain( domainId );
  newUser.setDomain( domain );
  //save user
  </code></pre>
  
  <p>THis will generate additional select, to fetch domain. Of course I can use <code>Integer domainId</code> instead of POJO, but that's not ORM. So once again the question, is this the way it should be done?</p>