1

I ran into the problem of adding my model class to the Room database. I've read about @Relation, @Embedded, but I can not figure out what to do with my model class.

Here my model class which comes from the server. I need to store this data

public class ResultsItem {

private String model;
private int id;
private int distanceLon;
private String brand;

//*PROMBLEM HERE*
private List<BidsItem> bids;

//*PROMBLEM HERE*
private User user;

//*PROMBLEM HERE*
private List<FotosItem> fotos;


// getters/setters...

}

FotosItem class

public class FotosItem {
private String imageUrl;
private int resultItemId;
private int id;

//getters/setters

}

BidsItem class

public class BidsItem {
private double distanceToAuto;
private boolean isWin;
private int sum;
private String currency;
private int id;

//getters/setters

}

I will be very grateful if someone tells me what I need to do, or at least in what direction to move

4
  • If you want to save a class in a databsae, you have to convert it into a byte array (apache commons lang has a tool for this, but you can create it on your own if you feel like it) and then you put that into the database using a blob field. Load it using getBlob and use apache lang to convert the byte stream back into the class Commented Jul 2, 2017 at 12:32
  • @LunarWatcher thx for quick comment, but i also need use LiveData<> for getting new data which was added to Room database. This data comes from server when paginate next portion of data Commented Jul 2, 2017 at 12:36
  • Um, well, we cannot really help you much, as we have no idea what BidsItem, User, or FotosItem are. We do not know the nature of the relationship between ResultsItem and these other things (e.g., does a User have one ResultsItem or N possible ResultItem instances)? Most likely, all of these are entities, and you need to determine where the foreign keys go, based on the type of relationship you have. Ignore @Relation. At most, User might be @Embedded, if there is a pure 1:1 relationship between ResultsItem and User. Commented Jul 2, 2017 at 12:55
  • I am added BidsItem and FotosItem code above Commented Jul 2, 2017 at 13:11

1 Answer 1

1

You can achieve it by doing some tweek in DAO class. Use Ignore Annotation for List.

public class ResultsItem {

private String model;
private int id;
private int distanceLon;
private String brand;

@Ignore
private List<BidsItem> bids;

private User user;

@Ignore
private List<FotosItem> fotos;


// getters/setters...

}

FotosItem class

public class FotosItem {
private String imageUrl;
private int resultItemId;
private int id;

//getters/setters

}

BidsItem class

public class BidsItem {
private double distanceToAuto;
private boolean isWin;
private int sum;
private String currency;
private int id;

//getters/setters

}

Now DAO Class

@Dao
public abstract class DBAccess {

@Insert
public abstract void insertBids(List<BidsItem> pets);

@Insert
public abstract void insertFotos(List<FotosItem> pets);

@Insert
public abstract void insertResultItem(ResultsItem resultItem);

public void insertResultWithBidsFotos(ResultsItem resultItem) {
        insertBids(resultItem.getBids());
        insertFotos(resultItem.getFotos());
        insertResultItem(resultItem);
    }

}

Same you can Implement for retrieve.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.