ahmetb / orman

lightweight and minimalist ORM for Java/Android. works with SQLite & MySQL. (not actively maintained)
Other
249 stars 47 forks source link

Issue creating one-to-many relationship with Id autoincrement #36

Closed Antpachon closed 13 years ago

Antpachon commented 13 years ago

Hi.

I'm trying to create a one-to-many relationship just like in the wiki example.

These are my class:

@Entity
public class CadenaDBO extends Model<CadenaDBO> {
@PrimaryKey(autoIncrement=true)
public long id;

private String uid;

private String medioRef;

private String title;

private String permalink;

       @OneToMany(toType = ProgramaDBO.class, onField = "channel")
    public EntityList<CadenaDBO, ProgramaDBO> programas = new EntityList(CadenaDBO.class, ProgramaDBO.class, this);
.
.

}

@Entity
public class ProgramaDBO extends Model<ProgramaDBO> {

@PrimaryKey(autoIncrement=true)
public long id;

private String uid;

private String name;

private String permalink;

private String language;

        @ManyToOne
private CadenaDBO channel;
}

Like in the wiki example but when I try to insert an element, first in CadenaDBO I'm getting a error column uid not unique.

I'm creating an Object and then I execute objectCadena.insert()

I can see the error in DDMS and the query is : INSTERT INTO cadena_dbo (permalink, uid, title, medioref) VALUES .....

Where is the long id column with autoincrement???

ahmetb commented 13 years ago

Normally, id (@PrimaryKey(autoIncrement=true)) column should not be in INSERT statement. Because, in mapping session, by default we defer generation of id to the database, and after .insert(), we assign generated id (by database) to the instance.

I think your problem might be caused from you might have previously created a database where uid is an @Index(unique=true) or @PrimaryKey(autoIncrement=true). Orman does not support schema changes, unfortunately.You should drop your database and let the Orman construct it from the beginning.

Therefore we recommend you to come up with a final schema because otherwise Orman will not be generating table modification queries for you and will throw errors.

Can you please delete your database and run the program again or execute program only once with the following line before starting the mapping session:

MappingSession.getConfiguration().setCreationPolicy(SchemaCreationPolicy.CREATE);

later on you can remove this code, it will use SchemaCreationPolicy.CREATE_IF_NOT_EXISTS.

If you want id field to be visible, you should set id generation policy in mapping configuration to manual.

I have copied your code on my box (changed private fields to public) , created a CadenaDAO and inserted it successfully. It is probably about your existing database schema does not match schema generated from current Java code and probably has nothing to do with one-to-many relationship.

Do any of these solve your issue?

Thanks.

Antpachon commented 13 years ago

Yes , that's it.

I had a Primary key from a old schema.

My mistake, I though that re-installing my app the schema will be deleted

Thank you!