【问题标题】:Spring Data Rest - resource becomes unavailable when subclassedSpring Data Rest - 子类化时资源变得不可用
【发布时间】: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


    【解决方案1】:

    好的...我想通了(大部分是错误的),这更令人沮丧。 问题是使用了变量名“from”。似乎 spring data rest 使用 this 作为关键字。一旦我重构了这个变量的名称,一切都按预期工作。不幸的是,异常和错误消息根本没有帮助。 希望其他人不会经历同样的调试地狱......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 2015-08-30
      • 2017-03-18
      • 2015-07-28
      • 2014-10-08
      • 2016-09-01
      • 2017-09-27
      相关资源
      最近更新 更多