Spring Boot:防止持久化在超类中声明的字段

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

Spring Boot: Prevent persisting field declared in superclass

问题

我正在使用Spring Boot创建一个Todo应用程序,我需要创建两个表:TaskTodoTodo继承自Task)。

Task表中,有一个名为description的字段,我希望阻止该列在Todo表中创建。

我该如何实现?

Task(父类):

package com.example.todo.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Task {

    @Id
    private long id;
    private String name;
    private String description;
}

Todo(子类):

package com.example.todo.model;

import javax.persistence.Entity;
import javax.persistence.Transient;

@Entity
public class Todo extends Task {

    private boolean isChecked;
}
英文:

I am creating a Todo app in Spring Boot and I need to create two tables: Task and Todo(Todo extends Task).

In Task table is a field called description and I would like to prevent that column to be created in Todo table.

How can I do it?

Task(parent):

package com.example.todo.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Task {

    @Id
    private long id;
    private String name;
    private String description;
}

Todo(child):

package com.example.todo.model;

import javax.persistence.Entity;
import javax.persistence.Transient;

@Entity
public class Todo extends Task {

    private boolean isChecked;
}

答案1

得分: 5

我建议您清理一下设计,因为具体类从其他具体类继承通常是一种代码异味。解决这个问题的正确方法是将这两个类的共同部分提取到一个(抽象的)超类中,然后将特定字段添加到具体继承类中:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Completable {

    @Id
    private long id;
    private String name;
}
@Entity
public class Task extends Completable {

    private String description;
}
@Entity
public class Todo extends Completable {

    private boolean isChecked;
}

这样,您可以将行为分组到其所属的类中,而无需确保一个东西包含描述,而实际上不应该包含描述。

英文:

I would suggest you clean up your design because concrete classes inheriting from other concrete classes is (often) a code smell. The proper solution to this is to factor out the common parts of both classes into a (abstract) super class and then add the specific fields to the concrete inheriting classes:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Completable {

    @Id
    private long id;
    private String name;
}
@Entity
public class Task extends Completable {

    private String description;
}
@Entity
public class Todo extends Completable {

    private boolean isChecked;
}

so you have the behaviour grouped in the classes where it belongs and don't have to make sure that one thing contains a description while it shouldn't.

答案2

得分: 2

你想要的并不容易实现。但是你可能正在以错误的方式尝试解决一个问题。

从我所阅读的内容来看,你有一个Task实体,它有两种不同的类型:

  • 一种带有复选框,表示其完成情况
  • 一种带有额外描述

如果是这种情况,你可能希望以相同的方式对这些类进行建模。因此有:

  • 一个没有描述的Task实体
  • 一个扩展了带复选框的TaskTodo实体
  • 一个扩展了带描述字段的TaskSummaryTask新实体
英文:

What you want cannot be done easily. But you might be trying to solve an issue in the wrong way.

From what I am reading you have a Task entity with has two separate types:

  • one with a checkbox indicating its completion
  • one with an additional description

If this is the case you might want to model the classes the same way. Thus having:

  • A Task entity without the description
  • A Todo entity extending Task with the checkbox
  • A new SummaryTask extending Task with a description field

huangapple
  • 本文由 发表于 2020年9月1日 21:39:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63688888.html
匿名

发表评论

匿名网友

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

确定