Solved Ebean constraint errors

Discussion in 'Spigot Plugin Development' started by B1acksheep, Jul 31, 2015.

  1. Hey,
    I'm writing a simple Plugin to log all Deaths (Location, Victim, Killer)

    If've setup 2 Classes in the MainPlugin Class
    Code (Text):

    private void setupDatabase() {
    try {
    getDatabase().find(Player.class).findRowCount();
    getDatabase().find(Death.class).findRowCount();} catch (PersistenceException e) {
    getLogger().log(Level.INFO, "Installing database for " + getDescription().getName() + " due to first time usage");
    installDDL();}
    }

    @Overridepublic List<Class<?>> getDatabaseClasses() {
    List<Class<?>> list = new ArrayList<>();
    list.add(Death.class);
    list.add(Player.class);
     return list;}
     
    with the following Code

    Death.java
    Code (Text):

    @Entity()
    @Table(name = "cn_death_deaths")
    public class Death {

    @Id
    private int id;

    private Date deathDate;

    @ManyToOne
    // @JoinColumn(name = "victim", referencedColumnName = "playerId")
    private Player victim;

    @ManyToOne
    // @JoinColumn(name = "killer", referencedColumnName = "playerId")
    private Player killer;

    @NotNull
    private int x;
    @NotNull
    private int y;
    @NotNull
    private int z;
    private String world;
    private String message;

    ... (Getter, Setter)

    public void save() {
    CNDeathLoggerPlugin.getInstance().getDatabase().save(this);}

    public void delete() {
    CNDeathLoggerPlugin.getInstance().getDatabase().delete(this);}

     
    Player.java
    Code (Text):

    @Entity()
    @Table(name = "cn_death_player")
    public class Player {

    @Id
     @GeneratedValue @Column(name = "playerId")
    private Long id;
    @Column(unique=true, nullable=false)
    private String uuid;
    @OneToMany(mappedBy = "victim")
    public List<Death> deaths;
    @OneToMany(mappedBy = "killer")
    public List<Death> kills;

    ...Getter/Setter

    public void save() {
    CNDeathLoggerPlugin.getInstance().getDatabase().save(this);
    }

    public void delete() {
    CNDeathLoggerPlugin.getInstance().getDatabase().delete(this);
    }
     


    But if I start the plugin, it throws some constraint errors

    Code (Text):

    [10:14:50] [Server thread/INFO]: Closing Connection[CNDeathLogger.1] psReuse[0] psCreate[1] psSize[0]
    [10:14:50] [Server thread/INFO]: [CNDeathLogger] Installing database for CNDeathLogger due to first time usage
    [10:14:50] [Server thread/INFO]: runScript
    [10:14:50] [Server thread/INFO]: executing 1 of 6 create table cn_death_deaths ( id                        integer primary key, de...
    [10:14:50] [Server thread/INFO]: executing 2 of 6 create table cn_death_player ( playerId                  bigint primary key, uui...
    [10:14:50] [Server thread/INFO]: executing 3 of 6 alter table cn_death_deaths add constraint fk_cn_death_deaths_victim_1 foreign k...
    [10:14:50] [Server thread/INFO]: Can not test connection as heartbeatsql is not set
    [10:14:50] [Server thread/INFO]: Closing Connection[CNDeathLogger.0] psReuse[0] psCreate[3] psSize[2]
    [10:14:50] [Server thread/INFO]: DataSourcePool [CNDeathLogger] grow; id[CNDeathLogger.2] busy[3] max[20]
    [10:14:50] [Server thread/ERROR]: Error occurred while enabling CNDeathLogger v1.0-SNAPSHOT (Is it up to date?)
    javax.persistence.PersistenceException: Error: Error executing stmt[alter table cn_death_deaths add constraint fk_cn_death_deaths_victim_1 foreign key (victim_playerId) references cn_death_player (playerId)] error[[SQLITE_ERROR] SQL error or missing database (near "constraint": syntax error)]
        at com.avaje.ebeaninternal.server.ddl.DdlGenerator.runScript(DdlGenerator.java:233) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.bukkit.plugin.java.JavaPlugin.installDDL(JavaPlugin.java:477) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at de.cubenation.cndeathlogger.CNDeathLoggerPlugin.setupDatabase(CNDeathLoggerPlugin.java:50) ~[?:?]
        at de.cubenation.cndeathlogger.CNDeathLoggerPlugin.onEnable(CNDeathLoggerPlugin.java:38) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:356) [spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:316) [spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:524) [spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_80]
    Caused by: java.lang.RuntimeException: Error executing stmt[alter table cn_death_deaths add constraint fk_cn_death_deaths_victim_1 foreign key (victim_playerId) references cn_death_player (playerId)] error[[SQLITE_ERROR] SQL error or missing database (near "constraint": syntax error)]
        at com.avaje.ebeaninternal.server.ddl.DdlGenerator.runStatement(DdlGenerator.java:281) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at com.avaje.ebeaninternal.server.ddl.DdlGenerator.runStatements(DdlGenerator.java:246) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at com.avaje.ebeaninternal.server.ddl.DdlGenerator.runScript(DdlGenerator.java:224) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        ... 14 more
    Caused by: java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near "constraint": syntax error)
        at org.sqlite.DB.newSQLException(DB.java:383) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.sqlite.DB.newSQLException(DB.java:387) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.sqlite.DB.throwex(DB.java:374) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.sqlite.NativeDB.prepare(Native Method) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.sqlite.DB.prepare(DB.java:123) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.sqlite.PrepStmt.<init>(PrepStmt.java:42) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.sqlite.Conn.prepareStatement(Conn.java:404) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.sqlite.Conn.prepareStatement(Conn.java:399) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at org.sqlite.Conn.prepareStatement(Conn.java:383) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at com.avaje.ebeaninternal.server.lib.sql.PooledConnection.prepareStatement(PooledConnection.java:426) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at com.avaje.ebeaninternal.server.lib.sql.PooledConnection.prepareStatement(PooledConnection.java:396) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at com.avaje.ebeaninternal.server.ddl.DdlGenerator.runStatement(DdlGenerator.java:271) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at com.avaje.ebeaninternal.server.ddl.DdlGenerator.runStatements(DdlGenerator.java:246) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        at com.avaje.ebeaninternal.server.ddl.DdlGenerator.runScript(DdlGenerator.java:224) ~[spigot.jar:git-Spigot-6d16e64-3e9b5c9]
        ... 14 more
     

    I hope somebody can help me with this :/
     
  2. Got it.
    This happens because if haven't setup the database in the bukkit.yml
    It looks like, a flat file can't handle this.
    I will keep it here as Solution for people with the same Problem :)
     
  3. Thank you for keeping this up as a solution! I've been experiencing a similar problem with a @ManyToMany relation. By setting up the database in the bukkit.yml, what do you mean exactly? Do you mean setting it to use an external MySQL server, or that there is some configuration required beyond a SQLite *.db per plugin? Thanks in advance!
     
  4. If I remember correctly it was because, the constraints could not be set with SQLite.
    But in my case it works with an external MySQL database.
     
    • Informative Informative x 1