房间每次只插入 1 行(替换以前的)
Room only inserting 1 row every time (replacing previous)
我已经设置了一些 FK 约束,它们很可能是罪魁祸首,但经过大量试验和错误后我无法弄清楚。
问题:每次我插入一所新大学时,它都会替换上一所。每次我在大学里插入一个学期,它就会取代旧的。每次我在学期中插入一门课程时,它都会取代旧的。
类如下:
User.class
@Entity
public class User
{
@PrimaryKey
@NotNull
private String userEmail;
private String userName;
public User(String userName, String userEmail)
{
this.userName = userName;
this.userEmail = userEmail;
}
}
University.class
@Entity(foreignKeys = @ForeignKey(entity = User.class, parentColumns = "userEmail", childColumns = "userEmail", onDelete = CASCADE, onUpdate = CASCADE),
indices = {@Index(value = {"userEmail"}, unique = true)})
public class University
{
@PrimaryKey
@NotNull
private String universityName;
private String userEmail;
public University(String universityName, String userEmail)
{
this.universityName = universityName;
this.userEmail = userEmail;
}
}
Semester.class
@Entity(foreignKeys = @ForeignKey(entity = University.class, parentColumns = "universityName", childColumns = "universityName", onDelete = CASCADE, onUpdate = CASCADE),
indices = {@Index(value = {"universityName"}, unique = true)})
public class Semester
{
@PrimaryKey(autoGenerate = true)
private Long semesterId;
private String name;
private String universityName;
public Semester(Long semesterId, String name, String universityName)
{
this.semesterId = semesterId;
this.name = name;
this.universityName = universityName;
}
@Ignore
public Semester(String name, String universityName)
{
this.name = name;
this.universityName = universityName;
}
}
Course.class
@Entity(foreignKeys = @ForeignKey(entity = Semester.class, parentColumns = "semesterId", childColumns = "semesterId", onDelete = CASCADE, onUpdate = CASCADE),
indices = {@Index(value = {"semesterId"}, unique = true)})
public class Course
{
@PrimaryKey(autoGenerate = true)
private Long courseId;
private String name;
private String code;
private Long semesterId;
public Course(Long courseId, String name, String code, Long semesterId)
{
this.courseId = courseId;
this.name = name;
this.code = code;
this.semesterId = semesterId;
}
@Ignore
public Course(String name, String code, Long semesterId)
{
this.name = name;
this.code = code;
this.semesterId = semesterId;
}
}
DAO
@Dao
public interface UserDao
{
@Query("SELECT * FROM User LIMIT 1")
User getUser();
@Query("SELECT * FROM University WHERE userEmail = :userEmail")
List<University> getUniversitiesByUser(String userEmail);
@Query("SELECT * FROM University WHERE universityName = :universityName LIMIT 1")
University getUniversityByName(String universityName);
@Query("SELECT * FROM Semester WHERE name = :semesterName AND universityName = :universityName LIMIT 1")
Semester getSemesterByNameAndUniversity(String semesterName, String universityName);
@Query("SELECT * FROM Semester WHERE universityName = :universityName")
List<Semester> getSemestersByUniversity(String universityName);
@Query("SELECT * FROM Course WHERE semesterId = :semesterId")
List<Course> getCoursesBySemester(long semesterId);
/**
* @param userEmail - The Email of the selected user
* @return - A list of all incidents submitted by the user
*/
@Query("SELECT * FROM Incident WHERE submitterEmail = :userEmail")
List<Incident> getAllIncidentsByUser(String userEmail);
// Updates
@Update(onConflict = REPLACE) void updateUser(User user);
@Update(onConflict = REPLACE) void updateUniversity(University university);
@Update(onConflict = REPLACE) void updateSemester(Semester semester);
// Inserts
@Insert(onConflict = REPLACE) void insertUser(User user);
@Insert(onConflict = REPLACE) void insertUniversity(University university);
@Insert(onConflict = REPLACE) void insertSemester(Semester semester);
@Insert(onConflict = REPLACE) void insertCourse(Course course);
// Deletes
@Delete void deleteUser(User user);
@Delete void deleteUniversity(University university);
@Delete void deleteSemester(Semester semester);
@Query("DELETE FROM User")
void deleteAllUsers();
}
通过在与 parent 的关系上使用唯一索引,您将 child 限制为 1-1 关系。
因此每学期只能有 1 门课程,每所大学只能有 1 个学期,每个用户邮箱只能有 1 所大学。
从索引中删除所有 unique = true
。
您也只能按名称拥有 1 所大学,因为名称是主键,因此一所大学只能有 1 位用户。我建议不要让用户作为大学的 parent。
要允许一个用户就读一所大学,而让多个用户就读同一所大学,您可以颠倒这种关系。
然而,您可能希望用户上多所大学,在这种情况下,您可以使用中级(associative/reference/mapping 加上其他名称)table。
我已经设置了一些 FK 约束,它们很可能是罪魁祸首,但经过大量试验和错误后我无法弄清楚。
问题:每次我插入一所新大学时,它都会替换上一所。每次我在大学里插入一个学期,它就会取代旧的。每次我在学期中插入一门课程时,它都会取代旧的。
类如下:
User.class
@Entity
public class User
{
@PrimaryKey
@NotNull
private String userEmail;
private String userName;
public User(String userName, String userEmail)
{
this.userName = userName;
this.userEmail = userEmail;
}
}
University.class
@Entity(foreignKeys = @ForeignKey(entity = User.class, parentColumns = "userEmail", childColumns = "userEmail", onDelete = CASCADE, onUpdate = CASCADE),
indices = {@Index(value = {"userEmail"}, unique = true)})
public class University
{
@PrimaryKey
@NotNull
private String universityName;
private String userEmail;
public University(String universityName, String userEmail)
{
this.universityName = universityName;
this.userEmail = userEmail;
}
}
Semester.class
@Entity(foreignKeys = @ForeignKey(entity = University.class, parentColumns = "universityName", childColumns = "universityName", onDelete = CASCADE, onUpdate = CASCADE),
indices = {@Index(value = {"universityName"}, unique = true)})
public class Semester
{
@PrimaryKey(autoGenerate = true)
private Long semesterId;
private String name;
private String universityName;
public Semester(Long semesterId, String name, String universityName)
{
this.semesterId = semesterId;
this.name = name;
this.universityName = universityName;
}
@Ignore
public Semester(String name, String universityName)
{
this.name = name;
this.universityName = universityName;
}
}
Course.class
@Entity(foreignKeys = @ForeignKey(entity = Semester.class, parentColumns = "semesterId", childColumns = "semesterId", onDelete = CASCADE, onUpdate = CASCADE),
indices = {@Index(value = {"semesterId"}, unique = true)})
public class Course
{
@PrimaryKey(autoGenerate = true)
private Long courseId;
private String name;
private String code;
private Long semesterId;
public Course(Long courseId, String name, String code, Long semesterId)
{
this.courseId = courseId;
this.name = name;
this.code = code;
this.semesterId = semesterId;
}
@Ignore
public Course(String name, String code, Long semesterId)
{
this.name = name;
this.code = code;
this.semesterId = semesterId;
}
}
DAO
@Dao
public interface UserDao
{
@Query("SELECT * FROM User LIMIT 1")
User getUser();
@Query("SELECT * FROM University WHERE userEmail = :userEmail")
List<University> getUniversitiesByUser(String userEmail);
@Query("SELECT * FROM University WHERE universityName = :universityName LIMIT 1")
University getUniversityByName(String universityName);
@Query("SELECT * FROM Semester WHERE name = :semesterName AND universityName = :universityName LIMIT 1")
Semester getSemesterByNameAndUniversity(String semesterName, String universityName);
@Query("SELECT * FROM Semester WHERE universityName = :universityName")
List<Semester> getSemestersByUniversity(String universityName);
@Query("SELECT * FROM Course WHERE semesterId = :semesterId")
List<Course> getCoursesBySemester(long semesterId);
/**
* @param userEmail - The Email of the selected user
* @return - A list of all incidents submitted by the user
*/
@Query("SELECT * FROM Incident WHERE submitterEmail = :userEmail")
List<Incident> getAllIncidentsByUser(String userEmail);
// Updates
@Update(onConflict = REPLACE) void updateUser(User user);
@Update(onConflict = REPLACE) void updateUniversity(University university);
@Update(onConflict = REPLACE) void updateSemester(Semester semester);
// Inserts
@Insert(onConflict = REPLACE) void insertUser(User user);
@Insert(onConflict = REPLACE) void insertUniversity(University university);
@Insert(onConflict = REPLACE) void insertSemester(Semester semester);
@Insert(onConflict = REPLACE) void insertCourse(Course course);
// Deletes
@Delete void deleteUser(User user);
@Delete void deleteUniversity(University university);
@Delete void deleteSemester(Semester semester);
@Query("DELETE FROM User")
void deleteAllUsers();
}
通过在与 parent 的关系上使用唯一索引,您将 child 限制为 1-1 关系。
因此每学期只能有 1 门课程,每所大学只能有 1 个学期,每个用户邮箱只能有 1 所大学。
从索引中删除所有 unique = true
。
您也只能按名称拥有 1 所大学,因为名称是主键,因此一所大学只能有 1 位用户。我建议不要让用户作为大学的 parent。
要允许一个用户就读一所大学,而让多个用户就读同一所大学,您可以颠倒这种关系。
然而,您可能希望用户上多所大学,在这种情况下,您可以使用中级(associative/reference/mapping 加上其他名称)table。