简体   繁体   中英

Spring Data JPA - How to insert child entities with composite key?

I have the following two domain model.

user
----
userid (PK)
type

like
------
likeid (PK)
userid (PK)
word

like.userid references userid in User and is part of the composite primary key.

And insert test 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);
    }
}

I have an error when run the JUnit test.

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]

First, is it possible?

Second, if possible, what is wrong in my code?

User.java :

@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<Like> likes = new ArrayList<>();

    public enum USER_TYPE {USER, ADMIN}

    @Override
    public String toString() {
        return "User [userNo=" + userNo + ", type=" + type + ", likes=" + likes + "]";
    }

}

And Like.java :

@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;

    }
}

https://gist.github.com/okihouse/78dacfeb37f4753628433beb1b591f99

I think the problem is in LIKE Entity because like is a Sql keyword

use this code :

@Entity
@Table(name = "like_table")
@Data
public class Like {
... 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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