调试完头后,我没有想法,需要一些帮助。让我感到非常好奇的是我想要做的事情的简单性以及实现它的不可能性...... 我试图通过创建一些实体,将它们作为剩余资源公开并将它们保存在内存中的h2数据库中来制作弹簧休息数据的小型演示。
以下代码有效:
@Entity
@Table(name="info")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="INFO_TYPE", discriminatorType=DiscriminatorType.STRING)
public class ItemInfo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
@RepositoryRestResource(collectionResourceRel="itemInfos", path="itemInfos")
public interface ItemInfoRepository extends PagingAndSortingRepository<ItemInfo, Long> {
}
当我发出卷曲http://localhost:8080/itemInfos的卷曲请求时,我得到以下正确答案:
{
"_embedded" : {
"itemInfos" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/itemInfos"
},
"profile" : {
"href" : "http://localhost:8080/profile/itemInfos"
}
},
"page" : {
"size" : 20,
"totalElements" : 0,
"totalPages" : 0,
"number" : 0
}
}
但是,只要我添加ItemInfo的子类实体,/ itemInfos资源就不再可用了。
所以添加课程:
@Entity
@Table(name="info")
@DiscriminatorValue(value="COMMENT")
public class UserComment extends ItemInfo {
private String from;
private String comment;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
将导致与之前相同的curl命令产生错误:
{"timestamp":1488282548770,"status":500,"error":"Internal Server Error","exception":"org.springframework.dao.InvalidDataAccessResourceUsageException","message":"could not prepare statement; SQL [select iteminfo0_.id as id2_0_, iteminfo0_.comment as comment3_0_, iteminfo0_.from as from4_0_, iteminfo0_.info_type as info_typ1_0_ from info iteminfo0_ limit ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement","path":"/itemInfos"}
此外,尝试添加新的itemInfo会导致类似的错误:
{"timestamp":1488282718547,"status":500,"error":"Internal Server Error","exception":"org.springframework.dao.InvalidDataAccessResourceUsageException","message":"could not prepare statement; SQL [insert into info (id, info_type) values (null, 'ItemInfo')]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement","path":"/itemInfos"}
添加UserCommentRepository也无法以任何方式解决问题:
public interface UserCommentRepository extends PagingAndSortingRepository<UserComment, Long> { }
在这种情况下,如果我尝试添加新用户评论:
curl -i -X POST -H "Content-Type:application/json" -d "{ \"from\" : \"Ana\", \"comment\" : \"some comment\" }" http://localhost:8080/userComments
我还有一个错误:
{"timestamp":1488282968879,"status":500,"error":"Internal Server Error","exception":"org.springframework.dao.InvalidDataAccessResourceUsageException","message":"could not prepare statement; SQL [insert into info (id, comment, from, info_type) values (null, ?, ?, 'COMMENT')]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement","path":"/userComments"}
似乎为我的实体添加继承完全破坏了我的资源。你们中有人有过类似的问题吗?!
答案 0 :(得分:0)
好的......我想通了(大多是错误的)而且更令人沮丧。 问题是使用变量名“from”。似乎spring数据休息使用它作为关键字。一旦我重构了这个变量的名称,一切都按预期工作。不幸的是,异常和错误消息根本没有帮助。 希望别人不会经历同样的调试......