2017-02-28 70 views
0

经过调试我的头,我没有想法,需要一些帮助。真正引起我兴趣的是我试图做的简单性,并且仍然不可能实现它... 我试图通过创建一些实体来制作一些弹簧休息数据的小演示,并将它们公开为其余的资源,坚持他们在内存中的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

好吧......我想通了(主要是错误),它更令人沮丧。 问题是使用变量名“from”。春天的数据似乎将其用作关键字。只要我重构了这个变量的名字,一切都按预期工作。不幸的是,例外和错误消息根本没有帮助。 希望别人不会经历相同的调试地狱...