2015-11-19 140 views
0

我有境界是生病了我的大腦:(添加新對象到關係領域

問題是,當我嘗試添加一個新的對象爲RealmList一個問題。該對象被插入,但它不與鏈接。關係

現在,我的數據庫中有多重關係:

用戶 1 - > MS 旅行 1 - > MS 車輛 1 - > MS

然後:

車輛 1 - > MS VehicleInfo

VehicleInfo 1 - > MS 溫度

當我嘗試插入一個新的對象的問題所在進入溫度等級。

我的課:

車輛:

@RealmClass 
    public class Vehicle extends RealmObject 
    { 
     private String state;     //R: Reservado | P: Listo para embarcar | E: Embarcado | P1: Pdte. confirmar medidas | P2: Tª no válida | R1: No embarca 
     private String locata  = ""; 
     private String customer  = ""; 
     private String customerCode = ""; 
     private String originPort = "";  //Origin port 
     private String destinyPort = "";  //Destiny port 
     private int  fp;      //Method pay (0 = CASH | 1 = CREDIT) 

     //Relationship 
     private Trip trip;        //Inverse 
     private RealmList<VehicleInfo> vehicleInfo = new RealmList<>(); //One-to-many 
     private RealmList<Observation> observations = new RealmList<>();; //One-to-many 
..... 
} 

VehicleInfo:

@RealmClass 
public class VehicleInfo extends RealmObject { 

    private String sv;     //Vehicle type 
    private String licensePlate = ""; //License 
    private String seal   = ""; //Seal 
    private String temperature = ""; //Temperature control 
    private String iv   = ""; //Ida/Vuelta 
    private String commodityCode = ""; 
    private int  tara   = 0; //TARA 
    private int  packages  = 0; //Bultos 
    private int  weight  = 0; 
    private double length  = 0.0; //Meters 
    private boolean flagFT;    //Flag Technical data 
    private boolean flagDua; 
    private boolean flagManifest; 
    private boolean flagTransport; 
    private boolean flagDangerCommodity; 

    //Relationship 
    private RealmList<Temperature> temperatures = new RealmList<>(); //One-to-many 
.... 
} 

我的代碼中加入新的溫度:

Temperature temp = new Temperature(); 
temp.setDate(appCommon.getCurrentTimeOrDate("DATE")); 
temp.setTime(appCommon.getCurrentTimeOrDate("TIME")); 
temp.setValue(Double.parseDouble(etTemp.getText().toString())); 

VehicleInfoPersistence.updateVehicleInfoTemperature(realm, vehicle.getLocata(), selectedUnitPosition, temp); 
updateRecycler(tempRecyclerAdapter, temp); 

方法找到並堅持境界:

public static VehicleInfo findVehicleInfoFromLocata(Realm realm, String locata, int position) { 
     RealmQuery<Vehicle> query = realm.where(Vehicle.class).equalTo("locata", locata); 
     Vehicle realmVehicle = query.findFirst(); 

     return realmVehicle.getVehicleInfo().get(position); 
    } 

public static void updateVehicleInfoTemperature(Realm realm, String locata, int position, Temperature temperature) { 

    Vehicle vehicle = VehiclePersistence.findVehicleFromLocata(realm, locata); 

realm.beginTransaction(); 
Temperature realmTemp = realm.copyToRealm(temperature); 
vehicle.getVehicleInfo().get(position).getTemperatures().add(realmTemp);  
realm.commitTransaction(); 

}

我怎麼說,對象在數據庫中創建的,但它不與車輛連接 - > vehicleInfo - >溫度。

我的代碼有什麼問題?

感謝提前:)

+0

你不缺少'realm.copyToRealmOrUpdate(車輛);'加入溫度之後? –

+0

不起作用。下一個異常失敗:java.lang。IllegalArgumentException:一個沒有@PrimaryKey的RealmObject無法更新:class io.realm.VehicleRealmProxy :( –

+0

「findVehicleFromLocata」是怎麼樣的? – beeender

回答

3

我解決我的問題:)

通過一些動力,當我嘗試添加新的對象爲RealmClass的子陣列境界失敗。

要解決此問題,我創建了一箇中間對象,然後將對象添加到此中間對象。

public static void updateVehicleInfoTemperature(Realm realm, String locata, int position, Temperature temperature) { 

    Vehicle vehicle = VehiclePersistence.findVehicleFromLocata(realm, locata); 

    realm.beginTransaction(); 

    VehicleInfo vInfo   = vehicle.getVehicleInfo().get(position); 
    VehicleInfo realmVehicleInfo = realm.copyToRealm(vInfo); 
    Temperature realmTemp  = realm.copyToRealm(temperature); 
    realmVehicleInfo.getTemperatures().add(realmTemp); 

    realm.commitTransaction(); 
} 

我希望能幫助別人:)