Spring存储使用子类创建的具有属性的对象的存储库

huangapple go评论73阅读模式
英文:

Spring repository saving objects with attributes that are made using subclasses

问题

所以我有一个 VoteEvent 类,它是 Event 类的子类。Event 类的结构如下:

@MappedSuperclass
public class Event {
    @Column(name="name")
    private String name;
    @Column(name="timeSlot")
    private String timeSlot;

VoteEvent 类的结构如下:

@Entity
@Table(name = "vote_events")
public class VoteEvent extends Event implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name = "upVotes")
    private Integer upVotes = 0;

在我的用户对象中,我有一个 VoteEvents 数组,以便我知道特定用户赞过哪些事件。在我的前端中,当我更新用户赞过的事件时,我传入了一个具有 timeslot 和 name 属性的 vote 事件数组。然而,当我执行以下操作时:

return new ResponseEntity<User>(userRepository.save(_user), HttpStatus.OK);

响应是正确的,用户的 vote 事件数组具有 name 和 time slot 属性,但是当我查看数据库时,用户的 vote 事件数组具有空的 name 和 time slot 属性。所以,每当我使用 Spring Repository 保存一个带有子类构造的对象时,它们会丢失超类属性。

英文:

So i have the VoteEvent class that is a a subclass of the Event class. The Event class looks like this:

@MappedSuperclass
public class Event {
    @Column(name=&quot;name&quot;)
    private String name;
    @Column(name=&quot;timeSlot&quot;)
    private String timeSlot;

The VoteEvent class looks like this:

@Entity
@Table(name = &quot;vote_events&quot;)

public class VoteEvent extends Event implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name = &quot;upVotes&quot;)
    private Integer upVotes = 0;

In my user object, I have an array of VoteEvents so that I know which events a specific user as upvoted. In my frontend, I pass in an array of vote events that have the timeslot and name attributes when I update the user upvoted events. However when I do:

return new ResponseEntity&lt;User&gt;(userRepository.save(_user), HttpStatus.OK);

, the response is correct and the user array of vote events have the name and time slot attributes, but when I look in the database, the user array of vote events have the null name and time slot attributes. So, whenever I save an object using a spring repository that has objects that are constructed with subclasses, they lose the superclass attributes.

答案1

得分: 0

以下是翻译好的代码部分:

尝试以下代码我这边可以正常运行

package com.apimanager.metadata;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.experimental.SuperBuilder;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;

@Data
@SuperBuilder(toBuilder = true)
@RequiredArgsConstructor
@MappedSuperclass
public class Event {
    @Column(name = "name")
    private String name;
    @Column(name = "timeSlot")
    private String timeSlot;
}

对于VoteEvent和Event请提供**`@SuperBuilder`****`@RequiredArgsConstructor`**可能是因为这些属性在**`VoteEvent`**对象中不可访问

@SuperBuilder(toBuilder = true)
@RequiredArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "vote_events")
public class VoteEvent extends Event implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name = "upVotes")
    private Integer upVotes = 0;
}
英文:

Try below code, it's working fine for me.

package com.apimanager.metadata;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.experimental.SuperBuilder;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;

@Data
@SuperBuilder(toBuilder = true)
@RequiredArgsConstructor
@MappedSuperclass
public class Event {
    @Column(name = &quot;name&quot;)
    private String name;
    @Column(name = &quot;timeSlot&quot;)
    private String timeSlot;
}

For VoteEvent and Event kindly provide @SuperBuilder and @RequiredArgsConstructor. May be it's due to those property are not accessible in VoteEvent object

@SuperBuilder(toBuilder = true)
@RequiredArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = &quot;vote_events&quot;)
public class VoteEvent extends Event implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name = &quot;upVotes&quot;)
    private Integer upVotes = 0;

huangapple
  • 本文由 发表于 2020年8月23日 05:31:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63541303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定