Hi,
First thanks for the great library even though its still missing some major(at least for me) features like auto generetad id, string id, auto relation etc.
I tried to manually set id for each of my entities since there's no id coming from source. Then i get this exception java.lang.IllegalArgumentException: ID is higher or equal to internal ID sequence: 2 (vs. 2). Use ID 0 (zero) to insert new entities.
int id = 0;
for (VehicleType vehicleType : vehicleTypes) {
vehicleType.setId(id++);
}
I tried to use random but it didn't work either
Random random = new Random();
for (VehicleType vehicleType : vehicleTypes) {
vehicleType.setId(random.nextInt());
}
Can you tell me how can i achieve this?
You have to set assignable at the id annotation to true.
@Id(assignable = true)
What Fabian said.
Hi, thanks for the comments.
I did what you suggested but it seems whatever id i set for any entity the library ignore previously assigned id and set it to 0 itself.
This is my entity class
@Entity
public class VehicleType {
@Id(assignable = true)
private long id;
private String code;
private String name;
private int maxPassenger;
private int sellingCountLimit;
private int orderno;
@SerializedName("vehManufacturerDTOs") @Relation(idProperty = "id")
private List<Brand> brands;
//selection parameters
private int count;
/** Used to resolve relations */
@Internal
@Generated(hash = 1307364262)
transient BoxStore __boxStore;
@Generated(hash = 1213098646)
public VehicleType(long id, String code, String name, int maxPassenger,
int sellingCountLimit, int orderno, int count) {
this.id = id;
this.code = code;
this.name = name;
this.maxPassenger = maxPassenger;
this.sellingCountLimit = sellingCountLimit;
this.orderno = orderno;
this.count = count;
}
@Generated(hash = 1440934529)
public VehicleType() {
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
public int getMaxPassenger() {
return maxPassenger;
}
public int getSellingCountLimit() {
return sellingCountLimit;
}
public int getOrderno() {
return orderno;
}
public int getCount() {
return count;
}
public void setCode(String code) {
this.code = code;
}
public void setName(String name) {
this.name = name;
}
public void setMaxPassenger(int maxPassenger) {
this.maxPassenger = maxPassenger;
}
public void setSellingCountLimit(int sellingCountLimit) {
this.sellingCountLimit = sellingCountLimit;
}
public void setOrderno(int orderno) {
this.orderno = orderno;
}
public void setCount(int count) {
this.count = count;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override public String toString() {
return "VehicleType{" +
"id=" + id +
", code='" + code + '\'' +
", name='" + name + '\'' +
'}';
}
/**
* To-many relationship, resolved on first access (and after reset).
* Changes to to-many relations are not persisted, make changes to the target entity.
*/
@Generated(hash = 2118679597)
public List<Brand> getBrands() {
if (brands == null) {
final BoxStore boxStore = this.__boxStore;
if (boxStore == null) {
throw new DbDetachedException();
}
Box<Brand> box = boxStore.boxFor(Brand.class);
int targetTypeId = boxStore.getEntityTypeIdOrThrow(Brand.class);
List<Brand> brandsNew = box.getBacklinkEntities(targetTypeId,
Brand_.code, id);
synchronized (this) {
if (brands == null) {
brands = brandsNew;
}
}
}
return brands;
}
/** Resets a to-many relationship, making the next get call to query for a fresh result. */
@Generated(hash = 326440771)
public synchronized void resetBrands() {
brands = null;
}
/**
* Removes entity from its object box. Entity must attached to an entity context.
*/
@Generated(hash = 1980709258)
public void remove() {
if (__boxStore == null) {
throw new DbDetachedException();
}
__boxStore.boxFor(VehicleType.class).remove(this);
}
/**
* Puts the entity in its object box.
* Entity must attached to an entity context.
*/
@Generated(hash = 776532669)
public void put() {
if (__boxStore == null) {
throw new DbDetachedException();
}
__boxStore.boxFor(VehicleType.class).put(this);
}
}
int i = 1;
for (VehicleType vehicleType : vehicleTypes) {
vehicleType.setId(i++);
}
vehicleTypeBox.removeAll();
vehicleTypeBox.put(vehicleTypes);
And another interesting thing is when i try to reset database before adding anyting i get this io.objectbox.exception.DbDetachedException: Cannot perform this action on a detached entity. Ensure it was loaded by ObjectBox, or attach it manually.
vehicleTypeBox.removeAll();
int i = 1;
for (VehicleType vehicleType : vehicleTypes) {
vehicleType.setId(i++);
vehicleType.put();
}
Most helpful comment
You have to set assignable at the id annotation to true.
@Id(assignable = true)