I have Class A contains List of Class B that contains List of Class C
so how I would achieve this in ObjectBox ? how do I store it?
in the pic below you can see that I have BasicInfo Class Contains Data class and so on....
I put #Entinty annotation on BasicInfo class and after generating the models class D doesn't show up in the
objectbox.g.dart file as it's on of BasicInfo properties
ModelEntity(
id: const IdUid(1, 3501283007979667013),
name: 'BasicInfo',
lastPropertyId: const IdUid(1, 2073579066569957521),
flags: 0,
properties: <ModelProperty>[
ModelProperty(
id: const IdUid(1, 2073579066569957521),
name: 'id',
type: 6,
flags: 1)
],
relations: <ModelRelation>[],
backlinks: <ModelBacklink>[])
If you're saying Class A contains a List of Class B, then their relationship isn't a subclass/superclass but it's a standard composition. I understand your definition looks somewhat like this:
class A {
List<B> bs;
}
class B {
List<C> cs;
}
class C {}
And in order to use it in ObjectBox, you can define them as relations, either a standalone ToMany relation, or a ToOne relation with a backlink - depends on what better describes your data, you can achieve the same with both.
1st alternative - using ToMany
ToMany relation stores the info in a separate "table" of IDs, like: A.id <--> B.id and another one between B.id <--> C.id.
#Entity()
class A {
int id;
final bs = ToMany<B>();
}
#Entity()
class B {
int id;
final cs = ToMany<C>;
}
#Entity()
class C {
int id;
}
2nd alternative - using ToOne with a backlink
ToOne relation can achieve the same in your case and the relatin info is actually stored on the object itself as a single ID, i.e. there's a hidden int B.classAId field that and you can get all Bs for a class A instance by looking for those Bs pointing to this A. This is done behind the scenes by a ToMany<> with a Backlink() annotation.
#Entity()
class A {
int id;
#Backlink()
final bs = ToMany<B>();
}
#Entity()
class B {
int id;
final a = ToOne<A>;
#Backlink()
final cs = ToMany<C>;
}
#Entity()
class C {
int id;
final b = ToOne<B>;
}
Related
Lets say I have this class from the documentation :
abstract class GeographicArea {
int get population;
String get name;
}
#freezed
class Example with _$Example {
const factory Example.person(String name, int age) = Person;
#Implements<GeographicArea>()
const factory Example.city(String name, int population) = City;
}
and I have an object of type Example ,
how can I check if the example implements Geographicarea and copy it with Interfacespecific properties?
var example = Example.city();
//How to check if this instance implements GeographicArea and call copyWith with a GeographicArea specific parameter?
example = example.copyWith(population: 20000);
we have
class A{
#id
int id;
List <B> bs;
}
where
class B{
String prId;
int pr;
}
i need a way to get list of B without duplication using prId.
so please suggest a way to get
[
{
prId: "18876",
pr: 1228876
},
{
prId: "123",
pr: 1228876
}
]
2 solutions (or more)
change List<B> bs; to Set<B> bs; and make sure class B has and equal/hashcode method base only on the prId
Since B is and embedded list of document (B does not have its on collection), there is no id created when you save A, you can generate a mongo id (ObjectId) on save of A by yourself like this
class B{
#Id
String prId;
int pr;
// get set
}
B b = new B();
b.setPrId(ObjectId.get().toString());
then by retreiving A all Bs are suppose to be unique
I have tried to put a full class path (com.xxxx.State.Finish) after != but not helping.
#Query("select c from CustomOrder c where c.dealer = :roleName and
c.nextManager = null and c.currentState != Finish")
List<CustomOrder> findOpenOrder(#Param("roleName") String roleName);
Entity:
#Getter
#Enumerated(EnumType.STRING)
CustomOrderEnums.State currentState;
Enum:
public enum State {
Open, Finish
}
#Query("select c from CustomOrder c where c.dealer = :roleName and
c.nextManager = null and c.currentState != com.xxx.FooEnum.Finish")
FooEnum has to be a top class not an inner one. If it has to be an inner class, use ' quoted string (haven't tried it without ').
#Query("select c from CustomOrder c where c.dealer = :roleName and
c.nextManager = null and c.currentState != 'Finish'")
I have just found that instead of using #Query it could be simply used as:
List<User> findIdByRoleRoleAndProvinceType(String role, ProvinceEnum.ProvinceType provinceType);
and this is entity User:
#Entity
public class User {
Role role; // entity has a String field role;
Province province; // entity has a ProvinceEnum.ProvinceType field type.
...
}
I have three entities, for example A, B, C. Entity A is parent for B, with inheritance type joined. Entity B aggregates entity C with ManyToOne relationship.
Structure looks like next:
#Entity
#Inheritance(strategy = InheritanceType.JOINED)
class A {
String str;
String cStr;
}
#Entity
class B extends A {
#ManyToOne
C c;
}
#Entity
class C {
String str;
}
I don't know if it is possible at all on entity level but, I need to link value of str from C to the filed A.cStr. How it should looks like: I create entity B with populated value of c, store it and value from C.str is populating into filed A.cStr. And when I fetch A from datebase I can see A.cStr with same value as C.str has.
No, it's not possible. All you need to do to get the C string from A is to implement a method and override it in B:
in A:
public String getCString() {
return null;
}
in B:
#Override
public String getCString() {
return c.getStr();
}
Of course, the instances of A that are not B instances won't have any CString.
I would like a 'RolesPlayed' entity with the following columns
user
role
department/project/group
All the three columns above constitute a composite primary key. I would like to know if defining a column to be one of department/project/group possible ? If yes, how ? Or do I need to break the entity into DepartmentRoles, GroupRoles and ProjectRoles.
Thanks.
You could use polymorphism with an abstract base class to do that.
#Entity
public class RolePlayed {
#ManyToOne
private User user;
#ManyToOne
private Role role;
#ManyToOne
private Body body;
...
}
#Entity
#Inheritance(strategy = InheritanceType.JOINED)
public abstract class Body {
...
}
#Entity
public class Department extends Body {
...
}
#Entity
public class Project extends Body {
...
}
#Entity
public class Group extends Body {
...
}
Check out the Polymorphism section in the Java Enterprise tutorial for a good overview.
Alternatively, you could also make the RolePlayed entity abstract, with DepartmentRole, GroupRole and ProjectRole implementations.