【发布时间】:2017-02-28 12:00:22
【问题描述】:
调试完我的脑袋后,我没有想法,需要一些帮助。真正让我很感兴趣的是我正在尝试做的事情的简单性,但仍然不可能实现它...... 我正在尝试通过创建一些实体,将它们作为休息资源公开并将它们持久保存在内存中的 h2 数据库中,来做一个 spring 休息数据的小演示。
以下代码有效:
@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> {
}
当我发出 curl http://localhost:8080/itemInfos 的 curl 请求时,我得到以下正确响应:
{
"_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"}
似乎为我的实体添加继承完全破坏了我的资源。你们中有人遇到过类似的问题吗?!
【问题讨论】:
标签: spring rest jpa inheritance