Bukkit Persistence and ManyToMany

Discussion in 'Plugin Development' started by croxis, Jul 25, 2011.

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

    croxis

    I'm attempting to get ManyToMany relationships working with Bukkit's built in persistence with MySQL databases. The tables are created, however the entries are not. I'm not sure where the issue is, if it is the annotations or some other part of the code. Here are the relevant parts:

    Code:
    @Entity()
    @Table(name = "towny_worlds")
    public class SQLWorld {
        @Id
        private int id;
    
        private String name;
    
        @ManyToMany
        @JoinTable(
                name="towny_WRLD_TWN",
                joinColumns=
                    @JoinColumn(name="SQLWORLD_ID", referencedColumnName="id"),
                inverseJoinColumns=
                    @JoinColumn(name="SQLTOWN_ID", referencedColumnName="id"))
        private List<SQLTown> towns;
    //More stuff and getters and setters
    Code:
    @Entity()
    @Table(name = "towny_towns")
    public class SQLTown {
        @Id
        private int id;
        //snip...
        @ManyToMany(mappedBy="towns")
        private List<SQLWorld> worlds;
    //More stuff and getters and setters
    Code:
    plugin.sendDebugMsg("Step 4 of x. Converting Worlds");
            for (TownyWorld world : universe.getWorlds()){
                try {
                    //SQLWorld w = plugin.getDatabase().find(SQLWorld.class).where().ieq("name", world.getName()).findUnique();
                    //if (w == null)
                    SQLWorld w = new SQLWorld();
                    w.setName(world.getName());
    
                    // Towns
                    List<SQLTown> towns = new ArrayList<SQLTown>();
                    for (Town town : world.getTowns()){
                        towns.add(plugin.getDatabase().find(SQLTown.class).where().ieq("name", town.getName()).findUnique());
                    }
                    plugin.sendDebugMsg("World: " + world.getName() + ". Length: " + towns.size());
                    w.setTowns(towns);
    //snip
    plugin.getDatabase().save(w);
    I've confirmed that the towns array is populated, but the towny_WRLD_TWN table remains empty. Any help would be fantastic!
     
Thread Status:
Not open for further replies.

Share This Page