Persistence & DataBases - bukkit tutorial

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

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

    akrieger

    I heard it throws nasty errors and calls your mother names if you do that. Actually changing the table seems beyond the scope of EBeans for an obscure reason.
     
  2. Offline

    oliverw92

    Yeah i found that out through trial and error :p I ended up just using getDatabase.createSqlUpdate() to update it.
     
  3. Offline

    Pandarr

    From what I can tell, the DDL call that makes the DB can't actually do onetomany, manytomany, etc.
     
  4. Offline

    Butzenbaer

    I tried it and it worked just fine for me using a mysql database:
    Code:
    String sql= "ALTER TABLE myTableName ADD COLUMN `myNewColumnName` varchar(255) NOT NULL;";
    SqlUpdate update = getDatabase().createSqlUpdate(sql);
    int rows = update.execute();
    
    i dont think that SqlUpdate is the right place for "ALTER TABLE" and "ADD COLUMN", but it worked for me.
    i dont need this for my plugin (atm), but im really new to ebean and ebean queries and wanted to test it.

    you can still publish an update.sql file for your users, which they can download and execute
     
  5. Offline

    WMisiedjan

    Can someone explain me how it works when I have a custom object like this Ranks but within that I have 2 other custom objects. and those custom objects are a list of those custom objects?
     
  6. Offline

    Sammy

    Well I only tried using sqlite, I don't really use MySql, because too many users don't like or can't use a Mysql server.
    Cheers
     
  7. Offline

    rmb938

    Is persistence any better then just storing data in a map? Does it use more or less RAM ?
     
  8. Offline

    croxis

    An example on how to delete entries would be helpful!
     
  9. im wondering why im getting this


    Code:
    2011-07-06 11:29:20 [SEVERE] Error occurred while enabling umtown v0.1 (Is it up to date?): null
    java.lang.NullPointerException
        at com.rob4001.rob4001.umtown.Umtown.onEnable(Umtown.java:48)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:126)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:857)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:264)
        at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:151)
        at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:136)
        at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:284)
        at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:271)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:148)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:335)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    and this

    Code:
    2011-07-06 11:29:16 [SEVERE] ebean.properties not found
    2011-07-06 11:29:16 [INFO] DataSourcePool [umtown] autoCommit[false] transIsolation[SERIALIZABLE] min[2] max[20]
    2011-07-06 11:29:16 [INFO] SubClassFactory parent ClassLoader [org.bukkit.plugin.java.PluginClassLoader]
    2011-07-06 11:29:17 [SEVERE] Bean property com.rob4001.rob4001.umtown.Towns.ownerName has  missing readMethod  missing writeMethod . Should it be transient?
    2011-07-06 11:29:17 [INFO] Entities enhanced[0] subclassed[2]
    i have the database: true in my .yml

    what else do i need??
     
  10. Offline

    croxis

    Not quite true. I just got bidirectional ManyToOne and OneToMany to work dandy. I am still having issues with getting ManyToMany to work however. The proper tables and columns are but the data is not. Not sure if I am setting up the annotations wrong, my code, or their code.
     
  11. Offline

    Lolmewn

    So, is this MySQL only or also SQLite ?
     
  12. Offline

    mjomble

    Any data you store in a map or another data structure in the memory will be wiped when the server shuts down / is restarted (or sometimes even if you just reload a plugin).
    Persistence, on the other hand, stores the data in files so you can get it back after a restart. But it's significantly more complex than just storing stuff in memory.
     
  13. Offline

    smickles

    I think it could be either. bukkit.yml config has something to do with it.
     
  14. Offline

    mascready

    I'm french ... : help :
    I don't understand the hard parts of the tutorial
     
  15. Offline

    Dark_Balor

    Not really, it's for every JDBC driver that are supported by Ebean. Since you can configure them in the bukkit.yml file.

    If you want to use it with another driver, you have to add the lib, and add the classpath.

    Just want to say thanks to Sammy it's really helpful.
    to delete you just have to do : getDatabase().delete(myPersistentObject);
    The idea of Persistent is to store object on database instead of flat file. If you don't know how to use a database, you don't need this.
    Well it's quite normal. The principle is : you design your architecture, when your architecture is correct, you use persistence to store the objects that will not change.

    If you want to add new field to them, you have to use the inheritance and create a new object that will be linked to the parent.

    It's too bad that we can't choose our JPA implement ... because I prefer Hibernate then EbeanServer ...
     
  16. Offline

    croxis

    I found some really great documentation for the annotations you can use: http://en.wikibooks.org/wiki/Java_Persistence

    The current ebeans implementation only supports the JPA 1.x standard, so if you see a feature that is JPA 2.0 it wont work.
     
  17. Offline

    ShadowDrakken

    SQLite has very limited support for ALTER TABLE, it's typically better to rebuild the table and move the data instead. And since you can't rename tables in SQLite, that means copying the old table into a temp table with a different name, deleting the original table, creating the new table using the name you want, then copying the data from the temp table to the new table before releasing the temp table.

    We really need a more solid tutorial for EBeans within Bukkit. I can't follow the OP's instructions, he just isn't very clear and I know he's trying so it's still appreciated. I'd like to see something that does more step by step coverage... starting with how to do raw SQL table creation, basic inserts, deletes and selects, nothing too fancy, then switching over and showing how to use the abstraction parts in the same order - create table, insert data, delete data, read data

    I've been pulling my hair trying to get this working for two days now, and the only think that I can seem to get working is raw SQL SELECT statements... not even able to get CREATE TABLE or INSERT to work :(

    Sorry for the double post, I have a lot to add here so wanted it somewhat separated from my first post.

    So, I finally got Sammy's method working with my own code through much trial and error and hair pulling and forehead smashing, and I'm understanding most of it. I'm not quite understanding how the NotEmpty is different than the NotNull. I know that NotNull must have some value, even if it's an empty string, so I'm assuming NotEmpty cannot be an empty string, but can it be null still? Anyway, on to the coding process as I understand it...

    Rather than starting with the setupDatabase() and getDatabaseClasses() functions, I would recommend starting with your custom class(es).

    The way I'm thinking of it now is your custom class with all the @ stuff in it is essentially your table definition. The private variables define your columns and their mutators act as your SELECT (getter) and INSERT/UPDATE (setter) for those columns.

    Each instance of the class becomes a row in the table (or gives you access to a matching existing row).

    getDatabaseClasses() tells EBeans which of your custom classes to treat as tables, pretty straightforward there

    setupDatabase() checks if the database has the custom class(es) setup as tables yet, and if not it creates them

    Then you can use the methods within getDatabase() to populate instances of the custom class in order to read and modify the data :)

    I know this isn't really much of a tutorial in itself, but I hope that it can assist with the creation of a more complete tutorial.

    Thanks Sammy and Mixcoatl

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

    miyoko

    I've been doing my own trial and error for the past few days, and I must say, the walls in my room have quite a few forehead dents now. If you're going to use Ebean Persistence in Bukkit, I recommend that you use Persistence Reimplemented (http://forums.bukkit.org/threads/bukkit-persistance-reimplemented-no-code-changes-required.24987/) because it fixes quite a few problems that Ebean has inside of Bukkit right now, namely, the fact that @ManyToOne/@OneToMany work in SQLite, which is very handy for object relations/mapping. So, I just hoping that this would help someone out :)
     
Thread Status:
Not open for further replies.

Share This Page