question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OneToOne simple relationship

See original GitHub issue

Hello.

I spent lot of time finding the way to implement simple and elegant OnToOne relation for two models. But the only way to make it possible I described here https://github.com/Raizlabs/DBFlow/issues/216#issuecomment-120721936

My models actually are DTOs which means I have a pre-defined structure. Fields annotated with @Expose are handled by Retrofit serializers (Gson). Each User object has an UserProfile. UserProfile can not live without referenced User.

User.java

@Table(tableName = "Users", databaseName = "mydb")
@ModelContainer
public class User extends BaseModel {

    @Column(name = "id")
    @PrimaryKey(autoincrement = true)
    long id;

    @Expose
    @SerializedName("id")
    @Column(name = "userId")
    String userId;

    @Expose
    UserProfile profile;
}

UserProfile.java

@Table(tableName = "UserProfiles", databaseName = "mydb")
public class UserProfile extends BaseModel {

    @Column(name = "id")
    @PrimaryKey(autoincrement = true)
    long id;

    @Expose
    @Column(name = "firstName")
    String firstName;

    @Expose
    @Column(name = "lastName")
    String lastName;
}

The simple way means that I do not need to save or load UserProfile separately. I want to load a user only. The expected result should be:

UserProfile profile = new UserProfile();
User user = new User();
user.setUserId("133456789");
user.setUserProfile(profile);
user.save(); // saves also a userProfile

// load
User user = new Select().from(User.class).querySingle();
user.getUserProfile(); // not null, loaded from DB

// modify
user.getUserProfile().setFirstName("john");
user.save(); // saves also a userProfile

// delete
user.delete(); // deletes user and associated userProfile

Any suggestions?

With respect, Gennadi Kudrjavtsev

PS. I tried suggested method by combining OneToMany and ForeignKey but this caused circular queries (https://github.com/Raizlabs/DBFlow/issues/322#issuecomment-120845545)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
FarhanAhmadcommented, Dec 15, 2016

a little more info regarding putting that ‘@ForeignKey’ would be really helpful in removing the confusion of all this One to One relation in dbFlow. And a use case example just like ydanneg has described above. Thanx

0reactions
agrosnercommented, Feb 29, 2016

solution would have been to use a @ForeignKey

Read more comments on GitHub >

github_iconTop Results From Across the Web

One-to-One Relationship in JPA - Baeldung
Learn three different ways to maintain a one-to-one relationship with JPA. ... We'll need a basic understanding of the Hibernate framework, ...
Read more >
The best way to map a @OneToOne relationship with JPA and ...
The best way to map a @OneToOne relationship is to use @MapsId . This way, you don't even need a bidirectional association since...
Read more >
OneToOne relationship - Atlassian Developer
In Active Objects a one to one relationship is defined thanks to the net.java.ao. ... Then setting the relation ship is as simple...
Read more >
18.4. Creating a One-to-One Relationship
We defined a one-to-one relationship between two objects, A and B, as occurring when an object of type A can be related to...
Read more >
JPA / Hibernate - Mapping One-to-One relationships - Nullbeans
Configuring One-To-One relationships. In our example, we will be mapping the relationship between a Customer entity and a CustomerAddress entity ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found