Отношения сущностей:< /p>
- Предустановка — Этап: 1 — нет
- Стадия — Повтор: 1 — нет
Класс должен быть либо @Entity, либо @DatabaseView.
Вот мои объекты:
Preset.java
Код: Выделить всё
package com.example.intervalapp.entity;
import androidx.annotation.NonNull; import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.PrimaryKey;
import java.time.LocalDateTime;
@Entity(tableName = "preset") public class Preset { @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "preset_id") private int id;
@ColumnInfo(name = "preset_name") private String name;
@ColumnInfo(name = "created_date") private LocalDateTime createdDate;
@ColumnInfo(name = "modified_date") private LocalDateTime modifiedDate;
public Preset(String name, LocalDateTime createdDate, LocalDateTime modifiedDate) {
this.name = name;
this.createdDate = createdDate;
this.modifiedDate = modifiedDate; }
public int getId() {
return id; }
public void setId(int id) {
this.id = id; }
public String getName() {
return name; }
public void setName(String name) {
this.name = name; }
public LocalDateTime getCreatedDate() {
return createdDate; }
public void setCreatedDate(LocalDateTime createdDate) {
this.createdDate = createdDate; }
public LocalDateTime getModifiedDate() {
return modifiedDate; }
public void setModifiedDate(LocalDateTime modifiedDate) {
this.modifiedDate = modifiedDate; }
@NonNull @Override public String toString() {
return "Preset{" +
"id=" + id +
", name='" + name + '\'' +
", createdDate=" + createdDate +
", modifiedDate=" + modifiedDate +
'}'; } }
Код: Выделить всё
package com.example.intervalapp.entity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey;
import com.example.intervalapp.enums.StageType;
import java.time.Duration;
@Entity(
tableName = "stage",
foreignKeys = @ForeignKey(
entity = Preset.class,
parentColumns = "preset_id",
childColumns = "preset_id",
onDelete = ForeignKey.CASCADE
),
indices = {@Index("preset_id")}
)
public class Stage {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "stage_id")
private int id;
@ColumnInfo(name = "preset_id")
private int presetId;
@ColumnInfo(name = "set_number")
private int setNumber;
@ColumnInfo(name = "stage_label")
private String label;
@ColumnInfo(name = "stage_type")
private String stageType; // E.g., "TIMER" or "REP"
@Nullable
@ColumnInfo(name = "duration")
private Duration duration; // Nullable for REP-based stages
@Nullable
@ColumnInfo(name = "num_reps")
private Integer numReps; // Nullable for TIMER-based stages
public Stage(int presetId, String label, int setNumber, String stageType, @Nullable Duration duration, @Nullable Integer numReps) {
this.presetId = presetId;
this.label = label;
this.setNumber = setNumber;
this.stageType = stageType;
// Assign the duration only for TIMER stages
if (StageType.TIME.toString().equals(stageType)) {
this.duration = duration;
this.numReps = null; // Ensure no reps value for TIMER stages
} else if (StageType.REPS.toString().equals(stageType)) {
this.duration = null; // Ensure no duration for REP stages
this.numReps = numReps;
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPresetId() {
return presetId;
}
public void setPresetId(int presetId) {
this.presetId = presetId;
}
public int getSetNumber() {
return setNumber;
}
public void setSetNumber(int setNumber) {
this.setNumber = setNumber;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getStageType() {
return stageType;
}
public void setStageType(String stageType) {
this.stageType = stageType;
}
@Nullable
public Duration getDuration() {
return duration;
}
public void setDuration(@Nullable Duration duration) {
this.duration = duration;
}
@Nullable
public Integer getNumReps() {
return numReps;
}
public void setNumReps(@Nullable Integer numReps) {
this.numReps = numReps;
}
@NonNull
@Override
public String toString() {
return "Stage{" +
"id=" + id +
", presetId=" + presetId +
", label='" + label + '\'' +
", setNumber=" + setNumber +
", stageType='" + stageType + '\'' +
", duration=" + duration +
", numReps=" + numReps +
'}';
}
}
Код: Выделить всё
package com.example.intervalapp.entity;
import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey;
@Entity(
tableName = "rep",
foreignKeys = @ForeignKey(
entity = Stage.class,
parentColumns = "stage_id",
childColumns = "stage_id",
onDelete = ForeignKey.CASCADE
),
indices = {@Index("stage_id")}
)
public class Rep {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "rep_id")
private int id;
@ColumnInfo(name = "stage_id")
private int stageId;
@ColumnInfo(name = "rep_duration")
private float repDuration;
public Rep(int stageId, float repDuration) {
this.stageId = stageId;
this.repDuration = repDuration;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStageId() {
return stageId;
}
public void setStageId(int stageId) {
this.stageId = stageId;
}
public float getRepDuration() {
return repDuration;
}
public void setRepDuration(int repDuration) {
this.repDuration = repDuration;
}
@NonNull
@Override
public String toString() {
return "Rep{" +
"id=" + id +
", stageId=" + stageId +
", repDuration=" + repDuration +
'}';
}
}
Код: Выделить всё
package com.example.intervalapp.relation;
import androidx.room.Embedded;
import androidx.room.Relation;
import com.example.intervalapp.entity.Preset;
import java.util.List;
public class PresetWithStages {
@Embedded
private Preset preset;
@Relation(
parentColumn = "preset_id",
entityColumn = "preset_id"
)
private List stages;
// Getters and Setters
public Preset getPreset() {
return preset;
}
public void setPreset(Preset preset) {
this.preset = preset;
}
public List getStages() {
return stages;
}
public void setStages(List stages) {
this.stages = stages;
}
}
Код: Выделить всё
package com.example.intervalapp.relation;
import androidx.room.Embedded;
import androidx.room.Relation;
import com.example.intervalapp.entity.Stage;
import com.example.intervalapp.entity.Rep;
import java.util.List;
public class StageWithReps {
@Embedded
private Stage stage;
@Relation(
parentColumn = "stage_id",
entityColumn = "stage_id"
)
private List reps;
// Getters and Setters
public Stage getStage() {
return stage;
}
public void setStage(Stage stage) {
this.stage = stage;
}
public List getReps() {
return reps;
}
public void setReps(List reps) {
this.reps = reps;
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... relational