Lists in persistence tables

Discussion in 'Plugin Development' started by .$pIrit, Apr 13, 2011.

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

    .$pIrit

    Hello guys!
    I would like to use the new persistence features in my plugins.
    It worked for me in one plugin, but i would like to use a list in a class which doesnt work the normal way ;)
    Maybe you can help me. I have to classes so far:
    Classes (open)
    Code:
    package de.sp1rit.signedit;
    
    import java.util.List;
    
    import javax.persistence.*;
    
    import org.bukkit.entity.Player;
    
    import com.avaje.ebean.validation.NotEmpty;
    
    @Entity
    @Table(name="se_signname")
    public class SignName {
    	@Id
    	private int id;
    	@NotEmpty
    	private String name;
    	@NotEmpty
    	private Player player;
    	@ManyToMany(cascade=CascadeType.ALL)
    	private List<SignLocation> signLocations;
    
    	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 Player getPlayer() {
    		return player;
    	}
    
    	public void setPlayer(Player player) {
    		this.player = player;
    	}
    
    	public List<SignLocation> getSignLocations() {
    		return signLocations;
    	}
    
    	public void setSignLocations(List<SignLocation> signLocations) {
    		this.signLocations = signLocations;
    	}
    }
    
    Code:
    package de.sp1rit.signedit;
    
    import javax.persistence.*;
    
    import org.bukkit.Bukkit;
    import org.bukkit.World;
    
    import com.avaje.ebean.validation.NotEmpty;
    import com.avaje.ebean.validation.NotNull;
    
    @Entity
    @Table(name="se_signlocation")
    public class SignLocation {
    	@Id
    	private int id;
    	@NotNull
    	@ManyToOne
    	private SignName signName;
    	@NotNull
    	@NotEmpty
    	private String world;
    	@NotNull
    	private double x;
    	@NotNull
    	private double y;
    	@NotNull
    	private double z;
    
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	public SignName getSignName() {
    		return signName;
    	}
    
    	public void setSignName(SignName signName) {
    		this.signName = signName;
    	}
    
    	public World getWorld() {
    		return Bukkit.getServer().getWorld(world);
    	}
    
    	public void setWorld(World world) {
    		this.world = world.getName();
    	}
    
    	public double getX() {
    		return x;
    	}
    
    	public void setX(double x) {
    		this.x = x;
    	}
    
    	public double getY() {
    		return y;
    	}
    
    	public void setY(double y) {
    		this.y = y;
    	}
    
    	public double getZ() {
    		return z;
    	}
    
    	public void setZ(double z) {
    		this.z = z;
    	}
    }
    


    Now my problem is the list in SignName. How do i add a SignLocation to it?
    Do i have to get the SignName with a query and add it in the normal way over the list?
    Or do i have to insert the SignLocation directly in the database with an own query?

    How does it work? ;)

    Regards

    No one?
    I would like to go on :p

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

    Sammy

    I too, am learning this at the moment, when I reach a conclusion I'll give you something, if you get to it first please let me know :)
     
  3. Offline

    Mixcoatl

    Greetings!
    What's the problem exactly?
    It looks like you already have. You may need to add join criteria (@JoinColumn annotation), depending upon what you're shooting for.
    Not at all. When you find for the object it should come back already populated.
    Again, no. When you insert the top level record it should perform the corresponding inserts. You may have to set the cascade type to get this to work. I'm not certain if it applies only to DELETE operations or INSERT operations as well.
    "Damn well!" I usually tell people. ;) I've not actually had an opportunity to use the @OneToMany/@ManyToMany syntax in a plug-in yet, but that's definitely the right course of action to get what you're doing. Both of these annotations have some arguments you can set to determine their exact behavior. Also look at the @JoinColumn annotation.
     
  4. Offline

    Sammy

  5. Offline

    Archelaus

    OneToMany doesn't work with SQLite.
     
  6. Offline

    Sammy

    @RightLegRed how come it doesn't support it ?
    I'm going to be honest, I never tried using(never really needed it), but I see that developers use it on android.
     
  7. Offline

    Archelaus

  8. Offline

    Sammy

    Well a couple of days ago I copy pasted a @onetomay code and it didn't worked, but I thought it was my bad skills... thanks for the heads up rightlegered :)
     
  9. Offline

    DThielke

    Indeed. I spent quite a few hours trying to figure out why I could get any collections or maps to work before finally running into an error about ADD CONSTRAINT in an SQL statement. Lo and behold, when I switched the database to mysql it worked fine.
     
  10. Offline

    .$pIrit

    So if OneToMany doesnt work atm, how should i work with the two tables? There are different ways to do it but i would like to know the "best" or way someone should do it.

    For example i could just add the SignName-ID to the SignLocation and to get the SignName off a SignLocation i would have to do two requests:
    Code:
    SignLocation tmpLoc = db.find(SignLocation.class).select("signName").where().eq("world", sign.getWorld().getName()).eq("x", sign.getX()).eq("y", sign.getY()).eq("z", sign.getZ()).findUnique();
    and
    Code:
    SignName tmpName = db.find(SignName.class, tmpLoc.getSignName());
    But this isnt a good way at all. I havent worked with fetch so far.

    How would you use it?
    I need to get it to work in both directions. Getting all (or a specific) SignLocations by the SignName and the SignName by the SignLocation.

    Regards
     
Thread Status:
Not open for further replies.

Share This Page