英文:
Get all correct values from query, except for id
问题
我创建了一个类似这样的实体(我删除了一些属性,但结构是一样的)。
@Entity(tableName = "circuits")
public class Circuit {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
public Circuit(int id, String name) {
this.id = id;
this.name = name;
}
@Ignore
public Circuit(String name) {
this.name = name;
}
@Ignore
public Circuit() {}
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;
}
}
<br>
@Entity(tableName = "circuit_sessions")
public class CircuitSession {
@PrimaryKey(autoGenerate = true)
private int id;
private int circuitId;
private int driverId;
public CircuitSession(int id, int circuitId, int driverId) {
this.id = id;
this.circuitId = circuitId;
this.driverId = driverId;
}
@Ignore
public CircuitSession(int circuitId, int driverId) {
this.circuitId = circuitId;
this.driverId = driverId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCircuitId() {
return circuitId;
}
public void setCircuitId(int circuitId) {
this.circuitId = circuitId;
}
public int getDriverId() {
return driverId;
}
public void setDriverId(int driverId) {
this.driverId = driverId;
}
}
然后,在我的DAO中有一个类似以下的查询:
@Query("SELECT * FROM circuits INNER JOIN circuit_sessions ON circuits.id = circuit_sessions.circuitId WHERE circuit_sessions.driverId = :driverId ORDER BY name ASC")
LiveData<List<Circuit>> getCircuitsWithOwnSessions(int driverId);
当我调用这个查询时,它正确返回一个Circuit对象,而且它就是我期望的那个电路。但是,当我使用该Circuit对象的getId()
函数时,它返回1而不是正确的电路id 2。所有其他属性都是正确的,并且是预期的值。只有id是错误的。
当我检查Android Studio中的应用程序检查并在那里运行查询时,它显示相同的电路,并显示正确的id,即2。
那么,在我的代码中调用查询时,可能导致错误的id是什么原因?有没有办法确保我总是获取数据库中保存的id呢?
英文:
I created an entity like this (I removed some properties, but the structure is the same).
@Entity(tableName = "circuits")
public class Circuit {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
public Circuit(int id, String name) {
this.id = id;
this.name = name;
}
@Ignore
public Circuit(String name) {
this.name = name;
}
@Ignore
public Circuit() {}
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;
}
}
<br>
@Entity(tableName = "circuit_sessions")
public class CircuitSession {
@PrimaryKey(autoGenerate = true)
private int id;
private int circuitId;
private int driverId;
public CircuitSession(int id, int circuitId, int driverId) {
this.id = id;
this.circuitId = circuitId;
this.driverId = driverId;
}
@Ignore
public CircuitSession(int circuitId, int driverId) {
this.circuitId = circuitId;
this.driverId = driverId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCircuitId() {
return circuitId;
}
public void setCircuitId(int circuitId) {
this.circuitId = circuitId;
}
public int getDriverId() {
return driverId;
}
public void setDriverId(int driverId) {
this.driverId = driverId;
}
}
Then I have a query like this in my DAO.
@Query("SELECT * FROM circuits INNER JOIN circuit_sessions ON circuits.id = circuit_sessions.circuitId WHERE circuit_sessions.driverId = :driverId ORDER BY name ASC")
LiveData<List<Circuit>> getCircuitsWithOwnSessions(int driverId);
When I call this query it correctly returns one Circuit object and it is the circuit, which I expect it to be. But when I use the getId()
function of that Circuit object it returns 1 instead of 2, which is the correct circuit id. All the other properties are correct and the expected values. Just the id is wrong.
When I check the App Inspection in Android Studio and run the query there, it shows the same circuit and it shows, the correct id, which is 2.
So, what could cause the wrong id when I call the query in my code? Is there a way to make sure, that I always get the id, which is saved in the database?
答案1
得分: 1
如果您没有使用2.5.0或更高版本的Room库,请尝试更改以使用它们(如果存在高于2.5.0的版本)。
存在有歧义的列名问题(如果输出中存在相同的列)。相同命名列的最后一个值将应用于较早的值。2.5.0应该修复了这个问题。
此问题只会在使用JOINS而不是方便的@Relation时出现(在使用@Relation时,JOIN通过子查询模拟)。
另一个解决方案是使用唯一的列名。
英文:
If you aren't using 2.5.0 or greater Room libraries try changing to use them (if there are any greater than 2.5.0).
There were issues with ambiguous column names (if the same column exists in the output). The value of the last of same named columns would be applied to the earlier values. 2.5.0 should fix this.
The issue would only appear if you used JOINS instead of the convenience @Relation (using this being an alternative, when using @Relation the JOIN is mimicked via subqueries).
Another solution is to use unique column names.
答案2
得分: 0
MikeT的回答指引我朝正确的方向前进。问题是,这两个表都有一个id列。我通过将查询更改为以下内容解决了这个问题:
@Query("SELECT circuits.* FROM circuits INNER JOIN circuit_sessions ON circuits.id = circuit_sessions.circuitId WHERE circuit_sessions.driverId = :driverId ORDER BY name ASC")
LiveData<List<Circuit>> getCircuitsWithOwnSessions(int driverId);
英文:
MikeT's answer pointed me in the right direction. The problem was, that both tables have an id column. I resolved that by changing the query to this:
@Query("SELECT circuits.* FROM circuits INNER JOIN circuit_sessions ON circuits.id = circuit_sessions.circuitId WHERE circuit_sessions.driverId = :driverId ORDER BY name ASC")
LiveData<List<Circuit>> getCircuitsWithOwnSessions(int driverId);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论