-
-
Save LennardF1989/d2e5ea72bc38cf7d8183 to your computer and use it in GitHub Desktop.
Bukkit Persistance reimplemented - no code changes required!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.ManyToOne; | |
import javax.persistence.Table; | |
@Entity | |
@Table(name="comment") | |
public class Comment { | |
@Id | |
private int id; | |
@ManyToOne | |
private User user; | |
private String contents; | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public User getUser() { | |
return user; | |
} | |
public void setUser(User user) { | |
this.user = user; | |
} | |
public String getContents() { | |
return contents; | |
} | |
public void setContents(String contents) { | |
this.contents = contents; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lennardf1989.mydatabaseplugin; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import org.bukkit.util.config.Configuration; | |
import com.avaje.ebean.EbeanServer; | |
import com.lennardf1989.bukkitex.MyDatabase; | |
public class MyDatabasePlugin extends JavaPlugin { | |
private static MyDatabasePlugin plugin; | |
private MyDatabase database; | |
public void onEnable() { | |
try { | |
//Store reference to this class | |
plugin = this; | |
//Load the configuration | |
loadConfiguration(); | |
//Initialize the database | |
initializeDatabase(); | |
//Plugin succesfully enabled | |
System.out.print(String.format("[%s v%s] has been succesfully enabled!", getDescription().getName(), getDescription().getVersion())); | |
} | |
catch(Exception ex) { | |
//Plugin failed to enable | |
System.out.print(String.format("[%s v%s] could not be enabled!", getDescription().getName(), getDescription().getVersion())); | |
//Print the stack trace of the actual cause | |
Throwable t = ex; | |
while(t != null) { | |
if(t.getCause() == null) { | |
System.out.println(String.format("[%s v%s] exception:", getDescription().getName(), getDescription().getVersion())); | |
t.printStackTrace(); | |
} | |
t = t.getCause(); | |
} | |
} | |
} | |
public void onDisable() { | |
System.out.println(String.format("%s version %s is disabled!", getDescription().getName(), getDescription().getVersion())); | |
} | |
public static MyDatabasePlugin getInstance() { | |
return plugin; | |
} | |
private void loadConfiguration() { | |
Configuration config = getConfiguration(); | |
config.setProperty("database.driver", config.getString("database.driver", "org.sqlite.JDBC")); | |
config.setProperty("database.url", config.getString("database.url", "jdbc:sqlite:{DIR}{NAME}.db")); | |
config.setProperty("database.username", config.getString("database.username", "root")); | |
config.setProperty("database.password", config.getString("database.password", "")); | |
config.setProperty("database.isolation", config.getString("database.isolation", "SERIALIZABLE")); | |
config.setProperty("database.logging", config.getBoolean("database.logging", false)); | |
config.setProperty("database.rebuild", config.getBoolean("database.rebuild", true)); | |
config.save(); | |
} | |
private void initializeDatabase() { | |
Configuration config = getConfiguration(); | |
database = new MyDatabase(this) { | |
protected java.util.List<Class<?>> getDatabaseClasses() { | |
List<Class<?>> list = new ArrayList<Class<?>>(); | |
list.add(User.class); | |
list.add(Comment.class); | |
return list; | |
}; | |
}; | |
database.initializeDatabase( | |
config.getString("database.driver"), | |
config.getString("database.url"), | |
config.getString("database.username"), | |
config.getString("database.password"), | |
config.getString("database.isolation"), | |
config.getBoolean("database.logging", false), | |
config.getBoolean("database.rebuild", true) | |
); | |
config.setProperty("database.rebuild", false); | |
config.save(); | |
} | |
@Override | |
public EbeanServer getDatabase() { | |
return database.getDatabase(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import javax.persistence.CascadeType; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.OneToMany; | |
import javax.persistence.Table; | |
@Entity | |
@Table(name="user") | |
public class User { | |
@Id | |
private int id; | |
private String name; | |
@OneToMany(mappedBy="user", cascade=CascadeType.ALL) | |
private List<Comment> comments = new ArrayList<Comment>(); | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public List<Comment> getComments() { | |
return comments; | |
} | |
public void setComments(List<Comment> comments) { | |
this.comments = comments; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment