Reputation: 303
I'm using room for my android database. here i have a many to many relationship between Customer and ServiceProvider class i used this tutorial to do it but after compiling i received this error:
/home/omid/IntelliJIDEAProjects/Accounting/app/build/generated/ap_generated_sources/debug/out/com/omidmsl/accounting/db/CustomerDAO_Impl.java:69: error: incompatible types: cannot be converted to int if (!_cursor.isNull(null)) {
and it is mentioning to this code CustomerDAO_Impl.java (compiler created this):
package com.omidmsl.accounting.db;
import android.database.Cursor;
import androidx.collection.ArrayMap;
import androidx.room.RoomDatabase;
import androidx.room.RoomSQLiteQuery;
import androidx.room.util.CursorUtil;
import androidx.room.util.DBUtil;
import androidx.room.util.StringUtil;
import com.omidmsl.accounting.models.Business;
import com.omidmsl.accounting.models.Customer;
import com.omidmsl.accounting.models.serviceprovider.CustomerOfServiceProvider;
import java.lang.Override;
import java.lang.String;
import java.lang.StringBuilder;
import java.lang.SuppressWarnings;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@SuppressWarnings({"unchecked", "deprecation"})
public final class CustomerDAO_Impl implements CustomerDAO {
private final RoomDatabase __db;
public CustomerDAO_Impl(RoomDatabase __db) {
this.__db = __db;
}
@Override
public List<Customer> getCustomers() {
final String _sql = "SELECT * FROM Customer";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
__db.assertNotSuspendingTransaction();
final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
try {
final int _cursorIndexOfCustomerName = CursorUtil.getColumnIndexOrThrow(_cursor, "customerName");
final int _cursorIndexOfPhoneNumber = CursorUtil.getColumnIndexOrThrow(_cursor, "phoneNumber");
final List<Customer> _result = new ArrayList<Customer>(_cursor.getCount());
while(_cursor.moveToNext()) {
final Customer _item;
_item = new Customer();
final String _tmpCustomerName;
_tmpCustomerName = _cursor.getString(_cursorIndexOfCustomerName);
_item.setCustomerName(_tmpCustomerName);
final String _tmpPhoneNumber;
_tmpPhoneNumber = _cursor.getString(_cursorIndexOfPhoneNumber);
_item.setPhoneNumber(_tmpPhoneNumber);
_result.add(_item);
}
return _result;
} finally {
_cursor.close();
_statement.release();
}
}
@Override
public List<CustomerOfServiceProvider> getCustomersOfServiceProvider() {
final String _sql = "SELECT * FROM Customer";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
__db.assertNotSuspendingTransaction();
__db.beginTransaction();
try {
final Cursor _cursor = DBUtil.query(__db, _statement, true, null);
try {
final int _cursorIndexOfPhoneNumber = CursorUtil.getColumnIndexOrThrow(_cursor, "phoneNumber");
final ArrayMap<String, ArrayList<Customer>> _collectionCustomers = new ArrayMap<String, ArrayList<Customer>>();
while (_cursor.moveToNext()) {
if (!_cursor.isNull(null)) {
final String _tmpKey = _cursor.getString(null);
ArrayList<Customer> _tmpCustomersCollection = _collectionCustomers.get(_tmpKey);
if (_tmpCustomersCollection == null) {
_tmpCustomersCollection = new ArrayList<Customer>();
_collectionCustomers.put(_tmpKey, _tmpCustomersCollection);
}
}
}
_cursor.moveToPosition(-1);
__fetchRelationshipCustomerAscomOmidmslAccountingModelsCustomer(_collectionCustomers);
final List<CustomerOfServiceProvider> _result = new ArrayList<CustomerOfServiceProvider>(_cursor.getCount());
while(_cursor.moveToNext()) {
final CustomerOfServiceProvider _item;
final Business _tmpServiceProvider;
if (! (_cursor.isNull(_cursorIndexOfPhoneNumber))) {
_tmpServiceProvider = new Business();
final String _tmpPhoneNumber;
_tmpPhoneNumber = _cursor.getString(_cursorIndexOfPhoneNumber);
_tmpServiceProvider.setPhoneNumber(_tmpPhoneNumber);
} else {
_tmpServiceProvider = null;
}
ArrayList<Customer> _tmpCustomersCollection_1 = null;
if (!_cursor.isNull(null)) {
final String _tmpKey_1 = _cursor.getString(null);
_tmpCustomersCollection_1 = _collectionCustomers.get(_tmpKey_1);
}
if (_tmpCustomersCollection_1 == null) {
_tmpCustomersCollection_1 = new ArrayList<Customer>();
}
_item = new CustomerOfServiceProvider();
_item.setServiceProvider(_tmpServiceProvider);
_item.setCustomers(_tmpCustomersCollection_1);
_result.add(_item);
}
__db.setTransactionSuccessful();
return _result;
} finally {
_cursor.close();
_statement.release();
}
} finally {
__db.endTransaction();
}
}
@Override
public List<Customer> getCustomer(final String cName) {
final String _sql = "SELECT * FROM Customer WHERE customerName = ?";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
int _argIndex = 1;
if (cName == null) {
_statement.bindNull(_argIndex);
} else {
_statement.bindString(_argIndex, cName);
}
__db.assertNotSuspendingTransaction();
final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
try {
final int _cursorIndexOfCustomerName = CursorUtil.getColumnIndexOrThrow(_cursor, "customerName");
final int _cursorIndexOfPhoneNumber = CursorUtil.getColumnIndexOrThrow(_cursor, "phoneNumber");
final List<Customer> _result = new ArrayList<Customer>(_cursor.getCount());
while(_cursor.moveToNext()) {
final Customer _item;
_item = new Customer();
final String _tmpCustomerName;
_tmpCustomerName = _cursor.getString(_cursorIndexOfCustomerName);
_item.setCustomerName(_tmpCustomerName);
final String _tmpPhoneNumber;
_tmpPhoneNumber = _cursor.getString(_cursorIndexOfPhoneNumber);
_item.setPhoneNumber(_tmpPhoneNumber);
_result.add(_item);
}
return _result;
} finally {
_cursor.close();
_statement.release();
}
}
private void __fetchRelationshipCustomerAscomOmidmslAccountingModelsCustomer(final ArrayMap<String, ArrayList<Customer>> _map) {
final Set<String> __mapKeySet = _map.keySet();
if (__mapKeySet.isEmpty()) {
return;
}
// check if the size is too big, if so divide;
if(_map.size() > RoomDatabase.MAX_BIND_PARAMETER_CNT) {
ArrayMap<String, ArrayList<Customer>> _tmpInnerMap = new ArrayMap<String, ArrayList<Customer>>(androidx.room.RoomDatabase.MAX_BIND_PARAMETER_CNT);
int _tmpIndex = 0;
int _mapIndex = 0;
final int _limit = _map.size();
while(_mapIndex < _limit) {
_tmpInnerMap.put(_map.keyAt(_mapIndex), _map.valueAt(_mapIndex));
_mapIndex++;
_tmpIndex++;
if(_tmpIndex == RoomDatabase.MAX_BIND_PARAMETER_CNT) {
__fetchRelationshipCustomerAscomOmidmslAccountingModelsCustomer(_tmpInnerMap);
_tmpInnerMap = new ArrayMap<String, ArrayList<Customer>>(RoomDatabase.MAX_BIND_PARAMETER_CNT);
_tmpIndex = 0;
}
}
if(_tmpIndex > 0) {
__fetchRelationshipCustomerAscomOmidmslAccountingModelsCustomer(_tmpInnerMap);
}
return;
}
StringBuilder _stringBuilder = StringUtil.newStringBuilder();
_stringBuilder.append("SELECT `Customer`.`customerName` AS `customerName`,`Customer`.`phoneNumber` AS `phoneNumber`,_junction.`businessName` FROM `Service` AS _junction INNER JOIN `Customer` ON (_junction.`customerName` = `Customer`.`customerName`) WHERE _junction.`businessName` IN (");
final int _inputSize = __mapKeySet.size();
StringUtil.appendPlaceholders(_stringBuilder, _inputSize);
_stringBuilder.append(")");
final String _sql = _stringBuilder.toString();
final int _argCount = 0 + _inputSize;
final RoomSQLiteQuery _stmt = RoomSQLiteQuery.acquire(_sql, _argCount);
int _argIndex = 1;
for (String _item : __mapKeySet) {
if (_item == null) {
_stmt.bindNull(_argIndex);
} else {
_stmt.bindString(_argIndex, _item);
}
_argIndex ++;
}
final Cursor _cursor = DBUtil.query(__db, _stmt, false, null);
try {
final int _itemKeyIndex = 2; // _junction.businessName;
if (_itemKeyIndex == -1) {
return;
}
final int _cursorIndexOfCustomerName = CursorUtil.getColumnIndex(_cursor, "customerName");
final int _cursorIndexOfPhoneNumber = CursorUtil.getColumnIndex(_cursor, "phoneNumber");
while(_cursor.moveToNext()) {
if (!_cursor.isNull(_itemKeyIndex)) {
final String _tmpKey = _cursor.getString(_itemKeyIndex);
ArrayList<Customer> _tmpRelation = _map.get(_tmpKey);
if (_tmpRelation != null) {
final Customer _item_1;
_item_1 = new Customer();
if (_cursorIndexOfCustomerName != -1) {
final String _tmpCustomerName;
_tmpCustomerName = _cursor.getString(_cursorIndexOfCustomerName);
_item_1.setCustomerName(_tmpCustomerName);
}
if (_cursorIndexOfPhoneNumber != -1) {
final String _tmpPhoneNumber;
_tmpPhoneNumber = _cursor.getString(_cursorIndexOfPhoneNumber);
_item_1.setPhoneNumber(_tmpPhoneNumber);
}
_tmpRelation.add(_item_1);
}
}
}
} finally {
_cursor.close();
}
}
}
and here is the rest of my codes:
Customer.java:
package com.omidmsl.accounting.models;
import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity(tableName = "Customer")
public class Customer {
public static final String KEY_NAME = "customer_name";
public static final String KEY_PHONE_NUMBER = "phone_number";
@NonNull
@PrimaryKey
private String customerName;
private String phoneNumber;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
CustomerDAO.java:
package com.omidmsl.accounting.db;
import androidx.room.Dao;
import androidx.room.Query;
import androidx.room.Transaction;
import com.omidmsl.accounting.models.Customer;
import com.omidmsl.accounting.models.serviceprovider.CustomerOfServiceProvider;
import java.util.List;
@Dao
public interface CustomerDAO {
@Query("SELECT * FROM Customer")
public List<Customer> getCustomers();
@Transaction
@Query("SELECT * FROM Customer")
public List<CustomerOfServiceProvider> getCustomersOfServiceProvider();
@Query("SELECT * FROM Customer WHERE customerName = :cName")
public List<Customer> getCustomer(String cName);
}
Business.java:
package com.omidmsl.accounting.models;
import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.*;
@Entity(tableName = "Business")
public class Business {
public static final String KEY_TYPE = "type";
public static final String KEY_NAME = "business_name";
public static final String KEY_PHONE_NUMBER = "phone_number";
private int type;
@PrimaryKey
@NonNull
private String businessName;
private String phoneNumber;
@Ignore
private long costs;
@Ignore
private long buys;
public Business(int type) {
this.type = type;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public Business() {
businessName = "";
}
public String getBusinessName() {
return businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public long getCosts() {
return costs;
}
public void setCosts(long costs) {
this.costs = costs;
}
public long getBuys() {
return buys;
}
public void setBuys(long buys) {
this.buys = buys;
}
}
ServiceProviderDAO.java:
package com.omidmsl.accounting.db;
import androidx.room.Dao;
import androidx.room.Query;
import androidx.room.Transaction;
import com.omidmsl.accounting.models.serviceprovider.ServiceProviderOfCustomer;
import java.util.List;
@Dao
public interface ServiceProviderDAO {
@Transaction
@Query("SELECT * FROM Business")
public List<ServiceProviderOfCustomer> getServiceProvidersOfCustomer();
}
CustomersOfServiceProvider.java:
package com.omidmsl.accounting.models.serviceprovider;
import androidx.room.*;
import com.omidmsl.accounting.models.Business;
import com.omidmsl.accounting.models.Customer;
import java.util.List;
public class CustomerOfServiceProvider {
@Embedded
private Business serviceProvider;
@Relation(
parentColumn = "businessName",
entityColumn = "customerName",
associateBy = @Junction(Service.class)
)
private List<Customer> customers;
public Business getServiceProvider() {
return serviceProvider;
}
public void setServiceProvider(Business serviceProvider) {
this.serviceProvider = serviceProvider;
}
public List<Customer> getCustomers() {
return customers;
}
public void setCustomers(List<Customer> customers) {
this.customers = customers;
}
}
please help me
Upvotes: 6
Views: 7704
Reputation: 54801
If you previously had code in the style:
import androidx.room.OnConflictStrategy.REPLACE
@Update(onConflict = REPLACE)
This will no longer work, and can be fixed by the addition of Companion
, however, the style the API designers expected you to use would be:
import androidx.room.OnConflictStrategy
@Update(onConflict = OnConflictStrategy.REPLACE)
Which requires no fix.
Upvotes: 1
Reputation: 14622
Like @solamour I also upgraded to room 2.5.x but my problem was this line:
@ColumnInfo(name = "elapsedTime", typeAffinity = INTEGER, defaultValue = "0")
And I changed it to:
@ColumnInfo(name = "elapsedTime", typeAffinity = ColumnInfo.Companion.INTEGER, defaultValue = "0")
Upvotes: 0
Reputation: 3224
This has nothing to do with the problem described by the original poster, but if you got here via Google search, here is something that might be relevant.
I recently updated Room from 2.4.x to 2.5.x and encountered error: incompatible types: <null> cannot be converted to int
.
I had to replace androidx.room.OnConflictStrategy.REPLACE
with androidx.room.OnConflictStrategy.Companion.REPLACE
to fix.
Upvotes: 15
Reputation: 303
thank you @ianhanniballake. problem solved!.
in CustomerDAO file i found this problem:
@Transaction
@Query("SELECT * FROM Customer")
public List<CustomerOfServiceProvider> getCustomersOfServiceProvider();
type of this list must be ServiceProvidersOfCustomer:
@Transaction
@Query("SELECT * FROM Customer")
public List<ServiceProviderOfCustomer> getServiceProvidersOfCustomer();
Upvotes: 0