我有以下两个域模型。
user
----
userid (PK)
type
like
------
likeid (PK)
userid (PK)
word
like.userid引用User中的userid,它是复合主键的一部分。
并插入测试代码
@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);
}
}
运行JUnit测试时出错。
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]
首先,有可能吗?
其次,如果可能,我的代码有什么问题?
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 + "]";
}
}
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
答案 0 :(得分:0)
我认为问题出在LIKE Entity中,因为like是一个Sql关键字
使用此代码:
@Entity
@Table(name = "like_table")
@Data
public class Like {
...
}