ORMLite foreign @DatabaseField with inverse mapping?

I'm implementing a small project and I'm wondering if ORMLite supports inverse mapping for @DatabaseMapping s. 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, ie 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?


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.

链接地址: http://www.djcxy.com/p/68668.html

上一篇: ORMLite抽象类的外部集合

下一篇: ORMLite国外@DatabaseField与逆映射?