英文:
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="name")
private String name;
@Column(name="timeSlot")
private String timeSlot;
The VoteEvent class looks like this:
@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;
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<User>(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 = "name")
private String name;
@Column(name = "timeSlot")
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 = "vote_events")
public class VoteEvent extends Event implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "upVotes")
private Integer upVotes = 0;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论