【问题标题】:CRUD repository not returning the Super Class dataCRUD 存储库不返回超类数据
【发布时间】:2016-11-10 17:04:36
【问题描述】:

我有一个类,它扩展了另一个类的一些基本属性。我正在使用 CRUD 存储库与我的 MySql 数据库进行通信。我没有得到基类中定义的字段的值。

例如:我将 createdBy 和 modifiedBy 设置为 null。

实体类

@JsonInclude(JsonInclude.Include.NON_NULL)
@Entity
@Table(name = "user")
class User extends BaseEntity {

 @JsonProperty("id")
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "id")
 private String id;

 @JsonProperty("firstName")
 @Column(name = "first_name")
 private String firstName;

 @JsonProperty("lastName")
 @Column(name = "last_name")
 private String lastName;

 /**
  * @return the id
  */
 public String getId() {
  return id;
 }

 /**
  * @param id the id to set
  */
 public void setId(String id) {
  this.id = id;
 }

 /**
  * @return the firstName
  */
 public String getFirstName() {
  return firstName;
 }

 /**
  * @param firstName the firstName to set
  */
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 /**
  * @return the lastName
  */
 public String getLastName() {
  return lastName;
 }

 /**
  * @param lastName the lastName to set
  */
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
}

基类

class BaseEntity {

 @JsonProperty("createdBy")
 @Column(name = "created_by")
 public String createdBy;

 @JsonProperty("modifiedBy")
 @Column(name = "modified_by")
 public String modifiedBy;

 /**
  * @return the createdBy
  */
 public String getCreatedBy() {
  return createdBy;
 }

 /**
  * @param createdBy the createdBy to set
  */
 public void setCreatedBy(String createdBy) {
  this.createdBy = createdBy;
 }

 /**
  * @return the modifiedBy
  */
 public String getModifiedBy() {
  return modifiedBy;
 }

 /**
  * @param modifiedBy the modifiedBy to set
  */
 public void setModifiedBy(String modifiedBy) {
  this.modifiedBy = modifiedBy;
 }
}

存储库

@Transactional
public interface UserRepo extends CrudRepository < User, String > {}

服务实现方式

public User getUser(String userId) {
    User user = gUnifyUserRepository.findOne(userId); 
    //Here the data for createdBy and modifiedBy is returned as null though i have value in DB
    return response;
}

感谢任何帮助。

【问题讨论】:

    标签: mysql spring-boot repository crud


    【解决方案1】:
    public class BaseEntity implements Serializable {
    
        @LastModifiedDate
        private Date lastModified;
        @CreatedDate
        private Date createdAt;
    

    使用上面的代码,让我知道

    【讨论】:

    • 我的问题不是特别针对审计领域。我没有得到子类字段的值。在我的示例中,我没有 lastModified 和 createdAt。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    相关资源
    最近更新 更多