Can a Set be a foreign key?

#1

I have two classes, Cell and Character.  Basically, the cell is like a room, and contains a variable (private Set<Long> cellHeldCharacters;) which holds the IDs of the characters within the cell (The IDs are Primary Keys).  Can I use the Foreign Key annotation for the variable, or is it illegal/unrecommended to do it for a Set?

For a better picture, I have:

@Entity
public class Character {

    @Id @GeneratedValue
    private long characterId;
    private String characterName;
    private Location characterLocation;

and:

@Entity
public class Cell {

    @Id @GeneratedValue
    private long cellID;
    private String cellName;
    private String cellShortDesc;
    private String cellLongDesc;
    private String cellBlockDesc;
    private Location cellLocation;
    private double cellSize;
    private Set<Long> cellHeldCharacters;

And I want to know if I can do this safely and retrieve it nicely later on:

@Entity
public class Cell {

    @Id @GeneratedValue
    private long cellID;
    private String cellName;
    private String cellShortDesc;
    private String cellLongDesc;
    private String cellBlockDesc;
    private Location cellLocation;
    private double cellSize;

    @ForeignKey
    private Set<Long> cellHeldCharacters;

 

#2

ORM annotations are ignored by ObjectDB, therefore the @ForeignKey annotation has no effect.

I wonder why Cell holds Character instances indirectly by their IDs, instead of directly:

  private Set<Character> cellHeldCharacters;

which is more common and usually more convenient when using JPA.

ObjectDB Support

Reply