Question

I'm using ORMLITE with 2 Objects:

User:

@DatabaseTable(tableName = "user")
public class    User {

    @DatabaseField(columnName = "id", generatedId = true, index = true)
    public int      id;

    @ForeignCollectionField(eager = true, foreignFieldName = "user")
    public Collection<File> files;
}

File:

@DatabaseTable(tableName = "file")
public class    File {

    @DatabaseField(columnName = "id", generatedId = true, index = true)
    public int       id;

    @DatabaseField(columnName = "user", foreign = true) <- disable the read of this field
    public User          user;

    @DatabaseField(columnName = "filename")
    public String        filename;

}

It's a One to Many, a "User" can have many "File". For accessing to the "Files", I always use the object "User" like that :

List<User>      users;
Dao<User, ?>    daoUser;

users = daoUser.queryForAll();
System.out.println(users);

System.out.println(users.files.get(0));

The object "User" inside the collection "File" is instantiated. I don't want !

I would like "User.files.get(0).user" to be null.

How can I disable the read of a Field on ORMLite ?

Was it helpful?

Solution

The object "User" inside the collection "File" is instantiated. I don't want !

How can I disable the read of a Field on ORMLite ?

You have a couple different ways of solving this.

  1. You can have a int user_id field instead of a User field and then have a non-database field User that you hygrade from the database on your own from the user_id.

  2. Another thing you can do is to use the queryBuilder.selectColumns(...) to select all of the columns except the user_id column. If you don't return the user_id from the select then it won't instantiate a User instance. It is important to note that if you mark the user field as being a @DatabaseField, ORMLite will change it to user_id or something.

  3. You could use a dao.queryForRaw(...) method and a do the class translation yourself using your own RawRowMapper that ignores the user_id column in the results.

As an aside, may I ask why you don't want the User instantiated? Is this a memory issue?

OTHER TIPS

I think you have error in your class definition, following classes make one-to-many connection.

Check the ormlite.com documentation, especially eager and foreignAutoRefresh parts.

User:

@DatabaseTable(tableName = "user")
public class    User {

    @DatabaseField(columnName = "id", generatedId = true, index = true)
    public int      id;

    @ForeignCollectionField(eager = false)
    public Collection<File> files;
}

File:

@DatabaseTable(tableName = "file")
public class    File {

    @DatabaseField(columnName = "id", generatedId = true, index = true)
    public int       id;

    @DatabaseField(columnName = "id", foreign = true, foreignAutoRefresh = false)
    public User          user;

    @DatabaseField(columnName = "filename")
    public String        filename;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top