英文:
Spring Boot: Prevent persisting field declared in superclass
问题
我正在使用Spring Boot创建一个Todo应用程序,我需要创建两个表:Task
和Todo
(Todo
继承自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
实体 - 一个扩展了带复选框的
Task
的Todo
实体 - 一个扩展了带描述字段的
Task
的SummaryTask
新实体
英文:
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 extendingTask
with the checkbox - A new
SummaryTask
extendingTask
with a description field
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论