在Spring Boot中,如何使用扩展的setter和getter方法来设置(扩展的)属性?

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

In Spring Boot, how do you set a (extended) property using extended setters and getters?

问题

以下是翻译好的内容:

我正在使用Spring Boot,并且有以下实体定义(已删节):

package com.vw.asa.entities;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.PreUpdate;
import javax.validation.constraints.NotNull;

public abstract class CmsModel extends Model {
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "is_active")
    private short isActive;
    
    
    public short getIsActive() {
        return isActive;
    }
    
    public void setIsActive(short isActive) {
        this.isActive = isActive;
    }
    
    public void setIsActive(String isActive) {
        if (isActive.equals("true")) {
            this.isActive = IS_TRUE;
        } else if (isActive.equals("1")) {
            this.isActive = IS_TRUE;
        } else {
            this.isActive = IS_FALSE;
        }
    }
} 

然后我有几个模型扩展了这个“基础”模型,遵循以下结构:

package com.vw.asa.entities.cms;

import com.vw.asa.entities.CmsModel;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "cms_extra_questions", schema = "asa")
public class CmsExtraQuestions extends CmsModel {
        
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    
    ...
}

当我通过Hibernate查询初始化CmsExtraQuestions的实例时,如果在对象上调用setActive(true),它不会对该对象的成员属性产生影响。当我将CmsModel基类中的setter和getter复制到CmsExtraQuestions类中时,它可以正常工作。

$entity = new CmsExtraQuestions();
$entity->setActive(true);

为什么在调用扩展的setter时,这不会设置实例化对象的成员属性?如果这是正常的,是否有一种方法可以将这些属性和成员函数添加到基础模型中,使它们也能被继承?

英文:

I am using Spring Boot, and have the following Entity definitions (abridged):

package com.vw.asa.entities;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.PreUpdate;
import javax.validation.constraints.NotNull;

public abstract class CmsModel extends Model {
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "is_active")
    private short isActive;
    
    
    public short getIsActive() {
        return isActive;
    }
    
    public void setIsActive(short isActive) {
        this.isActive = isActive;
    }
    
    public void setIsActive(String isActive) {
        if (isActive.equals("true")) {
            this.isActive = IS_TRUE;
        } else if (isActive.equals("1")) {
            this.isActive = IS_TRUE;
        } else {
            this.isActive = IS_FALSE;
        }
    }
} 

Then I have several models which extend this 'base' model, following this flavor:

package com.vw.asa.entities.cms;

import com.vw.asa.entities.CmsModel;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

/**
 * @author Barry Chapman
 */
@Entity
@Table(name = "cms_extra_questions", schema = "asa")
public class CmsExtraQuestions extends CmsModel {
        
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    
    ...
}

When I initialize an instance of CmsExtraQuestions as a result of a hibernate query, if I call setActive(true) on the object, it has no effect on the members of that object. When I copy the setters and getters from the CmsModel base class into the CmsExtraQuestions class, it works fine.


$entity = new CmsExtraQuestions();
$entity->setActive(true);


Why does this not set the member properties of the instantiated object when calling the extended setter? If this is normal - is there a way to add these properties and member functions to the base model so that they can be inherited also?

答案1

得分: 0

以下是您要的翻译内容:

新手错误,我忘记给继承的模型CmsModel添加@MappedSuperClass注解。这允许JPA将该类的属性映射,就好像它们是在继承该基类的模型中定义的一样。

@MappedSuperclass
public abstract class CmsModel extends Model {
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "is_active")
英文:

Rookie mistake, I forgot to add @MappedSuperClass to the inherited model, CmsModel. That allows JPA to map the properties from that class as though they were defined in the model that was inheriting that base class.

@MappedSuperClass
public abstract class CmsModel extends Model {
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "is_active")

huangapple
  • 本文由 发表于 2020年9月25日 11:11:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64057262.html
匿名

发表评论

匿名网友

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

确定