1

I'm implementing a small project and I'm wondering if ORMLite supports inverse mapping for @DatabaseMappings. What I am looking for is this similar to JPA's/Hibernates's inverse mapping. Following, hypothetical and rather silly example, a table BlogPost:

@DatabaseTable
public class BlogPost {
  @DatabaseField(foreign = true)
  private Author owner;
}

and the according Author class, not really that important:

@DatabaseTable public class Author { }

This results in the following SQL (just the relevant parts):

CREATE TABLE blogpost ( ... , owner_id INTEGER NOT NULL, ... ) CREATE TABLE author ( ... )

See how table blogpost now has a foreign key for author. However, I'd prefer it the other way around, i.e. author should have a blogpost_id foreign key. (I told you it was a silly example... ;).

With inverse mapping I could utilize cascades for deletes but I haven't found anything in the ORMlite docs about this. Is it not a feature or am I just missing something?

ilikeorangutans
  • 1,753
  • 1
  • 11
  • 14

1 Answers1

1

I'm a bit confused by the question but I thought I'd try to answer it anyway.

I don't understand how that would work @ilikeorangutans. I assume that there are multiple blogposts for a single author? So how could there be a single blogpost_id on the account channel? You could have a join table which contains an author_id and a blogpost_id but that just adds complexity.

If you take a look at this discussion and this webpage, you can see that Hibernate's inverse mapping is about who controls the relationship and is responsible for updating the rows. In both cases, BlogPost would have an author_id and not the other way around.

ORMLite does support the concept of "foreign collections" where you can add a BlogPost to the database by adding it to the collection in the Author object. For more information see the foreign collection documentation.

Sorry if I'm missing something.

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
  • The assumption is that there's only one author per post. I looked into foreign collections, but they don't do what I want. The whole idea is that I can use cascades on delete to delete depending rows automatically, for example if you delete the author, *all* his posts are deleted. Not sure if that makes sense – ilikeorangutans Oct 15 '11 at 18:55
  • Yeah, ORMLite doesn't support cascading. Not sure it ever will. It's hard to balance the "lite" and the features people want. – Gray Oct 15 '11 at 22:00
  • Related to http://stackoverflow.com/questions/6789075/deleting-using-ormlite-on-android – Gray Oct 16 '11 at 16:18