- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个实体 User
和 Candidat
,其中 Candidat
扩展了类 User
,如下所示:
用户实体:
@Entity
@Table(name="users")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="TYPE_USER",discriminatorType=DiscriminatorType.STRING,length=2)
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME,include=JsonTypeInfo.As.PROPERTY,property="type")
@JsonSubTypes({
@Type(name="UC",value=Candidat.class)
})
@XmlSeeAlso({Candidat.class})
public class User implements Serializable {
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long codeUser;
//other code ...
候选实体:
@Entity
@DiscriminatorValue("UC")
@XmlType(name = "UC")
public class Candidat extends User {
private String codeMassar;
为了保存新的候选人,我调用这个存储库方法:
candidatRepository.save()
来自:
public interface CandidatRepository extends JpaRepository<Candidat, String> {
}
这是我调用 save 方法的休息服务:
@RequestMapping(value = "/candidats", method = RequestMethod.POST)
public Candidat saveCandidat(@RequestBody Candidat candidat) throws Exception {
return candidatMetier.saveCandidat(candidat);
}
问题是当我想保存一个新的候选人时,如下所示:
{
"username": "User",
"password": "123456",
"email": "user@gmail.com"
}
我收到一条错误消息:
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type' that is to contain type id (for class org.capvalue.fme.domain.Candidat)
据我了解,我必须在要发送的 JSON 对象中指定类型,但我认为这没有必要,因为我保存了一个新的 Candidat
,其中包含 @DiscriminatorValue("UC")
,所以当它的 save
在 User
中时表它将自动与 type='UC'
一起保存。
我该如何解决这个问题?
最佳答案
what I understand from it that I have to specify the type in the json object I'm sending
由于您已添加 @JsonTypeInfo
,因此您应该使用 type
字段指定对象实例的实际类。例如,如果您将 type
字段设置为 UC
,Jackson 将创建 Candidat
类的实例。
but I dont think that's necessary because I save a new Candidat which has the @DiscriminatorValue("UC") so when its save in the User table it will be saved with type='UC' automatically.
@DiscriminatorValue
将由您的 JPA 提供程序处理。相反,@JsonTypeInfo 是 Jackson 的概念,所以不!您不能指望 JPA @DiscriminatorValue
帮助 Jackson 确定对象实例的实际类型。
您应该以 JSON 表示形式发送 type
信息,或者从您的 User
中删除 @JsonTypeInfo
和 @JsonSubTypes
> 类。我想删除 Jackson 类型注释是更好的方法,因为您在以下位置使用实际的子类:
public Candidat saveCandidat(@RequestBody Candidat candidat) { ... }
此外,尝试定义一些DTO并将它们作为 REST 端点返回值返回。这种方法的一个优点是您的 Jackson 和 JPA 元数据是分开的,因此您可以避免这些问题。
关于java - 如何使用 DiscriminatorValue 保存实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36649683/
我们有以下类(class) @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) // optional annotation a
我有两个实体 User 和 Candidat ,其中 Candidat 扩展了类 User ,如下所示: 用户实体: @Entity @Table(name="users") @Inheritance
我有几个实体与具有一个基类的 @DiscriminatorValue 共享同一个数据库表: @Entity(name = "base_details") @DiscriminatorColumn(na
我有这个父类: @Entity @Inheritance(strategy = InheritanceType.JOINED) @Table(name = "BSEntity") @Discrimin
我有一个看起来像这样的实体 @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(nam
给定: @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="postType", discri
我正在使用具有连接继承和如下所示的数据库结构的 JPA: ACTION --------- ACTION_ID ACTION_MAPPING_ID ACTION_TYPE DELIVERY_CHANN
我有具有连接类型的实体的继承关系。 @Entity @Table(name = "MSM_SUBSCRIPTION") @DiscriminatorColumn(name = "SUBSCRIPTIO
Hibernate 有强大的机制可以使多个实体共享数据库中的同一个表。对我来说,能够获取所有记录(无论子类型如何)将是最重要的。然而这似乎不起作用。 假设我们有一些标准设置: @Entity @Tab
我有以下实体: @Table(name="Animal") @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @Discrim
在 hibernate 中使用 DiscriminatorValue 注解的最佳场景和时间是什么时候? 最佳答案 这两个链接最能帮助我理解继承概念: http://docs.oracle.com/ja
我正在尝试实现 JPA 实体之间的继承关系。 借用示例: http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual
我有一个经典的 Hibernate @Inheritance(strategy=InheritanceType.SINGLE_TABLE) 和 @DiscriminatorFormula。它工作正常。
我是一名优秀的程序员,十分优秀!