Greendao 3, make users table with no duplicate user names - android

I ask this question after searching for hours for the solution to this problem and did not find it.
I have build a database with three columns: username, password and age.
I am able to isert new users and update them, but I want it to be impossible to insert two identical user names. I know how to do it in sqlite but in greendao it just does not go for me. thank you.
here is my UsersClass:
#Entity(nameInDb = "users")
public class Users{
#Id(autoincrement = true)
private Long id;
#NotNull
private String userName;
#NotNull
private String password;
#NotNull
private int age;
}

The #Unique annotation should work for you.
#Entity(nameInDb = "users")
public class Users{
#Id(autoincrement = true)
private Long id;
#Unique
private String userName;
#NotNull
private String password;
#NotNull
private int age;
}
If you need to customize further you can add options to the #Entity annotation
#Entity(
...
// Define indexes spanning multiple columns here.
indexes = {
#Index(value = "name DESC", unique = true)
},
...
)

Related

How to Join in ObjectBox

How can I join two classes in ObjectBox which have one two many relationship?
I have two tables as following:
#Entity
public class Animal {
#Id(assignable = true)
public long id;
private String name;
private boolean flying;
private boolean swimming;
private boolean walking;
private ToOne<Zoo> zoo;
.../*setters and getters*/
}
and:
#Entity
public class Zoo {
#Id
public long id;
private String name;
// a Zoo can have many Animals
#Backlink
ToMany<Animal> animals;
.../*setters and getters*/
}
How can I implement Join operation?
Since version 2.0.0 you can do something like:
val builder = box.query().equal(Zoo_.name, "The Big Zoo")
builder.link(Zoo_.animals).equal(Animal_.flying, true)
val flyingAnimals = builder.build().find()
This is called "links" and there is documentation on it with an additional example.

How to link Room database entities on android?

I am quite familiar with these kind of thing on Spring JPA but I can not make it work on Android. I post my code below so you can understand the question better. I have two classes and a third class that needs to contain objects of class 1 and 2. I know that the code for the third class is not correct, since Room does not support object references like that. If I save just the id-s of the objects as foreign keys I am not able to query after the results.
There must be some kind of nice solution for this problem. Thank you.
#Entity(tableName = "soup")
public class Soup {
#PrimaryKey(autoGenerate = true)
private long id;
private String name;
// getter and setters
}
#Entity(tableName = "main_course")
public class MainCourse {
#PrimaryKey(autoGenerate = true)
private long id;
private String name;
// getter and setters
}
#Entity(tableName = "menu")
public class Menu {
#PrimaryKey(autoGenerate = true)
private long id;
private Soup soupOptionOne;
private Soup soupOptionTwo;
private Soup soupOptionThree;
private MainCourse courseOptionOne;
private MainCourse courseOptionTwo;
private MainCourse courseOptionThree;
}

Not able to get query results using Room persistence

I am developing an Android app using the Room persistence library. I have a User and a Car entity
#Entity(tableName = "users")
public class User {
#PrimaryKey
#NonNull
private int id;
private String name;
public User(#NonNull int id, String name) {
this.id = id;
this.name = name;
}
}
and
#Entity(tableName = "cars", foreignKeys = #ForeignKey(parentColumns =
"id", childColumns = "userId", entity = User.class))
public class Car {
#PrimaryKey(autoGenerate = true)
private int id;
private int userId;
private String brand;
public Car(int userId, String brand) {
this.userId = userId;
this.brand = brand;
}
}
Also I have created a UserWithCar class as below:
public class UserWithCar {
#Embedded(prefix = "user_")
public User user;
#Embedded(prefix = "car_")
public Car car;
}
As you can see in the UserWithCar I use a prefix cause if I don't I get the following error:
Multiple fields have the same columnName: id. Field names: user > id,
car > id.
I want to get all the UserWithCar using the following query:
#Query("SELECT * FROM users JOIN cars ON users.id = cars.userId")
List<UserWithCar> getUserWithCar();
Using this query I get the following error:
The query returns some columns [id, name, id, userId, brand] which are
not use by com.roomdemo.data.models.UserWithCar. You can use
#ColumnInfo annotation on the fields to specify the mapping.
com.roomdemo.data.models.UserWithCar has some fields [user_id,
user_name, car_id, car_userId, car_brand] which are not returned by
the query. If they are not supposed to be read from the result, you
can mark them with #Ignore annotation. You can suppress this warning
by annotating the method with
#SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by
the query: id, name, id, userId, brand. Fields in
com.foodtec.roomdemo.data.models.UserWithCar: user_id, user_name,
car_id, car_userId, car_brand.
Can I have some help? Thanks!
Update
Using #Wizard help, I removed the prefix from the #Embeded and I added #ColumnInfo "uId" for the User's id and "cId for the Car's id in order to not have the same id field. By this way it works!
Columns returned by the query: id, name, id, userId, brand. Fields in
com.foodtec.roomdemo.data.models.UserWithCar: user_id, user_name,
car_id, car_userId, car_brand.
Error indicates that, columns returned by query is different from Pojo class. It should be the same. Alternatively you can map your Pojo variable to column name using #ColumnInfo annotation.
For example,
#PrimaryKey
#NonNull
#ColumnInfo(name = "user_id")
private int id;
This way, id will be mapped to user_id.
Change your query:
#Query("SELECT * FROM users JOIN cars ON users.id = cars.userId")
to specify the columns in the POJO class UserWithCar. The columns returned must match all the columns, and have the same column name as your POJO. You can use AS to change the column names in the query.
#Query("SELECT userId as userId , brand as brand FROM users JOIN cars ON users.id = cars.userId")
Alternatively, you can use #ColumnInfo to specify the column name mappings.

How to handle relationships in Room?

I'm testing Room persistence library. I have two entity classes:
#Entity
public class User {
#PrimaryKey
int id;
String name;
}
#Entity(foreignKeys = #ForeignKey(entity = User.class,
parentColumns = "id",
childColumns = "userId")
public class Pet {
#PrimaryKey
int id;
String name;
int userId;
}
Now I'd like to get list of Pet and in each Pet object keep actual reference to User according to his userId. So each time when the userId is changed, this reference also should be changed. Is it possible to do? Or maybe there is a better way to handle relation like this?
Actually, ROOM not recommended to do that. The reasons are in the reference link.
Maybe you could try it,
#Entity
public class User {
#PrimaryKey
int id;
String name;
#Embedded
public List<Pet> userPet;
}
public class Pet {
int id;
String name;
}
Understand why Room doesn't allow object references
A convenience annotation which can be used in a Pojo to automatically fetch relation entities.

Class with collection of itself ORMLite

How can i create one-to-many relation with the table itself in ormlite? I have a class that holds collection of itself. I don't know how to map this class.
#DatabaseTable(tableName = "USER")
public class User {
#DatabaseField(generatedId = true)
private long id;
#DatabaseField
private String username;
#ForeignCollectionField
private ForeignCollection<User> friends;
...
Thanks in advance.
I have a class that holds collection of itself. I don't know how to map this class.
I think the best way would be to define a UserFriend entity that has a reference to the owner User, friend User, and an id. Something like:
#DatabaseTable(tableName = "USERFRIEND")
public class UserFriend {
#DatabaseField(generatedId = true)
private long id;
// corresponds to the user who has the friend
#DatabaseField(foreign = true)
private User user;
// corresponds to the friend of the user
#DatabaseField(foreign = true)
private User friend;
...
Then your foreign collection becomes:
#ForeignCollectionField(foreignFieldName = 'user')
private ForeignCollection<UserFriend> friends;
Notice the foreignFieldName which has to point to the user so the user's friends can be found.

Categories

Resources