0

I have 2 entities, for example car and engine.

Each car has one engine (at least in this model :) ), but not every engine is built into a car. So I want the following table-structure:

+------------------------------------+
|CAR                                 |
+------------------------------------+
|id_car     INTEGER,PK,AUTOINCREMENT |
|id_engine  INTEGER,FK               |
+------------------------------------+

+------------------------------------+
|ENGINE                              |
+------------------------------------+
|id_engine  INTEGER,PK,AUTOINCREMENT |
+------------------------------------+

(Of course there are much more columns. In fact I have two entities with about 15 columns each. But let's keep it simple.)

This can be modeled like this:

engine = schema.addEntity("Engine");
Property pkEngine = engine.addLongProperty("id_engine").primaryKey().autoincrement().getProperty();

car = schema.addEntity("Car");
car.addLongProperty("id_car").primaryKey().autoincrement();
Property fkEngine = car.addLongProperty("id_engine").getProperty();
car.addToOne(engine, fkEngine);

But I want to be able to query car from engine and engine from car, with getters on the corresponding entity-objects. Therefor I added:

engine.addToOneWithoutProperty("Car", car, "id_car");

I then have all the getters I want, but my tables look like this:

    +------------------------------------+
|CAR                                 |
+------------------------------------+
|id_car     INTEGER,PK,AUTOINCREMENT |
|id_engine  INTEGER,FK               |
+------------------------------------+

+------------------------------------+
|ENGINE                              |
+------------------------------------+
|id_engine  INTEGER,PK,AUTOINCREMENT |
|id_car     INTEGER                  |
+------------------------------------+

If I use the following statments for insert the fk-columns are inconsisent, as CAR.id_engine is NULL, which leeds to exceptions using the getter engine.getCar().

Engine engine = new Engine();
mEngineDao.insert(engine);
Car car = new Car();
car.setEngine(engine);
engine.setCar(car);
mCarDao.insert(car);
engine.update();

Does anyone have suggestions how to build a One2One-Relation using greendao, so that there is only one foreign-key-column and getters in each entity?

---UPDATE---

It seems like addToOneWithoutProperty() doesn't work correctly. I can't find a section in the generated void bindValues(SQLiteStatement stmt, Car entity) that cares about the column id_engine.

AlexS
  • 5,295
  • 3
  • 38
  • 54

2 Answers2

1

Bidirectional one-to-one-mappings seem to be not supported at the moment.

Since I don't want redundant data in my database (to avoid inconsistencies) I decided to work with a simple to-one mapping for now. The other reason for this decicion is that I don't know if there will be ring-dependencies causing stack-overflows or other things.

AlexS
  • 5,295
  • 3
  • 38
  • 54
0

I suggest a car-engine table to hold the relationship:

+------------------------------------+
|CAR                                 |
+------------------------------------+
|id_car     INTEGER,PK,AUTOINCREMENT |
+------------------------------------+

+------------------------------------+
|ENGINE                              |
+------------------------------------+
|id_engine  INTEGER,PK,AUTOINCREMENT |
+------------------------------------+

+------------------------------------+
|CARENGINE                           |
+------------------------------------+
|id_car     INTEGER,FK               |
|id_engine  INTEGER,FK               |
+------------------------------------+

CARENGINE uses a compound key, no experience of greenDAO so do not know if it supports composite keys (add a PK to CARENGINE if not).

Daniel S. Fowler
  • 1,982
  • 15
  • 12
  • I didn' ask for Many2Many-relationships. (I already modeled some although composit keys are not supported at the moment.) My question is about how to model One2One. Please don't delete this answer, since I already commented a similar answer and I don't want to do this again :) – AlexS Sep 30 '13 at 08:58