title: Spring Data JPA - How to insert child entities with composite key?
body: <p>I have the following two domain model.</p>
  
  <pre><code>user
  ----
  userid (PK)
  type
  
  like
  ------
  likeid (PK)
  userid (PK)
  word
  </code></pre>
  
  <p>like.userid references userid in User and is part of the composite primary key. </p>
  
  <p>And insert test code</p>
  
  <pre class="lang-java prettyprint-override"><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = SpringJpaTestApplication.class)
  @WebAppConfiguration
  @IntegrationTest("server.port:0")
  public class Test {
  
      @Autowired
      private UserRepository userRepository;
  
      @org.junit.Test
      public void application() throws Exception {
          User user = new User();
          user.setType(USER_TYPE.USER);
  
          Like like = new Like();
          LikeKey likeKey = new LikeKey();
          likeKey.setLikeNo(1L);
  
          like.setLikeKey(likeKey);
          like.setWord("word");
          like.setUser(user);
  
          user.getLikes().add(like);
  
          userRepository.save(user);
      }
  }
  </code></pre>
  
  <p>I have an error when run the JUnit test.</p>
  
  <pre><code>Hibernate: insert into user (uno, user_type) values (null, ?)
  Hibernate: insert into like (word, like_no, uno) values (?, ?, ?)
  2016-07-22 13:44:36.499 WARN SqlExceptionHelper : SQL Error: 42001, SQLState: 42001
  2016-07-22 13:44:36.499 ERROR SqlExceptionHelper : Syntax error in SQL statement "INSERT INTO LIKE[*] (WORD, LIKE_NO, UNO) VALUES (?, ?, ?) "; expected "identifier"; SQL statement: [42001-190]
  </code></pre>
  
  <p>First, is it possible?</p>
  
  <p>Second, if possible, what is wrong in my code?</p>
  
  <p><code>User.java</code> :</p>
  
  <pre><code>@Entity
  @Table(name = "user")
  @Data
  public class User {
  
      @Id
      @GeneratedValue
      @Column(name = "uno")
      private Long userNo;
  
      @Column(name = "user_type")
      @Enumerated(EnumType.STRING)
      private USER_TYPE type;
  
      @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
      private List&lt;Like&gt; likes = new ArrayList&lt;&gt;();
  
      public enum USER_TYPE {USER, ADMIN}
  
      @Override
      public String toString() {
          return "User [userNo=" + userNo + ", type=" + type + ", likes=" + likes + "]";
      }
  
  }
  </code></pre>
  
  <p>And <code>Like.java</code> :</p>
  
  <pre><code>@Entity
  @Table(name = "like")
  @Data
  public class Like {
  
      @EmbeddedId
      private LikeKey likeKey;
  
      @Column(name = "word")
      private String word;
  
      @MapsId(value = "userNo")
      @ManyToOne(optional = false, targetEntity = User.class)
      @JoinColumn(name = "uno")
      private User user;
  
      @Embeddable
      @Data
      @NoArgsConstructor
      @AllArgsConstructor
      public static class LikeKey implements Serializable {
  
          private static final long serialVersionUID = 2116470670954200809L;
  
          @Column(name = "likeNo")
          private Long likeNo;
  
          @Column(name = "uno")
          private Long userNo;
  
      }
  }
  </code></pre>
  
  <p><a href="https://gist.github.com/okihouse/78dacfeb37f4753628433beb1b591f99" rel="nofollow">https://gist.github.com/okihouse/78dacfeb37f4753628433beb1b591f99</a></p>