在运行时改变鉴别器值?

时间:2018-03-14 10:58:43

标签: java jpa inheritance

我正在使用具有jpa实体持久性支持的层次对象模型。

这里有班级模型:

用户类:

@Entity
@Table(name = "user", catalog = "users")
@NamedQueries({
  @NamedQuery(...
})
@Inheritance(strategy= InheritanceType.JOINED)
@DiscriminatorColumn(name = "apType", discriminatorType = 
DiscriminatorType.STRING, length = 255)
//@DiscriminatorValue("user")
public class User implements Serializable {

    @Transient
    protected PropertyChangeSupport changeSupport = new 
    PropertyChangeSupport(this);

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "apType")
    private String apType;

    @Basic(optional = false)
    @Column(name = "name")
    private String name;

    public UsuariDeClaus() {
        this.setApType("user");

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        Long oldId = this.id;
        this.id = id;
        changeSupport.firePropertyChange("id", oldId, id);
    }

    public String getApType() {
        return apType;
    }

    public void setApType(String apType) {
        this.apType = apType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        String oldName = this.name;
        this.name = name;
        changeSupport.firePropertyChange("name", oldName, name);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener 
    listener) {
        changeSupport.removePropertyChangeListener(listener);
    }

}


ApplicationUser class :

@Entity
@Table(name = "applicationuser", catalog = "usuweb793")
@NamedQueries({
  @NamedQuery(...    
})

public class ApplicationUser extends Users{


    @Basic(optional = false)

    @Column(name = "nickname", unique=true)
    private String nickname;

    @Basic(optional = false)
    @Column(name = "password")
    private String password;

    public ApplicationUser() {
        super.setApType("ApplicationUser");
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        String oldPassword = this.password;
        this.password = password;
        changeSupport.firePropertyChange("password", oldPassword, password);
    }

    public String getNickname() {
        return user;
    }

    public void setNickname(String nickname) {
        String oldNickname = this.nickname;
        this.nickname = nickname;
        changeSupport.firePropertyChange("nickname", oldNickname, nickname);
    }

}

and administratorUser class:

@Entity
@Table(name = "administratoruser", catalog = "usuweb793")
@NamedQueries({
  @NamedQuery(...})

public class AdministratorUser extends AplicationUser{

    public AdministratorUser() {
        super.setApType("administratoruser");
    }

}

实体管理器在mysql数据库4上创建表: sequence,user,aplicationuser和administratoruser。

用户表:

  • id name aptype
  • 1 aaa用户
  • 2 bbb aplicationuser
  • 3 ccc administratoruser

aplicationuser table:

  • id昵称密码
  • 2 bbbxxxx bbbyyyyy
  • 3 cccxxxx cccyyyyy

administratoruser table:

  • ID
  • 3

是否可以在不删除对象的情况下更改用户权限并创建新对象? (我希望id不要改变) 类似的东西:

User user = em.find(1); 新的AplicattionUser(用户);

用户表:

  • id name aptype
  • 1 aaa aplicationuser
  • 2 bbb aplicationuser
  • 3 ccc administratoruser

aplicationuser table:

  • id昵称密码
  • 1 aaaxxx aaayyyyy
  • 2 bbbxxxx bbbyyyyy
  • 3 cccxxxx cccyyyyy

administratoruser table:

  • ID
  • 3

1 个答案:

答案 0 :(得分:0)

根据我的理解和我的POV,如果ApplicationAdministrator只是User的角色,您可以在之间切换....删除无效,制作一个{ {1}}实体和User实体 - 具有相应的表格,并根据您的业务案例加入具有相应联接的两个实体 - RoleOneToOne - ....如果你不能改变数据库/代码......那就是"脏"您可以尝试的解决方案(以及"脏"再次)...制作本机批量更新语句以进行所需的更改,但请注意以下事项:

1-我不确定它是否有效,你需要尝试一下

2-您必须确保在运行

时,任何持久性上下文都不管理该实体

3-您负责在更新后刷新实体/持久性上下文/缓存