This is my database:
CREATE TABLE other (
id integer primary key autoincrement not null,
texto text
);
CREATE TABLE tabela (campoid INTEGER primary key autoincrement not null,
camponome text not null, chave int,
foreign key (chave) references other(id));
and this is my classes:
@DatabaseTable(tableName = "tabela")
public class Bean {
@DatabaseField(columnName = "campoid", generatedId = true)
private int _id;
@DatabaseField(columnName = "camponome")
private String nome;
@DatabaseField(foreign = true, columnName = "chave", canBeNull = false)
private Part chave;
}
and other classe mapped
@DatabaseTable(tableName = "other")
public class Part {
@DatabaseField(generatedId = true, columnName = "id")
private Integer id;
@DatabaseField(columnName = "texto")
private String texto;
}
but when i save an Bean object, the Object 'part' does not save too :(
Helper helper = OpenHelperManager.getHelper(this, Helper.class);
Dao<Bean, Integer> dao = helper.getDao(Bean.class);
Bean firstBean = new Bean();
firstBean.setNome("first be persisted");
Part part = new Part();
part.setTexto("ANY TEXT");
firstBean.setChave(part);
dao.create(firstBean);
in my log:
07-13 00:25:26.602: D/BaseMappedStatement(3796): insert data with statement 'INSERT INTO tabela
(camponome
,chave
) VALUES (?,?)' and 2 args, changed 1 rows
any idea?
Right. ORMLite does not by default persist sub-objects when you create an object. You can turn on the
foreignAutoCreate = true
flag which will do that for you however. See the javadocs forforeignAutoCreate
.Your
chave
field should be defined then as:If you want to do it by hand, you should:
You create the
Part
in the database first because you need the ID from thePart
which will be saved into your bean.