Persistence & DataBases - bukkit tutorial

Discussion in 'Resources' started by Sammy, Apr 10, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Dreadreaver

    I just implemented Persistance into my first plugin and I had the very same problem! For me using getDatabase().save(XXX); only worked if I was saving a new object, It won't update or replace an existing one. getDatanase().update(XXX); didnt work either so I first deleted the object and then saved a new one as that was always working. So try deleting "home" first like this:

    Code:
    if (home == null) {
     home = new Home();
    }else{
     getDatabase().delete(home);
    }             
    home.setPlayer(player.getName());
    home.setLocation(player.getLocation());
    getDatabase().save(home);
    
     
  2. Offline

    oliverw92

    It's weird because I think it used to work before the 1.5 bukkit updates. Someone must have broken something somewhher :p thanks for the delete idea, i'll give it a shot.

    So i tried deleting, but I'm now getting this error when trying to /home set with a pre-existing world:

    Code:
    Home home = getDatabase().find(Home.class).where().ieq("world", world.getName()).ieq("player", player.getName()).findUnique();
    
                        if (home != null)
                            getDatabase().delete(home);
    
                        home = new Home();
                        home.setPlayer(player.getName());
                        home.setLocation(player.getLocation());
                        getDatabase().save(home);
                        sendMessage(player, "`aYour home has been set in world `f" + world.getName());
    I tried it the way you had it, but if you already had a home set it deleted it and then didn't set another one

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  3. Offline

    Dreadreaver

    Code:
    BedRespawnLocation bedRespawn = plugin.getDatabase().find(BedRespawnLocation.class).where().ieq("playerName", player.getName()).findUnique();
    if (bedRespawn != null) {
         plugin.getDatabase().delete(bedRespawn);
    }
    bedRespawn = new BedRespawnLocation();
    bedRespawn.setPlayer(player);
    bedRespawn.setLocation(block.getLocation());
    plugin.getDatabase().save(bedRespawn);
    thats how I do it and it works just fine, can't help you much more, have to go sleeping now, waaay to tired ;)
     
  4. Offline

    oliverw92

    Ok well cheers for your help :)
     
  5. Offline

    Sammy

    Sorry but that just isn't true, I have many columns on my rows that need to be updated and I just need to open the row, use the setter and save it, you must be doing something wrong. :)
     
  6. Offline

    Dreadreaver

    what cb build are u runnig @Sammy ?
     
  7. Offline

    Sammy

    684CB and 654B, and Ebean didn't update for all I know.
     
  8. Offline

    oliverw92

    Sammy it is very true - try something like CB709 or 707 - I've tested a) my own plugin (which used to work) b) Dinnerbone's HomeBukkit and i see the same behaviour on both.
     
  9. Offline

    Sammy

    Mark it as a bug then, or wait until the next recommended build
     
  10. Offline

    Dreadreaver

    We were talking about "newer" builds - as in since Minecraft 1.5 ;)
    Believe me, my code is perfectly fine - as if calling .save() or .update() would be that hard lol.
    It's just not overwriting the old object.
     
  11. Offline

    oliverw92

    Maybe @Dinnerbone can give us some insight - he seems to be heading up the Persistence thing
     
  12. Offline

    oliverw92

    Sometimes when trying to getDatabase().delete(class) i get this error:

    Anyone got any idea what that is?
     
  13. Offline

    Sammy

    The way I learned was:
    Code:
    getDatabase().delete(MyClass.class, id);
     
    oliverw92 likes this.
  14. Offline

    oliverw92

    Ohhh my bad, thanks Sammy :)

    One last question, is Bukkit Persistence designed to work ok with remote MySQL databases? Users of my DataLog plugin are reporting errors when using remote databases, but no errors when using local databases.
     
  15. Offline

    Sammy

    I found that even if you change the folder of the database (using the bukkit.yml) the plugin still creates a folder in the /plugins/<yourPluginName> (only the folder not the .db)
    Have you tried to change that folder and see if those errors occur ?
     
  16. Offline

    oliverw92

    By local database i mean having the MySQL server on their local machine, rather than on a remote host. Not SQLite
     
  17. Offline

    Sammy

    Sorry my bad, I haven't tried using MySql...
     
  18. Thanks @Sammy this is a great tutorial :)

    I was wondering how fast is data i/o with this, I guess it's faster then flatfiles but by much?
     
  19. Offline

    alta189

    What needs to be in the ebeans.properties file?
     
  20. Just blank, it just needs to be there, at least I managed to remember one thing from this tutorial :p
     
  21. Offline

    alta189

    Where does the bukkit.yml go?

    EDIT: Nevermind

    I am having a problem. I posted it here. Please help!!! :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  22. Offline

    redspider

    hi, i have a question: how can i get an persistent object to save a object variable?
    e.g.:
    Code:
    @Entity
    @Table(name = "table_a")
    public class a
    {
          @Id
          private int id;
    
          @NotEmpty
          private b insertFunnyVarnameHere;
    
          ...getter and setter...
    }
    
    @ Entity
    @Table(name = "table_b")
    public class b
    {
          @Id
          private int b;
    
          ...getter and setter...
    }
    
    returns @NotEmpty not assignable to type class b
    if i omit it, it's just ignored

    @OneToOne does not work either because of the foreign key issue with sqllite

    thx in advance
     
  23. Offline

    CptSausage

    How can I save two different classes?
     
  24. Offline

    Sammy

    You just need to add this:
    Code:
        @Override
        public List<Class<?>> getDatabaseClasses() {
            List<Class<?>> list = new ArrayList<Class<?>>();
            list.add(HouseDataBase.class);
            list.add(PrisonDataBase.class);
            return list;
        }
    The setupDatabase() method just needs to have one class declaration
    To open u do just the same for both:
    Code:
    HouseDataBase db1=plugin.getDatabase().find(HouseDataBase.class).where().ieq("playerName",ply).findUnique();
    PrisonDataBase db2= plugin.getDatabase().find(PrisonDataBase.class).where().ieq("OwnerName", p).findUnique();
     
    CptSausage likes this.
  25. Offline

    axefan

    EbeanServer.save(Collection) starts failing consistently when a List contains around 7000 objects! This is a big problem since no exception is thrown and the return count indicates that all objects were written.

    I've created a simple plugin that demonstrates the problem:
    https://github.com/axefan/DatabaseErrorDemo

    Does anyone have a fix for this? Is this a bug or do I need to adjust some metadata somewhere?
     
  26. Offline

    MonsieurApple

    Thank. You. Very. Much.

    Here's a present for you:

    [cake]
     
  27. Offline

    Pandarr

    How would we set up relationships? Right now I have two tables "playerstore" and "blockstore". playerstore has a playerID key and blockstore utilizes the playerID as the foreign key back to playerstore. Currently I'm looking up the playerID in playerstore then looking up all rows in blockstore that have the playerID. I'm guessing there's a better way to do this. I see there is @onetomany and @manytoone. I'm just not sure how to link them.
     
  28. Offline

    oliverw92

    Does anyone know what would happen if you were to add a new field to the class you are persisting? Would the persistence system automatically update the actual table to have the new field?
     
  29. Offline

    FuzzeWuzze

    I too would like to know how to do this, i will have a total of 4 tables. I already have it diagram'd out how the Primary Keys/Foreign keys map, but not sure the easiest way to go about doing it. I was planning on doing what Pandarr posted but it seems wasteful...
     
  30. Offline

    Pandarr

    I think it's done close to this...

    Person.java (snippet)
    Code:
    @Entity
    @Table(name = "PERSON")
    public class Person {
    
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "personId")
      private int id;
    
      @OneToMany
      @JoinTable(name = "PersonAddress",
        joinColumns = {
          @JoinColumn(name="personId", unique = true)
        },
        inverseJoinColumns = {
          @JoinColumn(name="addressId")
        }
      )
      private Set<Address> addresses;
    }
    Address.java (snippet)
    Code:
    @Entity
    @Table(name = "ADDRESS")
    public class Address {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "addressId")
      private int id;
    }
    Next time I need it I'll try to use this to apply it... at worst it won't work.
     
Thread Status:
Not open for further replies.

Share This Page