Giter Club home page Giter Club logo

Comments (33)

satyan avatar satyan commented on August 23, 2024 24

Here’s an example:

Say you have two classes Author and Book. An Author can have many books.
In this case, the Book class would contain the Author object. like this:

class Book{
    String name;
    Author author;
}

class Author{
    String name;

    public List<Book> getBooks(){
        return Book.find(Book.class, "author = ?", new String{author.getId()})
    }
}

You can achieve the one to many relationship like this.

from sugar.

 avatar commented on August 23, 2024 2

@satyan Thanks, your example helped me to understand how to implement oneToMany relationships ! I suggest you to maybe add this on your doc as it is not so clear how to implement it, I struggled a bit before I found this issue !

from sugar.

satyan avatar satyan commented on August 23, 2024 1

Many to many is slightly tricky.

You could create a LinkerClass similar to the link table in database, and use that to link the two objects.

eg:

Book
Author
AuthorBooks

where AuthorBooks would be

class AuthorBooks{
    Author author;
    Book book;
}

you can fetch in a similar style then:

List<AuthorBooks> authorBooks = AuthorBooks.find(AuthorBooks.class, “author = ?”, author.getId());

This would have the list of books.

It’s a round about approach.. but so far there’s no direct support for many to many relationship.

from sugar.

MisterSpicy avatar MisterSpicy commented on August 23, 2024 1

I don't understand the example - I'm using 1.4, but the code won't compile.

class Book{
    String name;
    Author author;
}

class Author{
    String name;

    public List<Book> getBooks(){
        return Book.find(Book.class, "author = ?", new String{author.getId()})
    }
}

The "author.getId()" always says "cannot resolve symbol". This example seems very incomplete - neither class extends SugarRecord, and author and getId() are not visible symbols within the Author class. Am I missing something here?

Also, if I switch the anonymous String to an anonymous string array

new String[]{author.getId()})

That returns a long, not a string. Syntax errors abound.

from sugar.

rdenadai avatar rdenadai commented on August 23, 2024 1

@MisterSpicy, please try this:

class Book extends SugarRecord<Book> {
    String name;
    Author author;
}

class Author extends SugarRecord<Author> {
    String name;

    public List<Book> getBooks(){
        return Book.find(Book.class, "author = ?", String.valueOf(this.getId()))
    }
}

from sugar.

EE-GSlomin avatar EE-GSlomin commented on August 23, 2024 1

I never would have thought that I needed to check to see if a highly regarded ORM supported one-to-many relationships. This is absolutely crucial. Is there ever going to be an update to support this?

from sugar.

evgenybozhko2 avatar evgenybozhko2 commented on August 23, 2024

I'm want understand how to make relationship one-to-many too. I try
private ArrayList images;
but this method doesn't work.
E/Sugar﹕ Class cannot be read from Sqlite3 database.

from sugar.

satyan avatar satyan commented on August 23, 2024

ArrayList or other collections aren't supported yet. You could define your relationship like this example: http://satyan.github.io/sugar/creation.html#why

from sugar.

heatherSnepenger avatar heatherSnepenger commented on August 23, 2024

I'm not sure based on the docs posted how to create a one to many relationship. Could you elaborate?

from sugar.

philliphartin avatar philliphartin commented on August 23, 2024

I've hit this roadblock also. Struggling with this one!

from sugar.

bassrock avatar bassrock commented on August 23, 2024

Any chance of this being supported soon? Or does anyone recommend any other ORM for Android?

from sugar.

SwampMobile avatar SwampMobile commented on August 23, 2024

Is this being worked on currently?

from sugar.

satyan avatar satyan commented on August 23, 2024

Sugar does not handle many to many relationship for you currently. You'd have to manage the relationship yourself using an approach suggested above #60 (comment)

It's not worked upon currently. Would love to hear some suggestions on this..

from sugar.

SwampMobile avatar SwampMobile commented on August 23, 2024

I'd definitely like to help if I can. Would you mind chatting a bit via email? I sent you one a few minutes ago before I stumbled upon this ticket.

from sugar.

rdenadai avatar rdenadai commented on August 23, 2024

+1 on the suggestion of @OlivierRevial to add the example on the documentation!

from sugar.

Wirsing84 avatar Wirsing84 commented on August 23, 2024

yes, +1 on adding that to the documentation! i was struggling with this for hours because i didnt know that Collections aren't supported

from sugar.

 avatar commented on August 23, 2024

What about one to many relationships? Are this being worked on?

from sugar.

LOG-TAG avatar LOG-TAG commented on August 23, 2024

An book can have many Authors how to insert the values in this condition ?

class Book{
    String name;
    Author author;
 public List<Author> getAuthors{
        return Author.find(Book.class, "name = ?", new String{author.getName()})
    }

}

class Author{
    String name;
...

}

above code is correct ?

To-Many lists Relations?
One more thing how to insert the list of (array/Arraylist) values to One To Many relationship? (like mentioned in GreenDAO http://greendao-orm.com/documentation/relations/)

code from AddNoteActivity of Samples

Tag tag = new Tag(tagBox.getText().toString());
                save(tag);
                save(new Note(10 + (int) (10 * Math.random()), titleBox.getText().toString(), descBox.getText().toString(), tag));

How to save if there are list of many tags to the note?

List<Tag> taglist=new ArrayList<Tag>();
taglist.add(new Tag("Tagname")):
.
.
.

Tag tag = new Tag(taglist);

save(tag);

  save(new Note(10 + (int) (10 * Math.random()), titleBox.getText().toString(), descBox.getText().toString(), tag));

this Will work for inserting many tags to to a single note? or any other ways to achieve this?

from sugar.

cuberob avatar cuberob commented on August 23, 2024

Ah just ran into this solution, but wanted to share what I ended up doing, maybe it helps someone out:

  • @ignore the actual list structure
  • Add a String "listJson" or the likes that keeps a json representation of the list
  • Make sure that listJson is in sync before calling save on the object
  • in you getList() method restore the list structure from the listJson String if the structure is empty/null

Not the most efficient/clean way but works for me for now (using small lists). Hoping to see proper list support soon though!

from sugar.

LOG-TAG avatar LOG-TAG commented on August 23, 2024

Thanks Cuberob for the Info, I finally did it in the same way by storing the string in json format! but json/gson parsing while retrieving inserting will effect performance! GeenDAO have mentioned some solutions for this.. Need try that Ref: #195

from sugar.

Ajibola avatar Ajibola commented on August 23, 2024

This is definitely much needed functionality. Is there a way to pass values to the constructor for sugar orm? I know it needs the default constructor. If possible then we can build up the many - many lists by passing the parent_id to the child_id class. So on querying the parent, in the constructor you do the find all by parent_id and build that object in the parent.

from sugar.

whoshuu avatar whoshuu commented on August 23, 2024

This is a critical feature and it will go into 1.4.0.

from sugar.

shakmak avatar shakmak commented on August 23, 2024

Is this feature present in 1.4 beta release ?

from sugar.

MisterSpicy avatar MisterSpicy commented on August 23, 2024

@rdenadai Hey, thanks for that, I was pretty tired and a little frustrated. I also found the whole set of examples in the project, which implements things like you have. Hopefully the docs get updated soon for 1.4!

from sugar.

LarryLGHao avatar LarryLGHao commented on August 23, 2024

sorry,who can give me one full example about one-to-many?I'm always not successThanks

from sugar.

multivoltage avatar multivoltage commented on August 23, 2024

Documentation did not help guys. I tried more than 1 time to implement simple Relation One to Many without succes. Android crash every time saying: android.database.sqlite.SQLiteException: no such column: travelSugar (code 1): , while compiling: SELECT * FROM PLACE_SUGAR WHERE travelSugar = ? where travelSugar is like "author" in @MisterSpicy above comment

from sugar.

epfisztner avatar epfisztner commented on August 23, 2024

@multivoltage In Sugar your field(travelSugar) will be represented as 'travel_sugar' in the database. So try like this YourClass.find(YourClass.class,"travel_sugar = ?", "somthing");

from sugar.

frpeters avatar frpeters commented on August 23, 2024

I don't understand how the example is supposed to work when you can't store objects as parameters in sugar, I mean the one with the Book and the Author. I get an error "no such column: author (code 1)" which is exactly what I expected when I saw the example, am I missing something?

If I could store objects as parameters in the database I wouldn't need to use a one to many solution.

EDIT: I found my problem, I had a table with something like this "nameTable", but sugarORM automatically changes those kinds of names to "name_table", so I had to search for it that way.

from sugar.

hy9be avatar hy9be commented on August 23, 2024

@satyan So we will have a direct support for many-to-many relationship in 2.0.0 for sure?

from sugar.

alejantab avatar alejantab commented on August 23, 2024

Hello.
I am a little confused.

class Book extends SugarRecord<Book> {
    String name;
    Author author;
}
class Author extends SugarRecord<Author> {
    String name;

    public List<Book> getBooks(){
        return Book.find(Book.class, "author = ?", String.valueOf(this.getId()))
    }
}

This code is really needed?

public List<Book> getBooks(){
        return Book.find(Book.class, "author = ?", String.valueOf(this.getId()))
    }

Thank you for helping me.

from sugar.

rasik1010 avatar rasik1010 commented on August 23, 2024

public String venueId;
public String venueName;
public String venueType;
public List < String > profileImages = new ArrayList < String > ();

this is my response model keys.. what should i do so that i can store that profileImages array in my database using SugarORM v1.5?
if any one can help please reply me , thank you

from sugar.

cooperkong avatar cooperkong commented on August 23, 2024

I'm having save issue with one to many relationship.

The example here http://satyan.github.io/sugar/creation.html#why is too vague and it also seems that @OneToMany annotation is removed in 1.5 release.

from sugar.

abhilash29 avatar abhilash29 commented on August 23, 2024

If someone can help. I have a similar kind of issue.. https://stackoverflow.com/questions/45389403/sugarorm-not-saving-object-inside-arraylist

from sugar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.